Procházet zdrojové kódy

♻️ refactor(admin): 优化用户管理页面代码结构

- 移除MainLayout中未使用的Toaster组件
- 清理Users页面未使用的导入和组件
- 优化用户表单验证逻辑,统一处理空值转换
- 更新用户角色判断逻辑,支持多角色系统

🐛 fix(users): 修复用户角色显示错误

- 修正用户角色判断逻辑,从单角色改为多角色判断
- 确保管理员角色正确显示为红色标签
yourname před 4 měsíci
rodič
revize
f547f10c23

+ 0 - 2
src/client/admin-shadcn/layouts/MainLayout.tsx

@@ -20,8 +20,6 @@ import { Sheet, SheetContent, SheetHeader, SheetTitle } from '@/client/component
 import { ScrollArea } from '@/client/components/ui/scroll-area';
 import { cn } from '@/client/lib/utils';
 import { Badge } from '@/client/components/ui/badge';
-import { Toaster } from '@/client/components/ui/sonner';
-
 /**
  * 主布局组件
  * 包含侧边栏、顶部导航和内容区域

+ 8 - 11
src/client/admin-shadcn/pages/Users.tsx

@@ -1,8 +1,8 @@
 import React, { useState } from 'react';
 import { useQuery } from '@tanstack/react-query';
 import { format } from 'date-fns';
-import { Plus, Search, Edit, Trash2, User, Mail, Phone } from 'lucide-react';
-import { userClient, roleClient } from '@/client/api';
+import { Plus, Search, Edit, Trash2 } from 'lucide-react';
+import { userClient } from '@/client/api';
 import type { InferResponseType, InferRequestType } from 'hono/client';
 import { Button } from '@/client/components/ui/button';
 import { Input } from '@/client/components/ui/input';
@@ -11,16 +11,13 @@ import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@
 import { Badge } from '@/client/components/ui/badge';
 import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/client/components/ui/dialog';
 import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from '@/client/components/ui/form';
-import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/client/components/ui/select';
 import { useForm } from 'react-hook-form';
 import { zodResolver } from '@hookform/resolvers/zod';
 import { z } from 'zod';
 import { toast } from 'sonner';
 import { Skeleton } from '@/client/components/ui/skeleton';
 import { Switch } from '@/client/components/ui/switch';
-import { Label } from '@/client/components/ui/label';
 
-type UserListResponse = InferResponseType<typeof userClient.$get, 200>;
 type CreateUserRequest = InferRequestType<typeof userClient.$post>['json'];
 type UpdateUserRequest = InferRequestType<typeof userClient[':id']['$put']>['json'];
 
@@ -28,9 +25,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.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),
+  email: z.string().email('请输入有效的邮箱地址').nullable().optional().transform(val => val === '' ? null : val),
+  phone: z.string().regex(/^1[3-9]\d{9}$/, '请输入有效的手机号').nullable().optional().transform(val => val === '' ? null : val),
+  name: z.string().nullable().optional().transform(val => val === '' ? null : val),
   password: z.string().min(6, '密码至少6个字符').optional(),
   isDisabled: z.boolean().default(false),
 });
@@ -270,11 +267,11 @@ export const UsersPage = () => {
                     <TableCell>{user.email || '-'}</TableCell>
                     <TableCell>{user.name || '-'}</TableCell>
                     <TableCell>
-                      <Badge 
-                        variant={user.role === 'admin' ? 'destructive' : 'default'}
+                      <Badge
+                        variant={user.roles?.some(role => role.name === 'admin') ? 'destructive' : 'default'}
                         className="capitalize"
                       >
-                        {user.role === 'admin' ? '管理员' : '普通用户'}
+                        {user.roles?.some(role => role.name === 'admin') ? '管理员' : '普通用户'}
                       </Badge>
                     </TableCell>
                     <TableCell>