utils.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. export function compareVersion(v1, v2) {
  2. v1 = v1.split('.')
  3. v2 = v2.split('.')
  4. const len = Math.max(v1.length, v2.length)
  5. while (v1.length < len) {
  6. v1.push('0')
  7. }
  8. while (v2.length < len) {
  9. v2.push('0')
  10. }
  11. for (let i = 0; i < len; i++) {
  12. const num1 = parseInt(v1[i], 10)
  13. const num2 = parseInt(v2[i], 10)
  14. if (num1 > num2) {
  15. return 1
  16. } else if (num1 < num2) {
  17. return -1
  18. }
  19. }
  20. return 0
  21. }
  22. function gte(version) {
  23. let { SDKVersion } = uni.getSystemInfoSync()
  24. // #ifdef MP-ALIPAY
  25. SDKVersion = my.SDKVersion
  26. // #endif
  27. return compareVersion(SDKVersion, version) >= 0;
  28. }
  29. export function canIUseCanvas2d() {
  30. // #ifdef MP-WEIXIN
  31. return gte('2.9.0');
  32. // #endif
  33. // #ifdef MP-ALIPAY
  34. return gte('2.7.0');
  35. // #endif
  36. // #ifdef MP-TOUTIAO
  37. return gte('1.78.0');
  38. // #endif
  39. return false
  40. }
  41. export const wrapEvent = (e) => {
  42. if (!e) return;
  43. if (!e.preventDefault) {
  44. e.preventDefault = function() {};
  45. }
  46. return e;
  47. }
  48. export const requestAnimationFrame = (cb) => {
  49. setTimeout(cb, 30)
  50. }
  51. // #ifdef MP
  52. export const prefix = () => {
  53. // #ifdef MP-TOUTIAO
  54. return tt
  55. // #endif
  56. // #ifdef MP-WEIXIN
  57. return wx
  58. // #endif
  59. // #ifdef MP-BAIDU
  60. return swan
  61. // #endif
  62. // #ifdef MP-ALIPAY
  63. return my
  64. // #endif
  65. // #ifdef MP-QQ
  66. return qq
  67. // #endif
  68. // #ifdef MP-360
  69. return qh
  70. // #endif
  71. }
  72. // #endif
  73. /**
  74. * base64转路径
  75. * @param {Object} base64
  76. */
  77. export function base64ToPath(base64) {
  78. const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64) || [];
  79. return new Promise((resolve, reject) => {
  80. // #ifdef MP
  81. const p = prefix()
  82. const fs = p.getFileSystemManager()
  83. //自定义文件名
  84. if (!format) {
  85. reject(new Error('ERROR_BASE64SRC_PARSE'))
  86. }
  87. const time = new Date().getTime();
  88. const filePath = `${p.env.USER_DATA_PATH}/${time}.${format}`;
  89. fs.writeFile({
  90. filePath,
  91. data: base64.split(',')[1],
  92. encoding: 'base64',
  93. success() {
  94. resolve(filePath)
  95. },
  96. fail(err) {
  97. reject(err)
  98. }
  99. })
  100. // #endif
  101. // #ifdef APP-PLUS
  102. const bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
  103. bitmap.loadBase64Data(base64, () => {
  104. if (!format) {
  105. reject(new Error('ERROR_BASE64SRC_PARSE'))
  106. }
  107. const time = new Date().getTime();
  108. const filePath = `_doc/uniapp_temp/${time}.${format}`
  109. bitmap.save(filePath, {},
  110. () => {
  111. bitmap.clear()
  112. resolve(filePath)
  113. },
  114. (error) => {
  115. bitmap.clear()
  116. reject(error)
  117. })
  118. }, (error) => {
  119. bitmap.clear()
  120. reject(error)
  121. })
  122. // #endif
  123. })
  124. }
  125. export function sleep(delay) {
  126. return new Promise(resolve => setTimeout(resolve, delay))
  127. }
  128. export function getRect(selector, options = {}) {
  129. const typeDefault = 'boundingClientRect'
  130. const { context, type = typeDefault} = options
  131. return new Promise((resolve, reject) => {
  132. const dom = uni.createSelectorQuery().in(context).select(selector);
  133. const result = (rect) => {
  134. if(rect) {
  135. resolve(rect)
  136. } else {
  137. reject()
  138. }
  139. }
  140. if(type == typeDefault) {
  141. dom[type](result).exec()
  142. } else {
  143. dom[type]({
  144. node: true,
  145. size: true,
  146. rect: true
  147. }, result).exec()
  148. }
  149. });
  150. };
  151. export function isTransparent(color) {
  152. // 判断颜色是否为 transparent
  153. if (color === 'transparent') {
  154. return true;
  155. }
  156. // 判断颜色是否为 rgba 的 a 为 0
  157. if (color.startsWith('rgba')) {
  158. const regex = /\d+(\.\d+)?/g;
  159. const matches = color.match(regex);
  160. if (matches !== null) {
  161. const alpha = parseFloat(matches[3]);
  162. if (alpha === 0) {
  163. return true;
  164. }
  165. }
  166. }
  167. return false;
  168. }