index.ts 3.9 KB

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