|
|
@@ -17,6 +17,7 @@ import { z } from 'zod';
|
|
|
import { toast } from 'sonner';
|
|
|
import { Skeleton } from '@/client/components/ui/skeleton';
|
|
|
import { Switch } from '@/client/components/ui/switch';
|
|
|
+import { DisabledStatus } from '@/share/types';
|
|
|
|
|
|
// 使用RPC方式提取类型
|
|
|
type CreateUserRequest = InferRequestType<typeof userClient.$post>['json'];
|
|
|
@@ -26,27 +27,27 @@ type UserResponse = InferResponseType<typeof userClient.$get, 200>['data'][0];
|
|
|
// 创建用户表单Schema - 与后端CreateUserSchema保持一致
|
|
|
const createUserFormSchema = z.object({
|
|
|
username: z.string().min(3, '用户名至少3个字符'),
|
|
|
- nickname: z.string().optional(),
|
|
|
- email: z.string().email('请输入有效的邮箱地址').optional().transform(val => val === '' ? null : val),
|
|
|
- phone: z.string().regex(/^1[3-9]\d{9}$/, '请输入有效的手机号').optional().transform(val => val === '' ? null : val),
|
|
|
- name: z.string().optional().transform(val => val === '' ? null : val),
|
|
|
+ nickname: z.string().nullable().optional(),
|
|
|
+ email: z.string().email('请输入有效的邮箱地址').nullable().optional(),
|
|
|
+ phone: z.string().regex(/^1[3-9]\d{9}$/, '请输入有效的手机号').nullable().optional(),
|
|
|
+ name: z.string().nullable().optional(),
|
|
|
password: z.string().min(6, '密码至少6个字符'),
|
|
|
- isDisabled: z.boolean().default(false),
|
|
|
+ isDisabled: z.number().int().min(0).max(1).default(DisabledStatus.ENABLED),
|
|
|
});
|
|
|
|
|
|
// 更新用户表单Schema - 与后端UpdateUserSchema保持一致
|
|
|
const updateUserFormSchema = z.object({
|
|
|
username: z.string().min(3, '用户名至少3个字符').optional(),
|
|
|
- nickname: z.string().optional(),
|
|
|
- email: z.string().email('请输入有效的邮箱地址').optional().transform(val => val === '' ? null : val),
|
|
|
- phone: z.string().regex(/^1[3-9]\d{9}$/, '请输入有效的手机号').optional().transform(val => val === '' ? null : val),
|
|
|
- name: z.string().optional().transform(val => val === '' ? null : val),
|
|
|
+ nickname: z.string().nullable().optional(),
|
|
|
+ email: z.string().email('请输入有效的邮箱地址').nullable().optional(),
|
|
|
+ phone: z.string().regex(/^1[3-9]\d{9}$/, '请输入有效的手机号').nullable().optional(),
|
|
|
+ name: z.string().nullable().optional(),
|
|
|
password: z.string().min(6, '密码至少6个字符').optional(),
|
|
|
- isDisabled: z.boolean().optional(),
|
|
|
+ isDisabled: z.number().int().min(0).max(1).default(DisabledStatus.ENABLED),
|
|
|
});
|
|
|
|
|
|
-type CreateUserFormData = z.infer<typeof createUserFormSchema>;
|
|
|
-type UpdateUserFormData = z.infer<typeof updateUserFormSchema>;
|
|
|
+type CreateUserFormData = CreateUserRequest;
|
|
|
+type UpdateUserFormData = UpdateUserRequest;
|
|
|
|
|
|
export const UsersPage = () => {
|
|
|
const [searchParams, setSearchParams] = useState({
|
|
|
@@ -65,25 +66,25 @@ export const UsersPage = () => {
|
|
|
resolver: zodResolver(createUserFormSchema),
|
|
|
defaultValues: {
|
|
|
username: '',
|
|
|
- nickname: '',
|
|
|
- email: '',
|
|
|
- phone: '',
|
|
|
- name: '',
|
|
|
+ nickname: undefined,
|
|
|
+ email: null,
|
|
|
+ phone: null,
|
|
|
+ name: null,
|
|
|
password: '',
|
|
|
- isDisabled: false,
|
|
|
+ isDisabled: DisabledStatus.ENABLED,
|
|
|
},
|
|
|
});
|
|
|
|
|
|
const updateForm = useForm<UpdateUserFormData>({
|
|
|
resolver: zodResolver(updateUserFormSchema),
|
|
|
defaultValues: {
|
|
|
- username: '',
|
|
|
- nickname: '',
|
|
|
- email: '',
|
|
|
- phone: '',
|
|
|
- name: '',
|
|
|
- password: '',
|
|
|
- isDisabled: false,
|
|
|
+ username: undefined,
|
|
|
+ nickname: undefined,
|
|
|
+ email: null,
|
|
|
+ phone: null,
|
|
|
+ name: null,
|
|
|
+ password: undefined,
|
|
|
+ isDisabled: undefined,
|
|
|
},
|
|
|
});
|
|
|
|
|
|
@@ -124,12 +125,12 @@ export const UsersPage = () => {
|
|
|
setIsCreateForm(true);
|
|
|
createForm.reset({
|
|
|
username: '',
|
|
|
- nickname: '',
|
|
|
- email: '',
|
|
|
- phone: '',
|
|
|
- name: '',
|
|
|
+ nickname: undefined,
|
|
|
+ email: null,
|
|
|
+ phone: null,
|
|
|
+ name: null,
|
|
|
password: '',
|
|
|
- isDisabled: false,
|
|
|
+ isDisabled: DisabledStatus.ENABLED,
|
|
|
});
|
|
|
setIsModalOpen(true);
|
|
|
};
|
|
|
@@ -140,11 +141,11 @@ export const UsersPage = () => {
|
|
|
setIsCreateForm(false);
|
|
|
updateForm.reset({
|
|
|
username: user.username,
|
|
|
- nickname: user.nickname || '',
|
|
|
- email: user.email || '',
|
|
|
- phone: user.phone || '',
|
|
|
- name: user.name || '',
|
|
|
- isDisabled: user.isDisabled === 1,
|
|
|
+ nickname: user.nickname || undefined,
|
|
|
+ email: user.email || null,
|
|
|
+ phone: user.phone || null,
|
|
|
+ name: user.name || null,
|
|
|
+ isDisabled: user.isDisabled,
|
|
|
});
|
|
|
setIsModalOpen(true);
|
|
|
};
|