| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- import CryptoJS from 'crypto-js'
- import service from '@/services'
- import moment from 'moment'
- /**
- * 获取url对象方法
- * @param url
- * @param base
- * @returns
- */
- export function getUrl(url: string, base?: string) {
- const { uploadUrl } = service.config
- return new URL(url, base ?? uploadUrl)
- }
- /**
- * 获取图片地址
- * @param fileUrl
- */
- export function getImageUrl(fileUrl: string) {
- if (fileUrl) {
- const url = getUrl(fileUrl)
- return url.href
- }
- return ''
- }
- /**
- * 格式化html字符串
- * @param text
- * @returns
- */
- export function formatHtmlString(text: string) {
- const html = text.replace(/[\n\r]/g, '<br />')
- return html.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, (match, capture) => {
- // 替换img标签src地址为绝对路径
- const url = getUrl(capture)
- return `<img src="${url.href}" alt=""/>`
- })
- }
- /**
- * 处理价格颜色的显示
- */
- export function handlePriceColor(curValue: number, preValue: number) {
- if (!curValue || curValue === preValue) {
- return ''
- } else if (curValue > preValue) {
- return 'g-color--up'
- } else {
- return 'g-color--down'
- }
- }
- /**
- * 处理空值的情况
- * @param value 值
- * @param suffix 后缀名
- * @returns
- */
- export function handleNoneValue<T>(value: T, suffix = '') {
- if (value == null || String(value) === '') {
- return '--'
- }
- return value + suffix
- }
- /**
- * 处理0值的情况
- * @param value 值
- * @param suffix 后缀名
- * @returns
- */
- export function handleNumberValue(value: number, suffix = '') {
- if (value === 0) {
- return '--'
- }
- return value + suffix
- }
- /**
- * 强制保留小数位,不足位数自动补零
- * @param value
- * @param decimal 保留小数位
- * @param round 是否四舍五入
- * @returns
- */
- export function formatDecimal(value: number | string, decimal = 2, round = true) {
- const val = Number(value)
- if (decimal <= 0) {
- if (round) {
- return Math.round(val).toString()
- }
- return Math.trunc(val).toString()
- } else {
- if (round) {
- return val.toFixed(decimal)
- }
- let num = val.toString()
- const index = num.indexOf('.')
- if (index < 0) {
- // 小数位自动补零
- if (decimal > 0) {
- const count = num.length
- num += '.'
- while (num.length <= count + decimal) {
- num += '0'
- }
- }
- }
- return num.substring(0, decimal + index + 1)
- }
- }
- /**
- * 数字转千位符金额
- * @param value 金额数值
- * @param decimal 保留小数位 默认2
- */
- export function formatAmount(value: number, decimal = 2) {
- const reg = new RegExp('(\\d)(?=(\\d{3})+$)', 'ig')
- const result = value.toFixed(decimal).toString().split('.')
- result[0] = result[0].replace(reg, '$1,')
- return result.join('.')
- }
- /**
- * 日期格式化
- * @param date
- * @param format
- * @returns
- */
- export function formatDate(date?: string, format = 'YYYY-MM-DD HH:mm:ss') {
- if (date) {
- return moment(date).format(format)
- }
- return moment(new Date()).format(format)
- }
- /**
- * Des密钥
- * 手机发送验证码时加密方法
- * @param mobile
- */
- export function encryptDesMobile(mobile: string) {
- const hexKey = CryptoJS.enc.Utf8.parse('Ub$kl^P3')
- const hexData = CryptoJS.enc.Utf8.parse(mobile)
- const desData = CryptoJS.DES.encrypt(hexData, hexKey, { iv: hexKey })
- const encrypted = CryptoJS.enc.Base64.stringify(desData.ciphertext)
- return encrypted
- }
- /**
- * 获取加密后的手机号码
- * @param mobile 手机号码
- * @returns 加密后的手机号码
- */
- export function getEncryptMobile(mobile: string) {
- const encryptMobile = encryptDesMobile(mobile)
- return encryptMobile.replace(new RegExp('\\+', 'g'), '*').replace(new RegExp('\\/', 'g'), '-').replace(new RegExp('\\=', 'g'), '.')
- }
|