utils.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // @ts-nocheck
  2. /**
  3. * 获取设备基础信息
  4. *
  5. * @see [uni.getDeviceInfo](https://uniapp.dcloud.net.cn/api/system/getDeviceInfo.html)
  6. */
  7. export function getDeviceInfo() {
  8. if (uni.getDeviceInfo || uni.canIUse('getDeviceInfo')) {
  9. return uni.getDeviceInfo();
  10. } else {
  11. return uni.getSystemInfoSync();
  12. }
  13. }
  14. /**
  15. * 获取窗口信息
  16. *
  17. * @see [uni.getWindowInfo](https://uniapp.dcloud.net.cn/api/system/getWindowInfo.html)
  18. */
  19. export function getWindowInfo() {
  20. if (uni.getWindowInfo || uni.canIUse('getWindowInfo')) {
  21. return uni.getWindowInfo();
  22. } else {
  23. return uni.getSystemInfoSync();
  24. }
  25. }
  26. /**
  27. * 获取APP基础信息
  28. *
  29. * @see [uni.getAppBaseInfo](https://uniapp.dcloud.net.cn/api/system/getAppBaseInfo.html)
  30. */
  31. export function getAppBaseInfo() {
  32. if (uni.getAppBaseInfo || uni.canIUse('getAppBaseInfo')) {
  33. return uni.getAppBaseInfo();
  34. } else {
  35. return uni.getSystemInfoSync();
  36. }
  37. }
  38. // #ifndef APP-NVUE
  39. // 计算版本
  40. export function compareVersion(v1, v2) {
  41. v1 = v1.split('.')
  42. v2 = v2.split('.')
  43. const len = Math.max(v1.length, v2.length)
  44. while (v1.length < len) {
  45. v1.push('0')
  46. }
  47. while (v2.length < len) {
  48. v2.push('0')
  49. }
  50. for (let i = 0; i < len; i++) {
  51. const num1 = parseInt(v1[i], 10)
  52. const num2 = parseInt(v2[i], 10)
  53. if (num1 > num2) {
  54. return 1
  55. } else if (num1 < num2) {
  56. return -1
  57. }
  58. }
  59. return 0
  60. }
  61. // const systemInfo = uni.getSystemInfoSync();
  62. function gte(version) {
  63. // 截止 2023-03-22 mac pc小程序不支持 canvas 2d
  64. // let {
  65. // SDKVersion,
  66. // platform
  67. // } = systemInfo;
  68. const { platform } = getDeviceInfo();
  69. let { SDKVersion } = getAppBaseInfo();
  70. // #ifdef MP-ALIPAY
  71. SDKVersion = my.SDKVersion
  72. // #endif
  73. // #ifdef MP-WEIXIN
  74. return platform !== 'mac' && compareVersion(SDKVersion, version) >= 0;
  75. // #endif
  76. return compareVersion(SDKVersion, version) >= 0;
  77. }
  78. export function canIUseCanvas2d() {
  79. // #ifdef MP-WEIXIN
  80. return gte('2.9.0');
  81. // #endif
  82. // #ifdef MP-ALIPAY
  83. return gte('2.7.0');
  84. // #endif
  85. // #ifdef MP-TOUTIAO
  86. return gte('1.78.0');
  87. // #endif
  88. return false
  89. }
  90. export function convertTouchesToArray(touches) {
  91. // 如果 touches 是一个数组,则直接返回它
  92. if (Array.isArray(touches)) {
  93. return touches;
  94. }
  95. // 如果touches是一个对象,则转换为数组
  96. if (typeof touches === 'object' && touches !== null) {
  97. return Object.values(touches);
  98. }
  99. // 对于其他类型,直接返回它
  100. return touches;
  101. }
  102. export function wrapTouch(event) {
  103. event.touches = convertTouchesToArray(event.touches)
  104. for (let i = 0; i < event.touches.length; ++i) {
  105. const touch = event.touches[i];
  106. touch.offsetX = touch.x;
  107. touch.offsetY = touch.y;
  108. }
  109. return event;
  110. }
  111. // export const devicePixelRatio = uni.getSystemInfoSync().pixelRatio
  112. export const devicePixelRatio = getWindowInfo().pixelRatio;
  113. // #endif
  114. // #ifdef APP-NVUE
  115. export function base64ToPath(base64) {
  116. return new Promise((resolve, reject) => {
  117. const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64) || [];
  118. const bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
  119. bitmap.loadBase64Data(base64, () => {
  120. if (!format) {
  121. reject(new Error('ERROR_BASE64SRC_PARSE'))
  122. }
  123. const time = new Date().getTime();
  124. const filePath = `_doc/uniapp_temp/${time}.${format}`
  125. bitmap.save(filePath, {},
  126. () => {
  127. bitmap.clear()
  128. resolve(filePath)
  129. },
  130. (error) => {
  131. bitmap.clear()
  132. console.error(`${JSON.stringify(error)}`)
  133. reject(error)
  134. })
  135. }, (error) => {
  136. bitmap.clear()
  137. console.error(`${JSON.stringify(error)}`)
  138. reject(error)
  139. })
  140. })
  141. }
  142. // #endif
  143. export function sleep(time) {
  144. return new Promise((resolve) => {
  145. setTimeout(() => {
  146. resolve(true)
  147. }, time)
  148. })
  149. }
  150. export function getRect(selector, context, node) {
  151. return new Promise((resolve, reject) => {
  152. const dom = uni.createSelectorQuery().in(context).select(selector);
  153. const result = (rect) => {
  154. if (rect) {
  155. resolve(rect)
  156. } else {
  157. reject()
  158. }
  159. }
  160. if (!node) {
  161. dom.boundingClientRect(result).exec()
  162. } else {
  163. dom.fields({
  164. node: true,
  165. size: true,
  166. rect: true
  167. }, result).exec()
  168. }
  169. });
  170. };