utils.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. export function wrapEvent(e) {
  25. if (!e) return;
  26. if (!e.preventDefault) {
  27. e.preventDefault = function() {};
  28. }
  29. return e;
  30. }
  31. export const pixelRatio = uni.getSystemInfoSync().pixelRatio
  32. // #endif
  33. // #ifdef APP-NVUE
  34. export function base64ToPath(base64) {
  35. return new Promise((resolve, reject) => {
  36. const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64) || [];
  37. const bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
  38. bitmap.loadBase64Data(base64, () => {
  39. if (!format) {
  40. reject(new Error('ERROR_BASE64SRC_PARSE'))
  41. }
  42. const time = new Date().getTime();
  43. const filePath = `_doc/uniapp_temp/${time}.${format}`
  44. bitmap.save(filePath, {},
  45. () => {
  46. bitmap.clear()
  47. resolve(filePath)
  48. },
  49. (error) => {
  50. bitmap.clear()
  51. console.error(`${JSON.stringify(error)}`)
  52. reject(error)
  53. })
  54. }, (error) => {
  55. bitmap.clear()
  56. console.error(`${JSON.stringify(error)}`)
  57. reject(error)
  58. })
  59. })
  60. }
  61. // #endif