tools.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import Taro from "@tarojs/taro";
  2. const utc = require('dayjs/plugin/utc')
  3. const timezone = require('dayjs/plugin/timezone')
  4. import dayjs from 'dayjs'
  5. dayjs.extend(utc)
  6. dayjs.extend(timezone)
  7. export function alphaToHex(alpha) {
  8. var alphaValue = Math.round(alpha * 255); // 将透明度乘以255并四舍五入
  9. var hexValue = alphaValue.toString(16); // 将整数转换为十六进制字符串
  10. if (hexValue.length === 1) {
  11. hexValue = "0" + hexValue; // 如果十六进制字符串只有一位,补零
  12. }
  13. return hexValue;
  14. }
  15. export function rgbaToRgb(rgba) {
  16. if (rgba.startsWith("rgba")) {
  17. // 提取 RGBA 值
  18. const regex = /rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+(\.\d+)?)\s*\)/;
  19. const matches = rgba.match(regex);
  20. if (matches && matches.length === 6) {
  21. const red = parseInt(matches[1]);
  22. const green = parseInt(matches[2]);
  23. const blue = parseInt(matches[3]);
  24. const alpha = parseFloat(matches[4]);
  25. // 计算 RGB 值
  26. const rgbRed = Math.round(red * alpha);
  27. const rgbGreen = Math.round(green * alpha);
  28. const rgbBlue = Math.round(blue * alpha);
  29. return `rgb(${rgbRed}, ${rgbGreen}, ${rgbBlue})`;
  30. }
  31. }
  32. // 如果无法解析或不是 RGBA 值,则返回原始值
  33. return rgba;
  34. }
  35. export function rpxToPx(n: number) {
  36. var rate = Taro.getSystemInfoSync().windowWidth / 750;
  37. return n * rate;
  38. }
  39. export function vibrate(type?: string) {
  40. if (process.env.TARO_ENV == 'rn') {
  41. const ReactNativeHapticFeedback = require("react-native-haptic-feedback")
  42. // Optional configuration
  43. const options = {
  44. enableVibrateFallback: true,
  45. ignoreAndroidSystemSettings: false,
  46. };
  47. // Trigger haptic feedback
  48. ReactNativeHapticFeedback.trigger("impactLight", options);
  49. }
  50. else {
  51. Taro.vibrateShort({
  52. type: 'medium'
  53. })
  54. }
  55. }
  56. //微信sdk版本比较
  57. export function compareVersion(v1, v2) {
  58. v1 = v1.split('.')
  59. v2 = v2.split('.')
  60. const len = Math.max(v1.length, v2.length)
  61. while (v1.length < len) {
  62. v1.push('0')
  63. }
  64. while (v2.length < len) {
  65. v2.push('0')
  66. }
  67. for (let i = 0; i < len; i++) {
  68. const num1 = parseInt(v1[i])
  69. const num2 = parseInt(v2[i])
  70. if (num1 > num2) {
  71. return 1
  72. } else if (num1 < num2) {
  73. return -1
  74. }
  75. }
  76. return 0
  77. }
  78. export function getTimezone() {
  79. var split = new Date().toString().split(' ');
  80. var timeZoneFormatted = '';
  81. split.map(item => {
  82. if (item.indexOf('GMT') != -1) {
  83. timeZoneFormatted = item;
  84. }
  85. })
  86. return timeZoneFormatted;
  87. }
  88. export function getTimezoneName() {
  89. var split = new Date().toString().split('(');
  90. if (split.length < 2) {
  91. return '';
  92. }
  93. return split[1].split(')')[0];
  94. }
  95. export function getTimezoneId() {
  96. if (process.env.TARO_ENV == 'weapp') {
  97. if (kIsIOS) {
  98. return dayjs.tz.guess()
  99. }
  100. return '';
  101. }
  102. else {
  103. if (kIsIOS) {
  104. return dayjs.tz.guess()
  105. }
  106. const isDebug = __DEV__;
  107. if (isDebug) {
  108. return 'Asia/Shanghai'
  109. }
  110. var getTimeZone = require('react-native-localize').getTimeZone
  111. return getTimeZone()
  112. }
  113. }
  114. export const kIsIOS = Taro.getSystemInfoSync().platform == 'ios'
  115. export const kIsAndroid = Taro.getSystemInfoSync().platform == 'android'