utils.js 3.9 KB

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