utils.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // #ifndef APP-NVUE
  2. // 计算版本
  3. export function compareVersion(v1, v2) {
  4. v1 = v1.split('.')
  5. v2 = v2.split('.')
  6. const len = Math.max(v1.length, v2.length)
  7. while (v1.length < len) {
  8. v1.push('0')
  9. }
  10. while (v2.length < len) {
  11. v2.push('0')
  12. }
  13. for (let i = 0; i < len; i++) {
  14. const num1 = parseInt(v1[i], 10)
  15. const num2 = parseInt(v2[i], 10)
  16. if (num1 > num2) {
  17. return 1
  18. } else if (num1 < num2) {
  19. return -1
  20. }
  21. }
  22. return 0
  23. }
  24. const systemInfo = uni.getSystemInfoSync();
  25. function gte(version) {
  26. // 截止 2023-03-22 mac pc小程序不支持 canvas 2d
  27. console.log('uni', uni)
  28. let { SDKVersion, platform } = systemInfo;
  29. // #ifdef MP-ALIPAY
  30. SDKVersion = my.SDKVersion
  31. // #endif
  32. // #ifdef MP-WEIXIN
  33. return platform !== 'mac' && compareVersion(SDKVersion, version) >= 0;
  34. // #endif
  35. return compareVersion(SDKVersion, version) >= 0;
  36. }
  37. export function canIUseCanvas2d() {
  38. // #ifdef MP-WEIXIN
  39. return gte('2.9.0');
  40. // #endif
  41. // #ifdef MP-ALIPAY
  42. return gte('2.7.0');
  43. // #endif
  44. // #ifdef MP-TOUTIAO
  45. return gte('1.78.0');
  46. // #endif
  47. return false
  48. }
  49. export function wrapTouch(event) {
  50. for (let i = 0; i < event.touches.length; ++i) {
  51. const touch = event.touches[i];
  52. touch.offsetX = touch.x;
  53. touch.offsetY = touch.y;
  54. }
  55. return event;
  56. }
  57. export const devicePixelRatio = uni.getSystemInfoSync().pixelRatio
  58. // #endif
  59. // #ifdef APP-NVUE
  60. export function base64ToPath(base64) {
  61. return new Promise((resolve, reject) => {
  62. const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64) || [];
  63. const bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
  64. bitmap.loadBase64Data(base64, () => {
  65. if (!format) {
  66. reject(new Error('ERROR_BASE64SRC_PARSE'))
  67. }
  68. const time = new Date().getTime();
  69. const filePath = `_doc/uniapp_temp/${time}.${format}`
  70. bitmap.save(filePath, {},
  71. () => {
  72. bitmap.clear()
  73. resolve(filePath)
  74. },
  75. (error) => {
  76. bitmap.clear()
  77. console.error(`${JSON.stringify(error)}`)
  78. reject(error)
  79. })
  80. }, (error) => {
  81. bitmap.clear()
  82. console.error(`${JSON.stringify(error)}`)
  83. reject(error)
  84. })
  85. })
  86. }
  87. // #endif
  88. export function sleep(time) {
  89. return new Promise((resolve) => {
  90. setTimeout(() => {
  91. resolve(true)
  92. }, time)
  93. })
  94. }
  95. export function getRect(selector, options = {}) {
  96. const typeDefault = 'boundingClientRect'
  97. const { context, type = typeDefault} = options
  98. return new Promise((resolve, reject) => {
  99. const dom = uni.createSelectorQuery().in(context).select(selector);
  100. const result = (rect) => {
  101. if(rect) {
  102. resolve(rect)
  103. } else {
  104. reject()
  105. }
  106. }
  107. if(type == typeDefault) {
  108. dom[type](result).exec()
  109. } else {
  110. dom[type]({
  111. node: true,
  112. size: true,
  113. rect: true
  114. }, result).exec()
  115. }
  116. });
  117. };