feat(user): 补充个人信息页接入身份证已存在复用弹框与登录引导
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
9911dea32f
commit
90f84cc7df
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
/** 身份证解析结果 */
|
||||
export interface IdCardParsed {
|
||||
/** 性别文本(男/女),解析失败返回空字符串 */
|
||||
gender: string
|
||||
/** 年龄文本,解析失败返回空字符串 */
|
||||
age: string
|
||||
/** 生日 YYYY-MM-DD */
|
||||
birthday: string
|
||||
/** 性别 1=男 2=女 */
|
||||
sex: number
|
||||
}
|
||||
Reference in New Issue
Block a user