554ff64aa7
新增 shop-manage-edit-form.ts(店铺行预填编辑字段,密码掩码不回填需重录),
补充 toShopManageUpdateRequest(不含 createdById),并在 shop-manage-api.ts 增加
submitUpdateShopManage(PUT /api/admin/shop-manages/{id})。
TDD: task-92.test.ts 8 用例先 RED 后 GREEN。
120 lines
4.2 KiB
TypeScript
120 lines
4.2 KiB
TypeScript
/** 店铺新增/编辑共用表单模型(任务 91/92):镜像 Java ShopManageCreateRequest/UpdateRequest 的
|
|
* 必填与长度校验、敏感密码字段处理与 camelCase 请求序列化;纯逻辑。 */
|
|
|
|
export const SHOP_MANAGE_ZN_USERNAME_MAX_LEN = 128
|
|
|
|
/** 店铺表单字段(密码为敏感字段,仅本次提交内存中使用,不写入日志/存储)。 */
|
|
export interface ShopManageFormValues {
|
|
groupId: number | null
|
|
shopName: string
|
|
mallName: string
|
|
znUsername: string
|
|
account: string
|
|
password: string
|
|
}
|
|
|
|
export interface ShopManageFormErrors {
|
|
groupId?: string
|
|
shopName?: string
|
|
mallName?: string
|
|
account?: string
|
|
password?: string
|
|
znUsername?: string
|
|
}
|
|
|
|
/** 发送 POST /api/admin/shop-manages 的请求体(与 Java ShopManageCreateRequest camelCase 对齐)。 */
|
|
export interface ShopManageCreatePayload {
|
|
groupId: number
|
|
shopName: string
|
|
mallName: string
|
|
account: string
|
|
password: string
|
|
createdById: number
|
|
znUsername?: string
|
|
}
|
|
|
|
/** 发送 PUT /api/admin/shop-manages/{id} 的请求体(与 Java ShopManageUpdateRequest camelCase 对齐)。 */
|
|
export interface ShopManageUpdatePayload {
|
|
groupId: number
|
|
shopName: string
|
|
mallName: string
|
|
account: string
|
|
password: string
|
|
znUsername?: string
|
|
}
|
|
|
|
export function emptyShopManageForm(): ShopManageFormValues {
|
|
return { groupId: null, shopName: '', mallName: '', znUsername: '', account: '', password: '' }
|
|
}
|
|
|
|
function positiveGroupId(value: number | null): number | null {
|
|
return typeof value === 'number' && Number.isFinite(value) && value >= 1 ? Math.floor(value) : null
|
|
}
|
|
|
|
function requiredText(value: string, fallback: string): string {
|
|
return (value || '').trim() || fallback
|
|
}
|
|
|
|
function errorText(value: string): string {
|
|
return (value || '').trim() || ''
|
|
}
|
|
|
|
/** 校验表单(镜像后端 NotBlank/NotNull/Size 规则),返回逐字段错误。 */
|
|
export function validateShopManageForm(form: ShopManageFormValues): { valid: boolean; errors: ShopManageFormErrors } {
|
|
const errors: ShopManageFormErrors = {}
|
|
if (positiveGroupId(form.groupId) === null) {
|
|
errors.groupId = '分组不能为空'
|
|
}
|
|
if (!errorText(form.shopName)) errors.shopName = '店铺名称不能为空'
|
|
if (!errorText(form.mallName)) errors.mallName = '商城名称不能为空'
|
|
if (!errorText(form.account)) errors.account = '账号不能为空'
|
|
if (!errorText(form.password)) errors.password = '密码不能为空'
|
|
const znUsername = (form.znUsername || '').trim()
|
|
if (znUsername.length > SHOP_MANAGE_ZN_USERNAME_MAX_LEN) {
|
|
errors.znUsername = `自动化账号长度不能超过${SHOP_MANAGE_ZN_USERNAME_MAX_LEN}个字符`
|
|
}
|
|
return { valid: Object.keys(errors).length === 0, errors }
|
|
}
|
|
|
|
/** 新增/更新共用的必填业务段。 */
|
|
interface ManagePayloadBase {
|
|
groupId: number
|
|
shopName: string
|
|
mallName: string
|
|
account: string
|
|
password: string
|
|
znUsername?: string
|
|
}
|
|
|
|
/** 通用必填段(校验 + 归一);缺有效分组/必填时抛可操作错误(调用前应先校验)。 */
|
|
function requiredBase(form: ShopManageFormValues): ManagePayloadBase {
|
|
const groupId = positiveGroupId(form.groupId)
|
|
if (groupId === null) {
|
|
throw new Error('分组不能为空')
|
|
}
|
|
if (!errorText(form.shopName)) throw new Error('店铺名称不能为空')
|
|
if (!errorText(form.mallName)) throw new Error('商城名称不能为空')
|
|
if (!errorText(form.account)) throw new Error('账号不能为空')
|
|
if (!errorText(form.password)) throw new Error('密码不能为空')
|
|
const payload: ManagePayloadBase = {
|
|
groupId,
|
|
shopName: errorText(form.shopName),
|
|
mallName: errorText(form.mallName),
|
|
account: errorText(form.account),
|
|
password: errorText(form.password),
|
|
}
|
|
const znUsername = (form.znUsername || '').trim()
|
|
if (znUsername) payload.znUsername = znUsername
|
|
return payload
|
|
}
|
|
|
|
/** 新增表单 → 创建请求体(含 createdById)。 */
|
|
export function toShopManageCreateRequest(form: ShopManageFormValues, createdById: number): ShopManageCreatePayload {
|
|
return { ...requiredBase(form), createdById }
|
|
}
|
|
|
|
/** 编辑表单 → 更新请求体(与新增同字段,不含 createdById)。 */
|
|
export function toShopManageUpdateRequest(form: ShopManageFormValues): ShopManageUpdatePayload {
|
|
return requiredBase(form)
|
|
}
|