mobile-mask.js
1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* 手机号脱敏工具(受系统配置 privacyMobileMask 控制)
* 默认明文;开关开启时中间四位打码,如 181****7871。
* 对已含 **** 的展示串再次处理可幂等。
*/
/**
* @param {string} mobile
* @returns {string}
*/
export function maskMobileValue(mobile) {
if (mobile == null || mobile === '') return ''
const m = String(mobile).trim()
if (!m) return ''
if (m.indexOf('*') !== -1) return m
if (m.length < 7) return '****'
return `${m.slice(0, 3)}****${m.slice(-4)}`
}
/**
* 按是否开启脱敏返回展示值
* @param {string} mobile
* @param {boolean|number} enabled 1/true 开启脱敏
* @returns {string}
*/
export function formatMobileDisplay(mobile, enabled) {
const raw = mobile == null ? '' : String(mobile).trim()
if (!raw) return ''
return Number(enabled) === 1 ? maskMobileValue(raw) : raw
}
/**
* 展示用:空值返回 emptyText(默认空串,页面侧可再 `|| '无'`)
* @param {string} mobile
* @param {boolean|number} enabled
* @param {string} [emptyText='']
* @returns {string}
*/
export function displayMobile(mobile, enabled, emptyText = '') {
const v = formatMobileDisplay(mobile, enabled)
return v || emptyText
}
export default {
maskMobileValue,
formatMobileDisplay,
displayMobile
}