util.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. duration: 2000
  23. })
  24. }
  25. export function compareVersion(v1Text: string, v2Text: string): number {
  26. const v1 = v1Text.split('.')
  27. const v2 = v2Text.split('.')
  28. let out = 0;
  29. const len = Math.max(v1.length, v2.length)
  30. while (v1.length < len) {
  31. v1.push('0')
  32. }
  33. while (v2.length < len) {
  34. v2.push('0')
  35. }
  36. for (let i = 0; i < len; i++) {
  37. const num1 = parseInt(v1[i])
  38. const num2 = parseInt(v2[i])
  39. if (num1 > num2) {
  40. out = 1;
  41. break
  42. } else if (num1 < num2) {
  43. out = -1;
  44. break
  45. }
  46. }
  47. if (out === -1) {
  48. wx.showModal({
  49. title: '提示',
  50. showCancel: false,
  51. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  52. })
  53. }
  54. return out
  55. }