utils.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 wrapTouch(event) {
  25. for (let i = 0; i < event.touches.length; ++i) {
  26. const touch = event.touches[i];
  27. touch.offsetX = touch.x;
  28. touch.offsetY = touch.y;
  29. }
  30. return event;
  31. }
  32. export const devicePixelRatio = wx.getSystemInfoSync().pixelRatio
  33. // #endif
  34. // #ifdef APP-NVUE
  35. export function base64ToPath(base64) {
  36. return new Promise((resolve, reject) => {
  37. const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64) || [];
  38. const bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
  39. bitmap.loadBase64Data(base64, () => {
  40. if (!format) {
  41. reject(new Error('ERROR_BASE64SRC_PARSE'))
  42. }
  43. const time = new Date().getTime();
  44. const filePath = `_doc/uniapp_temp/${time}.${format}`
  45. bitmap.save(filePath, {},
  46. () => {
  47. bitmap.clear()
  48. resolve(filePath)
  49. },
  50. (error) => {
  51. bitmap.clear()
  52. console.error(`${JSON.stringify(error)}`)
  53. reject(error)
  54. })
  55. }, (error) => {
  56. bitmap.clear()
  57. console.error(`${JSON.stringify(error)}`)
  58. reject(error)
  59. })
  60. })
  61. }
  62. // #endif
  63. export function sleep(time) {
  64. return new Promise((resolve) => {
  65. setTimeout(() => {
  66. resolve(true)
  67. },time)
  68. })
  69. }