util.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. export const formatTime = (date: Date) => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return (
  9. [year, month, day].map(formatNumber).join('/') +
  10. ' ' +
  11. [hour, minute, second].map(formatNumber).join(':')
  12. )
  13. }
  14. const formatNumber = (n: number) => {
  15. const s = n.toString()
  16. return s[1] ? s : '0' + s
  17. }
  18. export function errHttp(msg: string) {
  19. wx.showToast({
  20. title: msg || "请稍后在试!",
  21. icon: "error"
  22. })
  23. }
  24. export function compareVersion(v1Text: string, v2Text: string): number {
  25. const v1 = v1Text.split('.')
  26. const v2 = v2Text.split('.')
  27. let out = 0;
  28. const len = Math.max(v1.length, v2.length)
  29. while (v1.length < len) {
  30. v1.push('0')
  31. }
  32. while (v2.length < len) {
  33. v2.push('0')
  34. }
  35. for (let i = 0; i < len; i++) {
  36. const num1 = parseInt(v1[i])
  37. const num2 = parseInt(v2[i])
  38. if (num1 > num2) {
  39. out = 1;
  40. break
  41. } else if (num1 < num2) {
  42. out = -1;
  43. break
  44. }
  45. }
  46. if (out === -1) {
  47. wx.showModal({
  48. title: '提示',
  49. showCancel: false,
  50. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  51. })
  52. }
  53. return out
  54. }