index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // 立即执行计时器
  2. import accounting from 'accounting'
  3. const setIntervalImmediately = (fn, duration) => setInterval((() => (fn(), fn))(), duration)
  4. async function needLogin() {
  5. const route = useRoute()
  6. const useAuth = useAuthStore()
  7. const { token } = storeToRefs(useAuth)
  8. if (!token.value) {
  9. await navigateTo({
  10. path: '/login',
  11. query: {
  12. redirect: route.fullPath,
  13. ...route.query
  14. },
  15. replace: true
  16. })
  17. return
  18. }
  19. }
  20. function jumpToLoginPage() {
  21. const route = useRoute()
  22. navigateTo({
  23. path: '/login',
  24. query: {
  25. redirect: route.path,
  26. ...route.query
  27. },
  28. replace: true
  29. })
  30. }
  31. function formatImgSrc(srcArr) {
  32. if (Array.isArray(srcArr) && srcArr.length > 0) {
  33. return srcArr[0] ?? ''
  34. }
  35. return ''
  36. }
  37. function priceToArray(price) {
  38. if (!price) return ['0', '00']
  39. const priceStr = price.toString()
  40. if (!priceStr.includes('.')) {
  41. return [priceStr, '00']
  42. }
  43. return [priceStr.split('.')[0], priceStr.split('.')[1]]
  44. }
  45. function isEmail(email) {
  46. if (!email) return false
  47. const value = email.trim()
  48. if (
  49. /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(
  50. value
  51. )
  52. ) {
  53. return true
  54. }
  55. return false
  56. }
  57. const isEmptyValue = (value) => {
  58. if (value == null) return true
  59. if (Array.isArray(value) && value.length === 0) return true
  60. if (typeof value === 'object' && Object.keys(value).length === 0) return true
  61. return false
  62. }
  63. const formatNumber = (n) => {
  64. let num = Number(n ?? '')
  65. // 如果数字大于或等于10,000万,则显示为9999w+
  66. if (num >= 10000 * 10000) {
  67. return '9999万+'
  68. }
  69. // 如果数字大于或等于1万,则转换为以“万”为单位,不四舍五入,最多保留1位小数
  70. else if (num >= 10000) {
  71. let w = Math.floor(num / 10000) // 取整,避免四舍五入
  72. let remainder = num % 10000
  73. // 计算小数部分并限制为最多两位
  74. let decimalPart = ''
  75. if (remainder > 0) {
  76. // 将余数转换为1位小数,但不进行四舍五入
  77. decimalPart = ('.' + Math.floor(remainder / 100)).slice(0, 2)
  78. // 移除结尾的0,如果有的话
  79. decimalPart = decimalPart.replace(/\.?0+$/, '')
  80. }
  81. return `${w}${decimalPart}万`
  82. }
  83. // 对于小于1万的数字直接输出
  84. else {
  85. // return accounting.formatNumber(num)
  86. }
  87. }
  88. const transferCount = (num = 0) => {
  89. if (isNaN(num)) return 0
  90. if (num > 10000) {
  91. return (num / 10000).toFixed(1) + 'w'
  92. } else {
  93. return num
  94. }
  95. }
  96. const transferCountChinese = (num = 0) => {
  97. if (isNaN(num)) return 0
  98. if (num > 10000) {
  99. return (num / 10000).toFixed(1) + '万'
  100. } else {
  101. return num
  102. }
  103. }
  104. export {
  105. setIntervalImmediately,
  106. needLogin,
  107. jumpToLoginPage,
  108. formatImgSrc,
  109. isEmail,
  110. formatNumber,
  111. isEmptyValue,
  112. priceToArray,
  113. transferCount,
  114. transferCountChinese
  115. }