feat(user): 补充个人信息页接入身份证已存在复用弹框与登录引导

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
17792275749
2026-08-24 11:54:33 +08:00
co-authored by Claude Sonnet 4.6
parent 9911dea32f
commit 90f84cc7df
13 changed files with 353 additions and 72 deletions
+40
View File
@@ -0,0 +1,40 @@
import type { IdCardParsed } from './types'
/** 校验身份证号格式是否合法 */
export const isIdCardValid = (idCard: string): boolean => {
if (idCard.length !== 18) return false
// 前 17 位必须为数字
if (!/^\d{17}$/.test(idCard.slice(0, 17))) return false
// 第 18 位为数字或 X
if (!/^[\dXx]$/.test(idCard[17])) return false
return true
}
/** 解析身份证号,提取性别、年龄、生日 */
export const parseIdCard = (idCard: string): IdCardParsed => {
const empty: IdCardParsed = { gender: '', age: '', birthday: '', sex: 0 }
if (!isIdCardValid(idCard)) return empty
// 第 17 位奇数为男,偶数为女
const sexCode = parseInt(idCard[16], 10)
const sex = sexCode % 2 === 1 ? 1 : 2
// 第 7-14 位为生日 YYYYMMDD
const year = parseInt(idCard.slice(6, 10), 10)
const month = parseInt(idCard.slice(10, 12), 10)
const day = parseInt(idCard.slice(12, 14), 10)
// 计算年龄
const now = new Date()
let age = now.getFullYear() - year
if (now.getMonth() < month - 1 || (now.getMonth() === month - 1 && now.getDate() < day)) {
age--
}
return {
gender: sex === 1 ? '男' : '女',
age: String(age),
birthday: `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`,
sex,
}
}