feat: 完成前端业务闭环与后端联调
This commit is contained in:
@@ -16,6 +16,15 @@ export const PROFILE_SEX_OPTIONS = Object.freeze([
|
||||
|
||||
const profileSexValues = new Set(PROFILE_SEX_OPTIONS.map(({ value }) => value))
|
||||
|
||||
const isCalendarDate = (value) => {
|
||||
if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) return false
|
||||
const [year, month, day] = value.split('-').map(Number)
|
||||
const date = new Date(Date.UTC(year, month - 1, day))
|
||||
return date.getUTCFullYear() === year &&
|
||||
date.getUTCMonth() === month - 1 &&
|
||||
date.getUTCDate() === day
|
||||
}
|
||||
|
||||
const normalizeProfileResponseText = (value, field) => {
|
||||
if (value === undefined || value === null) return ''
|
||||
if (typeof value !== 'string') {
|
||||
@@ -25,11 +34,18 @@ const normalizeProfileResponseText = (value, field) => {
|
||||
}
|
||||
|
||||
export const normalizePhoneChangePayload = (payload) => {
|
||||
assertPlainPayload(payload, new Set(['phone', 'smsCode']), '换绑手机号请求')
|
||||
assertPlainPayload(payload, new Set(['phone', 'smsCode', 'currentPasswordHash']), '换绑手机号请求')
|
||||
if (typeof payload.phone !== 'string' || !/^1\d{10}$/.test(payload.phone.trim())) {
|
||||
throw new TypeError('新手机号格式无效')
|
||||
}
|
||||
return { phone: payload.phone.trim(), smsCode: assertSmsCode(payload.smsCode) }
|
||||
if (typeof payload.currentPasswordHash !== 'string' || !/^[a-f0-9]{32}$/i.test(payload.currentPasswordHash)) {
|
||||
throw new TypeError('当前密码摘要无效')
|
||||
}
|
||||
return {
|
||||
phone: payload.phone.trim(),
|
||||
smsCode: assertSmsCode(payload.smsCode),
|
||||
currentPassword: payload.currentPasswordHash.toLowerCase()
|
||||
}
|
||||
}
|
||||
|
||||
const normalizeProfileUserId = (value) => {
|
||||
@@ -43,6 +59,10 @@ export const normalizeAppProfile = (payload) => {
|
||||
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
|
||||
throw createRequestError('个人资料响应格式无效', 'PROFILE_RESPONSE_INVALID')
|
||||
}
|
||||
const birthday = normalizeProfileResponseText(payload.birthday, 'birthday')
|
||||
if (birthday && !isCalendarDate(birthday.slice(0, 10))) {
|
||||
throw createRequestError('个人资料字段 birthday 无效', 'PROFILE_RESPONSE_INVALID')
|
||||
}
|
||||
return {
|
||||
userId: normalizeProfileUserId(payload.userId),
|
||||
tenantId: normalizeProfileResponseText(payload.tenantId, 'tenantId'),
|
||||
@@ -56,7 +76,7 @@ export const normalizeAppProfile = (payload) => {
|
||||
'PROFILE_RESPONSE_INVALID'
|
||||
),
|
||||
sex: normalizeProfileResponseText(payload.sex, 'sex'),
|
||||
birthday: normalizeProfileResponseText(payload.birthday, 'birthday'),
|
||||
birthday,
|
||||
email: normalizeProfileResponseText(payload.email, 'email'),
|
||||
registerSource: normalizeProfileResponseText(payload.registerSource, 'registerSource'),
|
||||
status: normalizeProfileResponseText(payload.status, 'status')
|
||||
@@ -84,7 +104,7 @@ export const normalizeProfileUpdatePayload = (payload) => {
|
||||
if (Object.prototype.hasOwnProperty.call(payload, 'birthday')) {
|
||||
const birthday = normalizeOptionalText(payload.birthday, 'birthday')
|
||||
if (birthday) {
|
||||
if (!/^\d{4}-\d{2}-\d{2}$/.test(birthday)) {
|
||||
if (!isCalendarDate(birthday)) {
|
||||
throw new TypeError('birthday 必须是 yyyy-MM-dd 日期')
|
||||
}
|
||||
normalizedPayload.birthday = birthday
|
||||
|
||||
Reference in New Issue
Block a user