tools.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import moment from 'moment-timezone'
  2. import { useUserStore } from '@/stores/modules/user'
  3. const userStore = useUserStore()
  4. const formatString = computed(() => {
  5. return userStore.dateFormat || 'MM/DD/YYYY'
  6. })
  7. export const formatTimezone = (time: string, timezone?: string) => {
  8. if (!time) return '--'
  9. let formattedTime = ''
  10. if (time.length > 12) {
  11. formattedTime = moment(time).format(`${formatString.value} hh:mm A`)
  12. if (!timezone) {
  13. return formattedTime
  14. }
  15. let utcOffset = ''
  16. const timeZoneOffset = moment.tz(`${moment().year()}-01-01`, timezone).format('Z')
  17. // 替换 "+07:00" 为 "UTC+07"
  18. utcOffset = `(UTC${timeZoneOffset.slice(0, 3)})`
  19. return `${formattedTime} ${utcOffset}`
  20. } else {
  21. formattedTime = moment(time).format(formatString.value)
  22. return formattedTime
  23. }
  24. }
  25. /**
  26. * 返回传入地区的UTC时区格式化
  27. * @param timezone
  28. * @returns
  29. */
  30. export const getTimezone = (timezone: string): string => {
  31. if (!timezone) return ''
  32. const offset = moment.tz(`${moment().year()}-01-01`, timezone).format('Z')
  33. return `UTC${offset.slice(0, 3)}`
  34. }
  35. export const formatTimezoneByUTCorGMT = (time: string, timezone: string) => {
  36. if (!time) return '--'
  37. let formattedTime = ''
  38. formattedTime = moment(time).format(`${formatString.value} hh:mm A`)
  39. let gmtOffset = ''
  40. if (timezone != null) {
  41. const timeZoneOffset = moment().tz(timezone).format('Z')
  42. // 替换 "+07:00" 为 "GMT+07"
  43. if (timezone.includes('Seoul')) {
  44. gmtOffset = `(UTC${timeZoneOffset.slice(0, 3)})`
  45. } else {
  46. gmtOffset = `(GMT${timeZoneOffset.slice(0, 3)})`
  47. }
  48. return `${formattedTime} ${gmtOffset}`
  49. }
  50. }
  51. /**
  52. * 判断是否是欧洲地区
  53. */
  54. export const isEuropean = () => {
  55. const userLanguage = navigator.language || 'en-US'
  56. const europeanLocales = ['de', 'fr', 'it', 'es', 'nl', 'pl'] // 例:德语、法语、西班牙语等
  57. return europeanLocales.some((locale) => userLanguage.startsWith(locale))
  58. }
  59. /**
  60. * 根据传入的地区 格式化数字
  61. * @param number - 需要格式化的数字
  62. * @param digits - 小数位数
  63. */
  64. export const formatNumber = (number: number, digits?: number): string => {
  65. if (isNaN(number)) return '0'
  66. const userLanguage = userStore.userInfo?.numbers_format === 'European' ? 'de-DE' : 'zh-CN'
  67. // 设置数字格式化的选项
  68. const options: Intl.NumberFormatOptions = {
  69. style: 'decimal'
  70. // minimumFractionDigits: digits
  71. // maximumFractionDigits: 3
  72. }
  73. digits ?? (options.minimumFractionDigits = digits)
  74. // 其他地区使用默认格式
  75. return new Intl.NumberFormat(userLanguage, options).format(number)
  76. }
  77. /**
  78. * 根据用户地区判断日期格式
  79. * @returns {string} - 返回日期格式
  80. */
  81. export const getDateFormat = () => {
  82. const userLanguage = navigator.language || 'en-US' // 获取浏览器的语言设置
  83. // 判断用户地区
  84. if (userLanguage === 'en-US') {
  85. return 'MM/DD/YYYY' // 美国使用 MM/DD/YYYY 格式
  86. } else if (
  87. userLanguage.startsWith('de') ||
  88. userLanguage.startsWith('fr') ||
  89. userLanguage.startsWith('it') ||
  90. userLanguage.startsWith('es') ||
  91. userLanguage.startsWith('pl') ||
  92. userLanguage.startsWith('nl') ||
  93. userLanguage.startsWith('pt') ||
  94. userLanguage.startsWith('se')
  95. ) {
  96. return 'DD/MM/YYYY' // 其他欧洲国家(例:德语、法语、西班牙语等)使用 DD/MM/YYYY 格式
  97. } else if (['zh-CN', 'ja-JP', 'ko-KR'].includes(userLanguage)) {
  98. return 'YYYY-MM-DD' // 东亚国家(如简体中文、日语、韩语)使用 YYYY-MM-DD 格式
  99. } else {
  100. return 'DD/MM/YYYY' // 其他地区默认 DD/MM/YYYY 格式
  101. }
  102. }
  103. /**
  104. * 返回用户设备是否是Mac系统或者iPhone系统
  105. * @returns {boolean}
  106. */
  107. export const isMacOS = () => {
  108. const userAgent = navigator.userAgent
  109. if (userAgent.includes('iPhone') || userAgent.includes('iPad') || userAgent.includes('Mac')) {
  110. return true
  111. } else {
  112. return false
  113. }
  114. }