| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586 |
- import { useState } from 'react';
- import { useQuery } from '@tanstack/react-query';
- import { useForm } from 'react-hook-form';
- import { zodResolver } from '@hookform/resolvers/zod';
- import { z } from 'zod';
- import { Plus, Search, Edit, Trash2, Folder } from 'lucide-react';
- import { toast } from 'sonner';
- import { Button } from '@/client/components/ui/button';
- import { Input } from '@/client/components/ui/input';
- import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/client/components/ui/card';
- import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/client/components/ui/table';
- 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 { Badge } from '@/client/components/ui/badge';
- import { DataTablePagination } from '@/client/admin-shadcn/components/DataTablePagination';
- import { ImageSelector } from '@/client/admin-shadcn/components/ImageSelector';
- import { goodsCategoryClient } from '@/client/api';
- import { CreateGoodsCategoryDto, UpdateGoodsCategoryDto } from '@/server/modules/goods/goods-category.schema';
- import type { InferRequestType, InferResponseType } from 'hono/client';
- // 类型定义
- type CreateRequest = InferRequestType<typeof goodsCategoryClient.$post>['json'];
- type UpdateRequest = InferRequestType<typeof goodsCategoryClient[':id']['$put']>['json'];
- type GoodsCategoryResponse = InferResponseType<typeof goodsCategoryClient.$get, 200>['data'][0];
- // 表单Schema直接使用后端定义
- const createFormSchema = CreateGoodsCategoryDto;
- const updateFormSchema = UpdateGoodsCategoryDto;
- export const GoodsCategories = () => {
- // 状态管理
- const [searchParams, setSearchParams] = useState({ page: 1, limit: 10, search: '' });
- const [isModalOpen, setIsModalOpen] = useState(false);
- const [editingCategory, setEditingCategory] = useState<GoodsCategoryResponse | null>(null);
- const [isCreateForm, setIsCreateForm] = useState(true);
- const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
- const [categoryToDelete, setCategoryToDelete] = useState<number | null>(null);
- // 表单实例
- const createForm = useForm<CreateRequest>({
- resolver: zodResolver(createFormSchema),
- defaultValues: {
- name: '',
- parentId: 0,
- imageFileId: null,
- level: 0,
- state: 1,
- },
- });
- const updateForm = useForm<UpdateRequest>({
- resolver: zodResolver(updateFormSchema),
- });
- // 数据查询
- const { data, isLoading, refetch } = useQuery({
- queryKey: ['goods-categories', searchParams],
- queryFn: async () => {
- const res = await goodsCategoryClient.$get({
- query: {
- page: searchParams.page,
- pageSize: searchParams.limit,
- keyword: searchParams.search,
- filters: JSON.stringify({state:[1,2]})
- },
- });
- if (res.status !== 200) throw new Error('获取商品分类列表失败');
- return await res.json();
- },
- });
- // 处理搜索
- const handleSearch = (e: React.FormEvent) => {
- e.preventDefault();
- setSearchParams(prev => ({ ...prev, page: 1 }));
- };
- // 处理创建商品分类
- const handleCreateCategory = () => {
- setIsCreateForm(true);
- setEditingCategory(null);
- createForm.reset({
- name: '',
- parentId: 0,
- imageFileId: null,
- level: 0,
- state: 1,
- });
- setIsModalOpen(true);
- };
- // 处理编辑商品分类
- const handleEditCategory = (category: GoodsCategoryResponse) => {
- setIsCreateForm(false);
- setEditingCategory(category);
- updateForm.reset({
- name: category.name,
- parentId: category.parentId,
- imageFileId: category.imageFileId,
- level: category.level,
- state: category.state,
- });
- setIsModalOpen(true);
- };
- // 处理删除商品分类
- const handleDeleteCategory = (id: number) => {
- setCategoryToDelete(id);
- setDeleteDialogOpen(true);
- };
- // 确认删除
- const confirmDelete = async () => {
- if (!categoryToDelete) return;
- try {
- const res = await goodsCategoryClient[':id']['$delete']({
- param: { id: categoryToDelete.toString() },
- });
- if (res.status === 204) {
- toast.success('删除成功');
- setDeleteDialogOpen(false);
- refetch();
- } else {
- throw new Error('删除失败');
- }
- } catch (error) {
- toast.error('删除失败,请重试');
- }
- };
- // 处理表单提交
- const handleCreateSubmit = async (data: CreateRequest) => {
- try {
- const res = await goodsCategoryClient.$post({ json: data });
- if (res.status !== 201) throw new Error('创建失败');
- toast.success('创建成功');
- setIsModalOpen(false);
- refetch();
- } catch (error) {
- toast.error('创建失败,请重试');
- }
- };
- const handleUpdateSubmit = async (data: UpdateRequest) => {
- if (!editingCategory) return;
- try {
- const res = await goodsCategoryClient[':id']['$put']({
- param: { id: editingCategory.id.toString() },
- json: data,
- });
- if (res.status !== 200) throw new Error('更新失败');
- toast.success('更新成功');
- setIsModalOpen(false);
- refetch();
- } catch (error) {
- toast.error('更新失败,请重试');
- }
- };
- // 获取状态显示文本
- const getStateText = (state: number) => {
- return state === 1 ? '可用' : '不可用';
- };
- const getStateBadgeVariant = (state: number) => {
- return state === 1 ? 'default' : 'secondary';
- };
- // 格式化日期
- const formatDate = (dateString: string) => {
- return new Date(dateString).toLocaleDateString('zh-CN');
- };
- // 渲染骨架屏
- if (isLoading) {
- return (
- <div className="space-y-4">
- <div className="flex justify-between items-center">
- <h1 className="text-2xl font-bold">商品分类管理</h1>
- <Button disabled>
- <Plus className="mr-2 h-4 w-4" />
- 创建分类
- </Button>
- </div>
- <Card>
- <CardHeader>
- <div className="h-6 w-1/4 bg-gray-200 rounded animate-pulse" />
- </CardHeader>
- <CardContent>
- <div className="space-y-2">
- <div className="h-4 w-full bg-gray-200 rounded animate-pulse" />
- <div className="h-4 w-full bg-gray-200 rounded animate-pulse" />
- <div className="h-4 w-full bg-gray-200 rounded animate-pulse" />
- </div>
- </CardContent>
- </Card>
- </div>
- );
- }
- return (
- <div className="space-y-4">
- {/* 页面标题区域 */}
- <div className="flex justify-between items-center">
- <div>
- <h1 className="text-2xl font-bold">商品分类管理</h1>
- <p className="text-muted-foreground">管理商品分类信息</p>
- </div>
- <Button onClick={handleCreateCategory}>
- <Plus className="mr-2 h-4 w-4" />
- 创建分类
- </Button>
- </div>
- {/* 搜索区域 */}
- <Card>
- <CardHeader>
- <CardTitle>商品分类列表</CardTitle>
- <CardDescription>查看和管理所有商品分类</CardDescription>
- </CardHeader>
- <CardContent>
- <form onSubmit={handleSearch} className="flex gap-2 mb-4">
- <div className="relative flex-1 max-w-sm">
- <Search className="absolute left-2 top-2.5 h-4 w-4 text-muted-foreground" />
- <Input
- placeholder="搜索分类名称..."
- value={searchParams.search}
- onChange={(e) => setSearchParams(prev => ({ ...prev, search: e.target.value }))}
- className="pl-8"
- />
- </div>
- <Button type="submit" variant="outline">
- 搜索
- </Button>
- </form>
- {/* 数据表格 */}
- <div className="rounded-md border">
- <Table>
- <TableHeader>
- <TableRow>
- <TableHead>ID</TableHead>
- <TableHead>分类名称</TableHead>
- <TableHead>上级ID</TableHead>
- <TableHead>层级</TableHead>
- <TableHead>状态</TableHead>
- <TableHead>图片</TableHead>
- <TableHead>创建时间</TableHead>
- <TableHead className="text-right">操作</TableHead>
- </TableRow>
- </TableHeader>
- <TableBody>
- {data?.data.map((category) => (
- <TableRow key={category.id}>
- <TableCell className="font-medium">{category.id}</TableCell>
- <TableCell>
- <div className="flex items-center gap-2">
- <Folder className="h-4 w-4 text-muted-foreground" />
- <span>{category.name}</span>
- </div>
- </TableCell>
- <TableCell>{category.parentId}</TableCell>
- <TableCell>{category.level}</TableCell>
- <TableCell>
- <Badge variant={getStateBadgeVariant(category.state)}>
- {getStateText(category.state)}
- </Badge>
- </TableCell>
- <TableCell>
- {category.imageFile?.fullUrl ? (
- <img
- src={category.imageFile.fullUrl}
- alt={category.name}
- className="w-10 h-10 object-cover rounded"
- onError={(e) => {
- e.currentTarget.src = '/placeholder.png';
- }}
- />
- ) : (
- <span className="text-muted-foreground text-xs">无图片</span>
- )}
- </TableCell>
- <TableCell>{formatDate(category.createdAt)}</TableCell>
- <TableCell className="text-right">
- <div className="flex justify-end gap-2">
- <Button
- variant="ghost"
- size="icon"
- onClick={() => handleEditCategory(category)}
- >
- <Edit className="h-4 w-4" />
- </Button>
- <Button
- variant="ghost"
- size="icon"
- onClick={() => handleDeleteCategory(category.id)}
- >
- <Trash2 className="h-4 w-4" />
- </Button>
- </div>
- </TableCell>
- </TableRow>
- ))}
- </TableBody>
- </Table>
- </div>
- {data?.data.length === 0 && !isLoading && (
- <div className="text-center py-8">
- <p className="text-muted-foreground">暂无数据</p>
- </div>
- )}
- {/* 分页 */}
- <DataTablePagination
- currentPage={searchParams.page}
- pageSize={searchParams.limit}
- totalCount={data?.pagination.total || 0}
- onPageChange={(page, limit) => setSearchParams(prev => ({ ...prev, page, limit }))}
- />
- </CardContent>
- </Card>
- {/* 创建/编辑模态框 */}
- <Dialog open={isModalOpen} onOpenChange={setIsModalOpen}>
- <DialogContent className="sm:max-w-[500px] max-h-[90vh] overflow-y-auto">
- <DialogHeader>
- <DialogTitle>{isCreateForm ? '创建商品分类' : '编辑商品分类'}</DialogTitle>
- <DialogDescription>
- {isCreateForm ? '创建一个新的商品分类' : '编辑现有商品分类信息'}
- </DialogDescription>
- </DialogHeader>
- {isCreateForm ? (
- <Form {...createForm}>
- <form onSubmit={createForm.handleSubmit(handleCreateSubmit)} className="space-y-4">
- <FormField
- control={createForm.control}
- name="name"
- render={({ field }) => (
- <FormItem>
- <FormLabel className="flex items-center">
- 分类名称 <span className="text-red-500 ml-1">*</span>
- </FormLabel>
- <FormControl>
- <Input placeholder="请输入分类名称" {...field} />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={createForm.control}
- name="parentId"
- render={({ field }) => (
- <FormItem>
- <FormLabel>上级分类ID</FormLabel>
- <FormControl>
- <Input
- type="number"
- placeholder="请输入上级分类ID,0表示顶级分类"
- {...field}
- onChange={(e) => field.onChange(parseInt(e.target.value) || 0)}
- />
- </FormControl>
- <FormDescription>顶级分类请填0</FormDescription>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={createForm.control}
- name="level"
- render={({ field }) => (
- <FormItem>
- <FormLabel>层级</FormLabel>
- <FormControl>
- <Input
- type="number"
- placeholder="请输入层级"
- {...field}
- onChange={(e) => field.onChange(parseInt(e.target.value) || 0)}
- />
- </FormControl>
- <FormDescription>顶级分类为0,依次递增</FormDescription>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={createForm.control}
- name="state"
- render={({ field }) => (
- <FormItem>
- <FormLabel>状态</FormLabel>
- <FormControl>
- <select
- className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
- {...field}
- onChange={(e) => field.onChange(parseInt(e.target.value))}
- >
- <option value={1}>可用</option>
- <option value={2}>不可用</option>
- </select>
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={createForm.control}
- name="imageFileId"
- render={({ field }) => (
- <FormItem>
- <FormLabel>分类图片</FormLabel>
- <FormControl>
- <ImageSelector
- value={field.value || undefined}
- onChange={(value) => field.onChange(value)}
- maxSize={2}
- uploadPath="/goods-categories"
- uploadButtonText="上传分类图片"
- previewSize="medium"
- placeholder="选择分类图片"
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <DialogFooter>
- <Button type="button" variant="outline" onClick={() => setIsModalOpen(false)}>
- 取消
- </Button>
- <Button type="submit">创建</Button>
- </DialogFooter>
- </form>
- </Form>
- ) : (
- <Form {...updateForm}>
- <form onSubmit={updateForm.handleSubmit(handleUpdateSubmit)} className="space-y-4">
- <FormField
- control={updateForm.control}
- name="name"
- render={({ field }) => (
- <FormItem>
- <FormLabel className="flex items-center">
- 分类名称 <span className="text-red-500 ml-1">*</span>
- </FormLabel>
- <FormControl>
- <Input placeholder="请输入分类名称" {...field} />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={updateForm.control}
- name="parentId"
- render={({ field }) => (
- <FormItem>
- <FormLabel>上级分类ID</FormLabel>
- <FormControl>
- <Input
- type="number"
- placeholder="请输入上级分类ID,0表示顶级分类"
- {...field}
- onChange={(e) => field.onChange(parseInt(e.target.value) || 0)}
- />
- </FormControl>
- <FormDescription>顶级分类请填0</FormDescription>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={updateForm.control}
- name="level"
- render={({ field }) => (
- <FormItem>
- <FormLabel>层级</FormLabel>
- <FormControl>
- <Input
- type="number"
- placeholder="请输入层级"
- {...field}
- onChange={(e) => field.onChange(parseInt(e.target.value) || 0)}
- value={field.value ?? ''}
- />
- </FormControl>
- <FormDescription>顶级分类为0,依次递增</FormDescription>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={updateForm.control}
- name="state"
- render={({ field }) => (
- <FormItem>
- <FormLabel>状态</FormLabel>
- <FormControl>
- <select
- className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
- value={field.value ?? 1}
- onChange={(e) => field.onChange(parseInt(e.target.value))}
- >
- <option value={1}>可用</option>
- <option value={2}>不可用</option>
- </select>
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={updateForm.control}
- name="imageFileId"
- render={({ field }) => (
- <FormItem>
- <FormLabel>分类图片</FormLabel>
- <FormControl>
- <ImageSelector
- value={field.value || undefined}
- onChange={(value) => field.onChange(value)}
- maxSize={2}
- uploadPath="/goods-categories"
- uploadButtonText="上传分类图片"
- previewSize="medium"
- placeholder="选择分类图片"
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <DialogFooter>
- <Button type="button" variant="outline" onClick={() => setIsModalOpen(false)}>
- 取消
- </Button>
- <Button type="submit">更新</Button>
- </DialogFooter>
- </form>
- </Form>
- )}
- </DialogContent>
- </Dialog>
- {/* 删除确认对话框 */}
- <Dialog open={deleteDialogOpen} onOpenChange={setDeleteDialogOpen}>
- <DialogContent>
- <DialogHeader>
- <DialogTitle>确认删除</DialogTitle>
- <DialogDescription>
- 确定要删除这个商品分类吗?此操作无法撤销。
- </DialogDescription>
- </DialogHeader>
- <DialogFooter>
- <Button variant="outline" onClick={() => setDeleteDialogOpen(false)}>
- 取消
- </Button>
- <Button variant="destructive" onClick={confirmDelete}>
- 删除
- </Button>
- </DialogFooter>
- </DialogContent>
- </Dialog>
- </div>
- );
- };
|