diff --git a/src/views/face/faceData/customDialog.vue b/src/views/face/faceData/customDialog.vue index 699bcd17..5c730791 100644 --- a/src/views/face/faceData/customDialog.vue +++ b/src/views/face/faceData/customDialog.vue @@ -97,7 +97,8 @@ export default { { required: true, message: '名称不能为空', trigger: 'blur' } ], idCard: [ - { required: true, message: '身份证号不能为空', trigger: 'blur' } + { required: true, message: '身份证号不能为空', trigger: 'blur' }, + { validator: this.validateIdCard, trigger: 'blur' } // 添加自定义校验 ] }, file: null // 用于存储选中的文件 @@ -113,6 +114,30 @@ export default { } }, methods: { + validateIdCard(rule, value, callback) { + if (!value) { + return callback(new Error('身份证号不能为空')) + } + const idCardPattern = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/ + if (!idCardPattern.test(value)) { + return callback(new Error('身份证号格式不正确')) + } + // 验证最后一位校验码 + if (!this.checkIdCardCode(value)) { + return callback(new Error('身份证校验码错误')) + } + callback() // 校验通过 + }, + checkIdCardCode(idCard) { + const factors = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] + const parity = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'] + const idCardArray = idCard.split('') + const sum = idCardArray + .slice(0, 17) + .reduce((acc, cur, idx) => acc + cur * factors[idx], 0) + const lastChar = idCardArray[17].toUpperCase() // 最后一位可能是小写 x + return parity[sum % 11] === lastChar + }, triggerFileInput() { this.$refs.fileInput.click() // 触发文件选择框 }, @@ -142,7 +167,7 @@ export default { // 添加表单数据 formData.append('name', this.form.name) formData.append('idCard', this.form.idCard) - formData.append('otherInfo', this.form.otherInfo) + formData.append('otherInfo', this.form.otherInfo || '') formData.append('groupCode', this.groupCode) // 根据是否是更新操作来调用不同的 API diff --git a/src/views/face/faceData/index.vue b/src/views/face/faceData/index.vue index 3491e28e..57dd82e9 100644 --- a/src/views/face/faceData/index.vue +++ b/src/views/face/faceData/index.vue @@ -66,7 +66,8 @@ icon="el-icon-close" size="mini" @click="handleClose" - >关闭 + >关闭 + @@ -76,7 +77,11 @@ - + + +