|
|
@@ -28,9 +28,9 @@ type UpdateUserRequest = InferRequestType<typeof userClient[':id']['$put']>['jso
|
|
|
const userFormSchema = z.object({
|
|
|
username: z.string().min(3, '用户名至少3个字符'),
|
|
|
nickname: z.string().optional(),
|
|
|
- email: z.string().email('请输入有效的邮箱地址').optional().or(z.literal('')),
|
|
|
- phone: z.string().regex(/^1[3-9]\d{9}$/, '请输入有效的手机号').optional().or(z.literal('')),
|
|
|
- name: z.string().optional(),
|
|
|
+ email: z.email('请输入有效的邮箱地址').optional().or(z.literal('')).transform(val => val || null),
|
|
|
+ phone: z.string().regex(/^1[3-9]\d{9}$/, '请输入有效的手机号').optional().or(z.literal('')).transform(val => val || null),
|
|
|
+ name: z.string().optional().or(z.literal('')).transform(val => val || null),
|
|
|
password: z.string().min(6, '密码至少6个字符').optional(),
|
|
|
isDisabled: z.boolean().default(false),
|
|
|
});
|
|
|
@@ -124,14 +124,16 @@ export const UsersPage = () => {
|
|
|
// 处理表单提交
|
|
|
const handleSubmit = async (data: UserFormData) => {
|
|
|
try {
|
|
|
+ const submitData = {
|
|
|
+ ...data,
|
|
|
+ isDisabled: data.isDisabled ? 1 : 0,
|
|
|
+ };
|
|
|
+
|
|
|
if (editingUser) {
|
|
|
// 编辑用户
|
|
|
const res = await userClient[':id']['$put']({
|
|
|
param: { id: editingUser.id },
|
|
|
- json: {
|
|
|
- ...data,
|
|
|
- isDisabled: data.isDisabled ? 1 : 0,
|
|
|
- } as UpdateUserRequest
|
|
|
+ json: submitData as UpdateUserRequest
|
|
|
});
|
|
|
if (res.status !== 200) {
|
|
|
throw new Error('更新用户失败');
|
|
|
@@ -140,10 +142,7 @@ export const UsersPage = () => {
|
|
|
} else {
|
|
|
// 创建用户
|
|
|
const res = await userClient.$post({
|
|
|
- json: {
|
|
|
- ...data,
|
|
|
- isDisabled: data.isDisabled ? 1 : 0,
|
|
|
- } as CreateUserRequest
|
|
|
+ json: submitData as CreateUserRequest
|
|
|
});
|
|
|
if (res.status !== 201) {
|
|
|
throw new Error('创建用户失败');
|