index.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import CryptoJS from 'crypto-js'
  2. import service from '@/services'
  3. import moment from 'moment'
  4. /**
  5. * 获取url对象方法
  6. * @param url
  7. * @param base
  8. * @returns
  9. */
  10. export function getUrl(url: string, base?: string) {
  11. const { uploadUrl } = service.config
  12. return new URL(url, base ?? uploadUrl)
  13. }
  14. /**
  15. * 获取文件链接地址
  16. * @param filePath
  17. */
  18. export function getFileUrl(filePath?: string) {
  19. if (filePath) {
  20. const url = getUrl(filePath)
  21. return url.href
  22. }
  23. return ''
  24. }
  25. /**
  26. * 处理价格颜色的显示
  27. */
  28. export function handlePriceColor(curValue: number, preValue: number) {
  29. if (!curValue || curValue === preValue) {
  30. return ''
  31. } else if (curValue > preValue) {
  32. return 'g-color--up'
  33. } else {
  34. return 'g-color--down'
  35. }
  36. }
  37. /**
  38. * 处理空值的情况
  39. * @param value 值
  40. * @param suffix 后缀名
  41. * @returns
  42. */
  43. export function handleNoneValue<T>(value?: T, suffix = '') {
  44. if (value == null || String(value) === '') {
  45. return '--'
  46. }
  47. return value + suffix
  48. }
  49. /**
  50. * 处理0值的情况
  51. * @param value 值
  52. * @param suffix 后缀名
  53. * @returns
  54. */
  55. export function handleNumberValue(value?: number | string, suffix = '') {
  56. try {
  57. if (value === undefined || Number(value) === 0) {
  58. return '--'
  59. }
  60. return value + suffix
  61. }
  62. catch {
  63. return '--'
  64. }
  65. }
  66. /**
  67. * 数值转换百分比
  68. * @param value
  69. * @param decimal 保留小数位
  70. */
  71. export function parsePercent(value: number, decimal = 2) {
  72. const val = formatDecimal(value * 100, decimal, false)
  73. return handleNumberValue(val, '%')
  74. }
  75. /**
  76. * 强制保留小数位,不足位数自动补零
  77. * @param value
  78. * @param decimal 保留小数位
  79. * @param round 是否四舍五入
  80. * @returns
  81. */
  82. export function formatDecimal(value: number | string, decimal = 2, round = true) {
  83. const val = Number(value)
  84. if (decimal <= 0) {
  85. if (round) {
  86. return Math.round(val).toString()
  87. }
  88. return Math.trunc(val).toString()
  89. } else {
  90. if (round) {
  91. return val.toFixed(decimal)
  92. }
  93. let num = val.toString()
  94. const index = num.indexOf('.')
  95. if (index !== -1) {
  96. num = num.substring(0, decimal + index + 1)
  97. } else {
  98. num = num.substring(0)
  99. }
  100. return parseFloat(num).toFixed(decimal)
  101. }
  102. }
  103. /**
  104. * 数字转千位符金额
  105. * @param value 金额数值
  106. * @param decimal 保留小数位 默认2
  107. */
  108. export function formatAmount(value: number, decimal = 2) {
  109. const reg = new RegExp('(\\d)(?=(\\d{3})+$)', 'ig')
  110. const result = value.toFixed(decimal).toString().split('.')
  111. result[0] = result[0].replace(reg, '$1,')
  112. return result.join('.')
  113. }
  114. /**
  115. * 日期格式化
  116. * @param value
  117. * @param format
  118. * @returns
  119. */
  120. export function formatDate(value?: string, format = 'YYYY-MM-DD HH:mm:ss') {
  121. if (value) {
  122. return moment(value).format(format)
  123. }
  124. return handleNoneValue()
  125. }
  126. /**
  127. * Des密钥
  128. * 手机发送验证码时加密方法
  129. * @param mobile
  130. */
  131. export function encryptDesMobile(mobile: string) {
  132. const hexKey = CryptoJS.enc.Utf8.parse('Ub$kl^P3')
  133. const hexData = CryptoJS.enc.Utf8.parse(mobile)
  134. const desData = CryptoJS.DES.encrypt(hexData, hexKey, { iv: hexKey })
  135. const encrypted = CryptoJS.enc.Base64.stringify(desData.ciphertext)
  136. return encrypted
  137. }
  138. /**
  139. * 获取加密后的手机号码
  140. * @param mobile 手机号码
  141. * @returns 加密后的手机号码
  142. */
  143. export function getEncryptMobile(mobile: string) {
  144. const encryptMobile = encryptDesMobile(mobile)
  145. return encryptMobile.replace(new RegExp('\\+', 'g'), '*').replace(new RegExp('\\/', 'g'), '-').replace(new RegExp('\\=', 'g'), '.')
  146. }