
function getIdentityInfo(idCard) {
if (!idCard || idCard.length !== 18) {
return {
age: null,
gender: null,
birthDate: null
};
}
const birthYear = parseInt(idCard.substring(6, 10), 10);
const birthMonth = parseInt(idCard.substring(10, 12), 10);
const birthDay = parseInt(idCard.substring(12, 14), 10);
const birthDate = new Date(birthYear, birthMonth - 1, birthDay);
if (
birthDate.getFullYear() !== birthYear ||
birthDate.getMonth() !== birthMonth - 1 ||
birthDate.getDate() !== birthDay
) {
return {
age: null,
gender: null,
birthDate: null
};
}
const today = new Date();
let age = today.getFullYear() - birthYear;
if (today.getMonth() < birthMonth - 1 || (today.getMonth() === birthMonth - 1 && today.getDate() < birthDay)) {
age--;
}
const genderCode = parseInt(idCard.substring(16, 17), 10);
const gender = genderCode % 2 === 1 ? '男' : '女';
const formattedBirthDate = `${birthYear}-${birthMonth.toString().padStart(2, '0')}-${birthDay.toString().padStart(2, '0')}`;
return {
age: age,
gender: gender,
birthDate: formattedBirthDate
};
}
const idCard =this.formModel.d10;
const identityInfo = getIdentityInfo(idCard);
this.formModel.d13 = identityInfo.age;
this.formModel.d9 = identityInfo.gender;
this.formModel.d12 = identityInfo.birthDate;