38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
/** 手机号展示脱敏(中间四位 ****) */
|
|
|
|
export function maskPhone(phone) {
|
|
if (phone == null || phone === '') return ''
|
|
|
|
const str = String(phone).trim()
|
|
if (str.includes('****')) return str
|
|
|
|
const digits = str.replace(/\D/g, '')
|
|
if (digits.length === 11) {
|
|
return `${digits.slice(0, 3)}****${digits.slice(7)}`
|
|
}
|
|
if (digits.length > 4) {
|
|
const hideLen = Math.min(4, digits.length - 3)
|
|
const start = Math.floor((digits.length - hideLen) / 2)
|
|
return `${digits.slice(0, start)}${'*'.repeat(hideLen)}${digits.slice(start + hideLen)}`
|
|
}
|
|
|
|
return str
|
|
}
|
|
|
|
/** 个人中心头部:138****6789 已绑定微信 */
|
|
export function formatMemberCenterPhone(phone) {
|
|
const masked = maskPhone(phone)
|
|
return masked ? `${masked} 已绑定微信` : ''
|
|
}
|
|
|
|
/** 保存前规范化:尽量存 11 位数字;已是脱敏串则原样保留 */
|
|
export function normalizePhoneForStore(phone) {
|
|
const str = String(phone || '').trim()
|
|
if (!str) return ''
|
|
if (str.includes('****')) return str
|
|
|
|
const digits = str.replace(/\D/g, '')
|
|
if (digits.length >= 11) return digits.slice(0, 11)
|
|
return digits || str
|
|
}
|