utils.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // 动态获取icon路径
  2. export const getIconUrl = url => {
  3. try {
  4. return require(`../assets/icons/${url}.png`)
  5. } catch (error) {
  6. console.error(`无法加载图标: ${url}`, error)
  7. return ''
  8. }
  9. }
  10. // 获取本地存储
  11. export const getLocalStorage = key => {
  12. if (typeof window !== 'undefined') {
  13. return window.localStorage.getItem(key)
  14. }
  15. return null
  16. }
  17. // 设置本地存储
  18. export const setLocalStorage = (key, value) => {
  19. if (typeof window !== 'undefined') {
  20. window.localStorage.setItem(key, value)
  21. }
  22. }
  23. // 格式化日期
  24. export const formatDate = date => {
  25. if (!date) return ''
  26. const now = new Date()
  27. const messageDate = new Date(date)
  28. // 如果是今天的邮件,只显示时间
  29. if (messageDate.toDateString() === now.toDateString()) {
  30. return messageDate.toLocaleTimeString('zh-CN', {
  31. hour: '2-digit',
  32. minute: '2-digit',
  33. second: '2-digit'
  34. })
  35. }
  36. // 如果是昨天的邮件,显示"昨天"加时间
  37. const yesterday = new Date(now)
  38. yesterday.setDate(now.getDate() - 1)
  39. if (messageDate.toDateString() === yesterday.toDateString()) {
  40. return (
  41. '昨天 ' +
  42. messageDate.toLocaleTimeString('zh-CN', {
  43. hour: '2-digit',
  44. minute: '2-digit',
  45. second: '2-digit'
  46. })
  47. )
  48. }
  49. // 其他情况显示完整日期 (yyyy-MM-dd格式)
  50. return (
  51. messageDate.getFullYear() +
  52. '-' +
  53. String(messageDate.getMonth() + 1).padStart(2, '0') +
  54. '-' +
  55. String(messageDate.getDate()).padStart(2, '0')
  56. )
  57. }