| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- import Taro from "@tarojs/taro";
- const utc = require('dayjs/plugin/utc')
- const timezone = require('dayjs/plugin/timezone')
- import dayjs from 'dayjs'
- dayjs.extend(utc)
- dayjs.extend(timezone)
- export function alphaToHex(alpha) {
- var alphaValue = Math.round(alpha * 255); // 将透明度乘以255并四舍五入
- var hexValue = alphaValue.toString(16); // 将整数转换为十六进制字符串
- if (hexValue.length === 1) {
- hexValue = "0" + hexValue; // 如果十六进制字符串只有一位,补零
- }
- return hexValue;
- }
- export function rgbaToRgb(rgba) {
- if (rgba.startsWith("rgba")) {
- // 提取 RGBA 值
- const regex = /rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+(\.\d+)?)\s*\)/;
- const matches = rgba.match(regex);
- if (matches && matches.length === 6) {
- const red = parseInt(matches[1]);
- const green = parseInt(matches[2]);
- const blue = parseInt(matches[3]);
- const alpha = parseFloat(matches[4]);
- // 计算 RGB 值
- const rgbRed = Math.round(red * alpha);
- const rgbGreen = Math.round(green * alpha);
- const rgbBlue = Math.round(blue * alpha);
- return `rgb(${rgbRed}, ${rgbGreen}, ${rgbBlue})`;
- }
- }
- // 如果无法解析或不是 RGBA 值,则返回原始值
- return rgba;
- }
- export function rpxToPx(n: number) {
- var rate = Taro.getSystemInfoSync().windowWidth / 750;
- return n * rate;
- }
- export function vibrate(type?: string) {
- if (process.env.TARO_ENV == 'rn') {
- const ReactNativeHapticFeedback = require("react-native-haptic-feedback")
- // Optional configuration
- const options = {
- enableVibrateFallback: true,
- ignoreAndroidSystemSettings: false,
- };
- // Trigger haptic feedback
- ReactNativeHapticFeedback.trigger("impactLight", options);
- }
- else {
- Taro.vibrateShort({
- type: 'medium'
- })
- }
- }
- //微信sdk版本比较
- export function compareVersion(v1, v2) {
- v1 = v1.split('.')
- v2 = v2.split('.')
- const len = Math.max(v1.length, v2.length)
- while (v1.length < len) {
- v1.push('0')
- }
- while (v2.length < len) {
- v2.push('0')
- }
- for (let i = 0; i < len; i++) {
- const num1 = parseInt(v1[i])
- const num2 = parseInt(v2[i])
- if (num1 > num2) {
- return 1
- } else if (num1 < num2) {
- return -1
- }
- }
- return 0
- }
- export function getTimezone() {
- var split = new Date().toString().split(' ');
- var timeZoneFormatted = '';
- split.map(item => {
- if (item.indexOf('GMT') != -1) {
- timeZoneFormatted = item;
- }
- })
- return timeZoneFormatted;
- }
- export function getTimezoneName() {
- var split = new Date().toString().split('(');
- if (split.length < 2) {
- return '';
- }
- return split[1].split(')')[0];
- }
- export function getTimezoneId() {
- if (process.env.TARO_ENV == 'weapp') {
- if (kIsIOS) {
- return dayjs.tz.guess()
- }
- return '';
- }
- else {
- if (kIsIOS) {
- return dayjs.tz.guess()
- }
- const isDebug = __DEV__;
- if (isDebug) {
- return 'Asia/Shanghai'
- }
- var getTimeZone = require('react-native-localize').getTimeZone
- return getTimeZone()
- }
- }
- export const kIsIOS = Taro.getSystemInfoSync().platform == 'ios'
- export const kIsAndroid = Taro.getSystemInfoSync().platform == 'android'
|