|
|
@@ -18,33 +18,16 @@ import { toast } from 'sonner';
|
|
|
import { Skeleton } from '@/client/components/ui/skeleton';
|
|
|
import { Switch } from '@/client/components/ui/switch';
|
|
|
import { DisabledStatus } from '@/share/types';
|
|
|
+import { CreateUserDto, UpdateUserDto } from '@/server/modules/users/user.schema';
|
|
|
|
|
|
// 使用RPC方式提取类型
|
|
|
type CreateUserRequest = InferRequestType<typeof userClient.$post>['json'];
|
|
|
type UpdateUserRequest = InferRequestType<typeof userClient[':id']['$put']>['json'];
|
|
|
type UserResponse = InferResponseType<typeof userClient.$get, 200>['data'][0];
|
|
|
|
|
|
-// 创建用户表单Schema - 与后端CreateUserSchema保持一致
|
|
|
-const createUserFormSchema = z.object({
|
|
|
- username: z.string().min(3, '用户名至少3个字符'),
|
|
|
- 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.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().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.number().int().min(0).max(1).default(DisabledStatus.ENABLED),
|
|
|
-});
|
|
|
+// 直接使用后端定义的 schema
|
|
|
+const createUserFormSchema = CreateUserDto;
|
|
|
+const updateUserFormSchema = UpdateUserDto;
|
|
|
|
|
|
type CreateUserFormData = CreateUserRequest;
|
|
|
type UpdateUserFormData = UpdateUserRequest;
|
|
|
@@ -125,7 +108,7 @@ export const UsersPage = () => {
|
|
|
setIsCreateForm(true);
|
|
|
createForm.reset({
|
|
|
username: '',
|
|
|
- nickname: undefined,
|
|
|
+ nickname: null,
|
|
|
email: null,
|
|
|
phone: null,
|
|
|
name: null,
|
|
|
@@ -141,10 +124,10 @@ export const UsersPage = () => {
|
|
|
setIsCreateForm(false);
|
|
|
updateForm.reset({
|
|
|
username: user.username,
|
|
|
- nickname: user.nickname || undefined,
|
|
|
- email: user.email || null,
|
|
|
- phone: user.phone || null,
|
|
|
- name: user.name || null,
|
|
|
+ nickname: user.nickname,
|
|
|
+ email: user.email,
|
|
|
+ phone: user.phone,
|
|
|
+ name: user.name,
|
|
|
isDisabled: user.isDisabled,
|
|
|
});
|
|
|
setIsModalOpen(true);
|
|
|
@@ -153,13 +136,8 @@ export const UsersPage = () => {
|
|
|
// 处理创建表单提交
|
|
|
const handleCreateSubmit = async (data: CreateUserFormData) => {
|
|
|
try {
|
|
|
- const submitData: CreateUserRequest = {
|
|
|
- ...data,
|
|
|
- isDisabled: data.isDisabled ? 1 : 0,
|
|
|
- };
|
|
|
-
|
|
|
const res = await userClient.$post({
|
|
|
- json: submitData
|
|
|
+ json: data
|
|
|
});
|
|
|
if (res.status !== 201) {
|
|
|
throw new Error('创建用户失败');
|
|
|
@@ -178,14 +156,9 @@ export const UsersPage = () => {
|
|
|
if (!editingUser) return;
|
|
|
|
|
|
try {
|
|
|
- const submitData: UpdateUserRequest = {
|
|
|
- ...data,
|
|
|
- isDisabled: data.isDisabled !== undefined ? (data.isDisabled ? 1 : 0) : undefined,
|
|
|
- };
|
|
|
-
|
|
|
const res = await userClient[':id']['$put']({
|
|
|
param: { id: editingUser.id },
|
|
|
- json: submitData
|
|
|
+ json: data
|
|
|
});
|
|
|
if (res.status !== 200) {
|
|
|
throw new Error('更新用户失败');
|
|
|
@@ -414,7 +387,7 @@ export const UsersPage = () => {
|
|
|
<FormItem>
|
|
|
<FormLabel>昵称</FormLabel>
|
|
|
<FormControl>
|
|
|
- <Input placeholder="请输入昵称" {...field} />
|
|
|
+ <Input placeholder="请输入昵称" {...field} value={field.value || ''} />
|
|
|
</FormControl>
|
|
|
<FormMessage />
|
|
|
</FormItem>
|
|
|
@@ -428,7 +401,7 @@ export const UsersPage = () => {
|
|
|
<FormItem>
|
|
|
<FormLabel>邮箱</FormLabel>
|
|
|
<FormControl>
|
|
|
- <Input type="email" placeholder="请输入邮箱" {...field} />
|
|
|
+ <Input type="email" placeholder="请输入邮箱" {...field} value={field.value || ''} />
|
|
|
</FormControl>
|
|
|
<FormMessage />
|
|
|
</FormItem>
|
|
|
@@ -442,7 +415,7 @@ export const UsersPage = () => {
|
|
|
<FormItem>
|
|
|
<FormLabel>手机号</FormLabel>
|
|
|
<FormControl>
|
|
|
- <Input placeholder="请输入手机号" {...field} />
|
|
|
+ <Input placeholder="请输入手机号" {...field} value={field.value || ''} />
|
|
|
</FormControl>
|
|
|
<FormMessage />
|
|
|
</FormItem>
|
|
|
@@ -456,7 +429,7 @@ export const UsersPage = () => {
|
|
|
<FormItem>
|
|
|
<FormLabel>真实姓名</FormLabel>
|
|
|
<FormControl>
|
|
|
- <Input placeholder="请输入真实姓名" {...field} />
|
|
|
+ <Input placeholder="请输入真实姓名" {...field} value={field.value || ''} />
|
|
|
</FormControl>
|
|
|
<FormMessage />
|
|
|
</FormItem>
|
|
|
@@ -490,8 +463,8 @@ export const UsersPage = () => {
|
|
|
</div>
|
|
|
<FormControl>
|
|
|
<Switch
|
|
|
- checked={field.value}
|
|
|
- onCheckedChange={field.onChange}
|
|
|
+ checked={field.value === 1}
|
|
|
+ onCheckedChange={(checked) => field.onChange(checked ? 1 : 0)}
|
|
|
/>
|
|
|
</FormControl>
|
|
|
</FormItem>
|
|
|
@@ -518,7 +491,7 @@ export const UsersPage = () => {
|
|
|
<FormItem>
|
|
|
<FormLabel>用户名</FormLabel>
|
|
|
<FormControl>
|
|
|
- <Input placeholder="请输入用户名" {...field} />
|
|
|
+ <Input placeholder="请输入用户名" {...field} value={field.value || ''} />
|
|
|
</FormControl>
|
|
|
<FormMessage />
|
|
|
</FormItem>
|
|
|
@@ -532,7 +505,7 @@ export const UsersPage = () => {
|
|
|
<FormItem>
|
|
|
<FormLabel>昵称</FormLabel>
|
|
|
<FormControl>
|
|
|
- <Input placeholder="请输入昵称" {...field} />
|
|
|
+ <Input placeholder="请输入昵称" {...field} value={field.value || ''} />
|
|
|
</FormControl>
|
|
|
<FormMessage />
|
|
|
</FormItem>
|
|
|
@@ -546,7 +519,7 @@ export const UsersPage = () => {
|
|
|
<FormItem>
|
|
|
<FormLabel>邮箱</FormLabel>
|
|
|
<FormControl>
|
|
|
- <Input type="email" placeholder="请输入邮箱" {...field} />
|
|
|
+ <Input type="email" placeholder="请输入邮箱" {...field} value={field.value || ''} />
|
|
|
</FormControl>
|
|
|
<FormMessage />
|
|
|
</FormItem>
|
|
|
@@ -560,7 +533,7 @@ export const UsersPage = () => {
|
|
|
<FormItem>
|
|
|
<FormLabel>手机号</FormLabel>
|
|
|
<FormControl>
|
|
|
- <Input placeholder="请输入手机号" {...field} />
|
|
|
+ <Input placeholder="请输入手机号" {...field} value={field.value || ''} />
|
|
|
</FormControl>
|
|
|
<FormMessage />
|
|
|
</FormItem>
|
|
|
@@ -574,7 +547,7 @@ export const UsersPage = () => {
|
|
|
<FormItem>
|
|
|
<FormLabel>真实姓名</FormLabel>
|
|
|
<FormControl>
|
|
|
- <Input placeholder="请输入真实姓名" {...field} />
|
|
|
+ <Input placeholder="请输入真实姓名" {...field} value={field.value || ''} />
|
|
|
</FormControl>
|
|
|
<FormMessage />
|
|
|
</FormItem>
|
|
|
@@ -608,8 +581,8 @@ export const UsersPage = () => {
|
|
|
</div>
|
|
|
<FormControl>
|
|
|
<Switch
|
|
|
- checked={field.value}
|
|
|
- onCheckedChange={field.onChange}
|
|
|
+ checked={field.value === 1}
|
|
|
+ onCheckedChange={(checked) => field.onChange(checked ? 1 : 0)}
|
|
|
/>
|
|
|
</FormControl>
|
|
|
</FormItem>
|