index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * 通用工具类
  3. * 此代码由AI生成
  4. */
  5. /**
  6. * 格式化时间
  7. * @param {string|Date} time
  8. * @param {string} pattern
  9. */
  10. export function formatTime(time, pattern = 'yyyy-MM-dd HH:mm:ss') {
  11. if (!time) return ''
  12. const date = new Date(time)
  13. const o = {
  14. 'M+': date.getMonth() + 1,
  15. 'd+': date.getDate(),
  16. 'H+': date.getHours(),
  17. 'm+': date.getMinutes(),
  18. 's+': date.getSeconds()
  19. }
  20. if (/(y+)/.test(pattern)) {
  21. pattern = pattern.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  22. }
  23. for (const k in o) {
  24. if (new RegExp('(' + k + ')').test(pattern)) {
  25. pattern = pattern.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
  26. }
  27. }
  28. return pattern
  29. }
  30. /**
  31. * 日期格式化展示(5分钟前,今天 HH:mm 等)
  32. */
  33. export function displayTime(time) {
  34. if (!time) return ''
  35. const date = new Date(time)
  36. const now = new Date()
  37. const diff = now.getTime() - date.getTime()
  38. if (diff < 60000) return '刚刚'
  39. if (diff < 3600000) return Math.floor(diff / 60000) + '分钟前'
  40. if (now.toDateString() === date.toDateString()) return '今天 ' + formatTime(date, 'HH:mm')
  41. const yesterday = new Date(now.getTime() - 86400000)
  42. if (yesterday.toDateString() === date.toDateString()) return '昨天 ' + formatTime(date, 'HH:mm')
  43. return formatTime(date, 'MM-dd HH:mm')
  44. }
  45. /**
  46. * 解密/解码 Base64 字符串为 UTF-8 字符串
  47. * @param {string} str - Base64 编码的字符串
  48. * @returns {string}
  49. */
  50. export function decodeBase64(str) {
  51. if (!str) return ''
  52. const cleanStr = String(str).trim().replace(/\s+/g, '')
  53. // 检查是否为 base64 格式
  54. const isBase64 = /^[A-Za-z0-9+/=]+$/.test(cleanStr)
  55. if (!isBase64) return str
  56. try {
  57. // 1. Base64 解码为二进制字符串
  58. const atobPolyfill = (s) => {
  59. if (typeof atob === 'function') return atob(s)
  60. const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
  61. let output = ''
  62. const clean = s.replace(/=+$/, '')
  63. for (let bc = 0, bs, buffer, idx = 0; idx < clean.length; ) {
  64. const char = clean.charAt(idx++)
  65. const buffer = chars.indexOf(char)
  66. if (buffer === -1) throw new Error('Invalid base64 character')
  67. bs = bc % 4 ? bs * 64 + buffer : buffer
  68. if (bc++ % 4) {
  69. output += String.fromCharCode(255 & bs >> (-2 * bc & 6))
  70. }
  71. }
  72. return output
  73. }
  74. const decodedBinary = atobPolyfill(cleanStr)
  75. // 2. 将二进制字符串解码为 UTF-8 字符串
  76. const decodeUtf8 = (bin) => {
  77. let result = ''
  78. let i = 0
  79. while (i < bin.length) {
  80. const c1 = bin.charCodeAt(i++)
  81. if (c1 < 128) {
  82. result += String.fromCharCode(c1)
  83. } else if (c1 > 191 && c1 < 224) {
  84. const c2 = bin.charCodeAt(i++)
  85. result += String.fromCharCode(((c1 & 31) << 6) | (c2 & 63))
  86. } else if (c1 > 223 && c1 < 240) {
  87. const c2 = bin.charCodeAt(i++)
  88. const c3 = bin.charCodeAt(i++)
  89. result += String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63))
  90. } else {
  91. const c2 = bin.charCodeAt(i++)
  92. const c3 = bin.charCodeAt(i++)
  93. const c4 = bin.charCodeAt(i++)
  94. const codepoint = ((c1 & 7) << 18) | ((c2 & 63) << 12) | ((c3 & 63) << 6) | (c4 & 63)
  95. if (codepoint > 0xffff) {
  96. const offset = codepoint - 0x10000
  97. result += String.fromCharCode((offset >> 10) + 0xd800, (offset & 0x3ff) + 0xdc00)
  98. } else {
  99. result += String.fromCharCode(codepoint)
  100. }
  101. }
  102. }
  103. return result
  104. }
  105. return decodeUtf8(decodedBinary)
  106. } catch (e) {
  107. console.error('Base64 decode failed:', e)
  108. return str
  109. }
  110. }