Blame view

antis-ncc-admin/src/views/public/utils/employee-onboard-validators.js 6.18 KB
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个字)'
  }
  
7fc03b57   “wangming”   1
112
113
114
115
116
117
118
119
120
121
122
123
124
  /** 校验身份证有效期:起始必填;非长期时截止必填且不得早于起始 */
  export function validateCertificatesValidity(form) {
    const f = form || {}
    if (!f.certificatesValidStart) return '请选择身份证有效期起始日期'
    if (Number(f.certificatesValidLongTerm) !== 1) {
      if (!f.certificatesValidEnd) return '请选择身份证有效期截止日期'
      if (f.certificatesValidEnd < f.certificatesValidStart) {
        return '身份证有效期截止日期不能早于起始日期'
      }
    }
    return ''
  }
  
c4040263   “wangming”   feat: V2.0.2 会员标签...
125
126
127
128
129
130
131
132
133
134
135
136
  /**
   * 校验员工入职表单,返回首个错误提示;全部通过返回空字符串。
   * 字段与顺序与后端 LqEmployeeProfileSubmissionService 的公开校验保持一致。
   * @param {object} form
   * @returns {string}
   */
  export function validateEmployeeOnboardForm(form) {
    const f = form || {}
    if (!f.realName || !f.realName.trim()) return '请输入姓名'
    if (!isValidMobile(f.mobilePhone)) return '请输入正确的手机号'
    if (!f.gender) return '请选择性别'
    if (!isValidIdCard(f.idCardNo)) return '身份证号不合法'
7fc03b57   “wangming”   1
137
138
    const certValidErr = validateCertificatesValidity(f)
    if (certValidErr) return certValidErr
c4040263   “wangming”   feat: V2.0.2 会员标签...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    if (!f.idCardAddress || !f.idCardAddress.trim()) return '请填写身份证地址'
    if (!isValidFullAddress(f.idCardAddress)) return fullAddressErrorMessage()
    if (!f.currentAddress || !f.currentAddress.trim()) return '请填写现居住地址'
    if (!f.bankName || !f.bankName.trim()) return '请填写开户行'
    if (!f.bankAccountName || !f.bankAccountName.trim()) return '请填写开户实名'
    if (!isValidBankCard(f.bankCardNo)) return '请输入正确的银行卡号'
    if (!f.idCardFrontUrl) return '请上传身份证正面照片'
    if (!f.idCardBackUrl) return '请上传身份证反面照片'
    if (!f.urgentContacts || !f.urgentContacts.trim()) return '请输入紧急联系人'
    if (!isValidMobile(f.urgentTelePhone)) return '请输入正确的紧急联系人电话'
    if (normalizeMobile(f.urgentTelePhone) === normalizeMobile(f.mobilePhone)) {
      return '紧急联系人电话不能与员工手机号一致'
    }
    if (f.enrollmentDate && f.graduationDate && f.graduationDate < f.enrollmentDate) {
      return '毕业时间不能早于入学时间'
    }
    if (f.previousStartDate && f.previousEndDate && f.previousEndDate < f.previousStartDate) {
      return '前公司结束时间不能早于开始时间'
    }
    return ''
  }