utils.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. let {
  28. SDKVersion,
  29. platform
  30. } = systemInfo;
  31. // #ifdef MP-ALIPAY
  32. SDKVersion = my.SDKVersion
  33. // #endif
  34. // #ifdef MP-WEIXIN
  35. return platform !== 'mac' && compareVersion(SDKVersion, version) >= 0;
  36. // #endif
  37. return compareVersion(SDKVersion, version) >= 0;
  38. }
  39. export function canIUseCanvas2d() {
  40. // #ifdef MP-WEIXIN
  41. return gte('2.9.0');
  42. // #endif
  43. // #ifdef MP-ALIPAY
  44. return gte('2.7.0');
  45. // #endif
  46. // #ifdef MP-TOUTIAO
  47. return gte('1.78.0');
  48. // #endif
  49. return false
  50. }
  51. export function convertTouchesToArray(touches) {
  52. // 如果 touches 是一个数组,则直接返回它
  53. if (Array.isArray(touches)) {
  54. return touches;
  55. }
  56. // 如果touches是一个对象,则转换为数组
  57. if (typeof touches === 'object' && touches !== null) {
  58. return Object.values(touches);
  59. }
  60. // 对于其他类型,直接返回它
  61. return touches;
  62. }
  63. export function wrapTouch(event) {
  64. for (let i = 0; i < event.touches.length; ++i) {
  65. const touch = event.touches[i];
  66. touch.offsetX = touch.x;
  67. touch.offsetY = touch.y;
  68. }
  69. return event;
  70. }
  71. export const devicePixelRatio = uni.getSystemInfoSync().pixelRatio
  72. // #endif
  73. // #ifdef APP-NVUE
  74. export function base64ToPath(base64) {
  75. return new Promise((resolve, reject) => {
  76. const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64) || [];
  77. const bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
  78. bitmap.loadBase64Data(base64, () => {
  79. if (!format) {
  80. reject(new Error('ERROR_BASE64SRC_PARSE'))
  81. }
  82. const time = new Date().getTime();
  83. const filePath = `_doc/uniapp_temp/${time}.${format}`
  84. bitmap.save(filePath, {},
  85. () => {
  86. bitmap.clear()
  87. resolve(filePath)
  88. },
  89. (error) => {
  90. bitmap.clear()
  91. console.error(`${JSON.stringify(error)}`)
  92. reject(error)
  93. })
  94. }, (error) => {
  95. bitmap.clear()
  96. console.error(`${JSON.stringify(error)}`)
  97. reject(error)
  98. })
  99. })
  100. }
  101. // #endif
  102. export function sleep(time) {
  103. return new Promise((resolve) => {
  104. setTimeout(() => {
  105. resolve(true)
  106. }, time)
  107. })
  108. }
  109. export function getRect(selector, options = {}) {
  110. const typeDefault = 'boundingClientRect'
  111. const {
  112. context,
  113. type = typeDefault
  114. } = options
  115. return new Promise((resolve, reject) => {
  116. const dom = uni.createSelectorQuery().in(context).select(selector);
  117. const result = (rect) => {
  118. if (rect) {
  119. resolve(rect)
  120. } else {
  121. reject()
  122. }
  123. }
  124. if (type == typeDefault) {
  125. dom[type](result).exec()
  126. } else {
  127. dom[type]({
  128. node: true,
  129. size: true,
  130. rect: true
  131. }, result).exec()
  132. }
  133. });
  134. };