c4040263
“wangming”
feat: V2.0.2 会员标签...
|
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
/**
* 员工入职扫码填报 - 表单校验与身份证派生信息。
* 从 绿纤uni-app/pages/employee-onboard/employee-onboard.vue 移植,
* 规则需与后端 NCC.Extend/EmployeeProfileSensitiveHelper.cs 保持一致。
*/
/** 性别选项(后端未提供枚举下拉,固定两项) */
export const GENDER_OPTIONS = [
{ value: 1, label: '男' },
{ value: 2, label: '女' }
]
const FULL_ADDRESS_DETAIL_KEYWORDS = [
'区', '县', '镇', '乡', '村', '路', '街', '道', '号', '弄', '栋', '单元', '室', '层', '楼', '组', '委', '队',
'巷', '里', '苑', '园', '小区', '社区', '大厦'
]
export function normalizeIdCard(idCard) {
return (idCard || '').trim().toUpperCase()
}
export function normalizeMobile(mobile) {
return (mobile || '').trim()
}
/** 从身份证号解析出生日期(yyyy-MM-dd),无法解析返回空字符串 */
export function parseBirthdayFromIdCard(idCard) {
const normalized = normalizeIdCard(idCard)
if (!normalized) return ''
if (normalized.length === 18) {
const part = normalized.substring(6, 14)
if (/^\d{8}$/.test(part)) {
return `${part.substring(0, 4)}-${part.substring(4, 6)}-${part.substring(6, 8)}`
}
}
if (normalized.length === 15) {
const part15 = normalized.substring(6, 12)
if (/^\d{6}$/.test(part15)) {
return `19${part15.substring(0, 2)}-${part15.substring(2, 4)}-${part15.substring(4, 6)}`
}
}
return ''
}
/** 从身份证号解析性别(1男/2女),无法解析返回 null */
export function parseGenderFromIdCard(idCard) {
const normalized = normalizeIdCard(idCard)
if (!normalized) return null
const index = normalized.length === 18 ? 16 : normalized.length === 15 ? 14 : -1
if (index < 0) return null
const code = parseInt(normalized.charAt(index), 10)
if (Number.isNaN(code)) return null
return code % 2 === 1 ? 1 : 2
}
/** 根据出生日期(yyyy-MM-dd)计算周岁 */
export function calcAge(birthdayStr) {
if (!birthdayStr) return null
const parts = birthdayStr.split('-')
if (parts.length !== 3) return null
const birth = new Date(parseInt(parts[0], 10), parseInt(parts[1], 10) - 1, parseInt(parts[2], 10))
const today = new Date()
let age = today.getFullYear() - birth.getFullYear()
const m = today.getMonth() - birth.getMonth()
if (m < 0 || (m === 0 && today.getDate() < birth.getDate())) {
age--
}
return age < 0 ? null : age
}
/** 18/15位身份证号合法性校验(含18位校验码) */
export function isValidIdCard(idCard) {
const normalized = normalizeIdCard(idCard)
if (!normalized) return false
if (normalized.length === 18) {
if (!/^\d{17}[\dX]$/.test(normalized)) return false
const weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
const codes = '10X98765432'
let sum = 0
for (let i = 0; i < 17; i++) {
sum += parseInt(normalized.charAt(i), 10) * weights[i]
}
return codes.charAt(sum % 11) === normalized.charAt(17)
}
if (normalized.length === 15) {
return /^\d{15}$/.test(normalized)
}
return false
}
export function isValidMobile(mobile) {
return /^1\d{10}$/.test(normalizeMobile(mobile))
}
export function isValidBankCard(bankCard) {
if (!bankCard || !bankCard.trim()) return false
const normalized = bankCard.replace(/\s+/g, '')
return normalized.length >= 12 && normalized.length <= 30 && /^\d+$/.test(normalized)
}
/** 身份证地址须为完整住址(长度>=8 且包含区/县/街/路/号等关键字) */
export function isValidFullAddress(address) {
const trimmed = (address || '').trim()
if (!trimmed || trimmed.length < 8) return false
return FULL_ADDRESS_DETAIL_KEYWORDS.some(keyword => trimmed.indexOf(keyword) !== -1)
}
export function fullAddressErrorMessage() {
return '身份证地址须填写完整住址(含区/县/街道/门牌等,至少8个字)'
}
|