utils.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. export const getCanvas2d = () => {
  23. let {SDKVersion, uniPlatform} = uni.getSystemInfoSync()
  24. if(!uniPlatform) {
  25. // #ifdef MP-WEIXIN
  26. uniPlatform = 'mp-weixin'
  27. // #endif
  28. // #ifdef MP-MP-ALIPAY
  29. SDKVersion = my.SDKVersion
  30. uniPlatform = 'mp-alipay'
  31. // #endif
  32. // #ifdef MP-MP-ALIPAY
  33. uniPlatform = 'mp-toutiao'
  34. // #endif
  35. }
  36. const MAP = {
  37. 'mp-weixin': '2.9.7',
  38. 'mp-toutiao': '1.78.0',
  39. 'mp-alipay': '2.7.0'
  40. }[uniPlatform]
  41. return MAP && SDKVersion && compareVersion(SDKVersion, MAP) >= 1
  42. }
  43. export const wrapEvent = (e) => {
  44. if (!e) return;
  45. if (!e.preventDefault) {
  46. e.preventDefault = function() {};
  47. }
  48. return e;
  49. }
  50. export const requestAnimationFrame = (cb) => {
  51. setTimeout(cb, 30)
  52. }
  53. /**
  54. * base64转路径
  55. * @param {Object} base64
  56. */
  57. export function base64ToPath(base64) {
  58. const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64) || [];
  59. return new Promise((resolve, reject) => {
  60. const bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
  61. bitmap.loadBase64Data(base64, () => {
  62. if (!format) {
  63. reject(new Error('ERROR_BASE64SRC_PARSE'))
  64. }
  65. const time = new Date().getTime();
  66. const filePath = `_doc/uniapp_temp/${time}.${format}`
  67. bitmap.save(filePath, {},
  68. () => {
  69. bitmap.clear()
  70. resolve(filePath)
  71. },
  72. (error) => {
  73. bitmap.clear()
  74. reject(error)
  75. })
  76. }, (error) => {
  77. bitmap.clear()
  78. reject(error)
  79. })
  80. })
  81. }
  82. export function sleep(delay) {
  83. return new Promise(resolve => setTimeout(resolve, delay))
  84. }