1.字符串加密(姓名、手机号、地址)
export function encryptionStr(data, type, isEncryption) {
if (!data) {
return ''
}
if (!isEncryption || typeof data !== 'string') {
return data
}
if (type === 'name') {
return data.substr(0, 1) + data.substr(1).replace(/[\s\S]/g, '*')
}
if (type === 'address') {
return data.replace(/[A-Za-z0-9]/g, '*')
}
if (type === 'phone') {
const start = data.substr(0, 3)
const end = data.substr(-4)
return start + '****' + end
}
}
2.千位数逢三断一
function numFormat(value) {
try {
if (!value) return '0'
value = value + ''
var intPart = Number(value).toFixed(0) // 获取整数部分
var intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 将整数部分逢三一断
var floatPart = '.00' // 预定义小数部分
var value2Array = value.split('.')
//= 2表示数据有小数位
if (value2Array.length === 2) {
floatPart = value2Array[1].toString() // 拿到小数部分
if (floatPart.length === 1) {
// 补0,实际上用不着
floatPart = '.' + floatPart + '0'
} else {
floatPart = '.' + floatPart
}
}
floatPart = floatPart === '.00' ? '' : floatPart
return intPartFormat + floatPart
} catch (error) {
return value
}
}