Просмотр исходного кода

♻️ refactor(home-icons): 优化图标管理相关代码

- 重命名File类型导入为FileType以避免命名冲突
- 统一使用homeIconClient替代client.homeIcons进行API调用
- 移除未使用的client变量
- 调整HomeIcon实体的时间戳字段定义,移除默认值配置

📝 docs(home-icons): 优化代码注释和类型定义

- 调整文件导入注释以提高可读性
- 统一代码格式化和缩进风格
yourname 7 месяцев назад
Родитель
Сommit
dcb052169f

+ 11 - 12
src/client/admin/pages/HomeIcons.tsx

@@ -4,7 +4,7 @@ import { PlusOutlined, EditOutlined, DeleteOutlined, EyeOutlined, LinkOutlined }
 import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
 import { homeIconClient } from '@/client/api';
 import type { HomeIcon } from '@/server/modules/home/home-icon.entity';
-import type { File } from '@/server/modules/files/file.entity';
+import type { File as FileType } from '@/server/modules/files/file.entity';
 import type { InferResponseType, InferRequestType } from 'hono/client';
 import MinioUploader from '@/client/admin/components/MinioUploader';
 
@@ -21,13 +21,12 @@ const HomeIconsPage: React.FC = () => {
   const [editingRecord, setEditingRecord] = useState<HomeIcon | null>(null);
   const [form] = Form.useForm();
   const queryClient = useQueryClient();
-  const client = useClient();
 
   // 获取图标列表
   const { data: iconsData, isLoading } = useQuery({
     queryKey: ['home-icons', activeTab],
     queryFn: async () => {
-      const response = await client.homeIcons.$get({
+      const response = await homeIconClient.$get({
         query: {
           type: activeTab,
           page: 1,
@@ -42,7 +41,7 @@ const HomeIconsPage: React.FC = () => {
   // 创建图标
   const createMutation = useMutation({
     mutationFn: async (data: CreateHomeIconRequest) => {
-      const response = await client.homeIcons.$post({ json: data });
+      const response = await homeIconClient.$post({ json: data });
       if (response.status !== 200) throw new Error('创建图标失败');
       return response.json();
     },
@@ -60,7 +59,7 @@ const HomeIconsPage: React.FC = () => {
   // 更新图标
   const updateMutation = useMutation({
     mutationFn: async ({ id, data }: { id: number; data: UpdateHomeIconRequest }) => {
-      const response = await client.homeIcons[":id"].$put({
+      const response = await homeIconClient[":id"].$put({
         param: { id: id.toString() },
         json: data
       });
@@ -81,7 +80,7 @@ const HomeIconsPage: React.FC = () => {
   // 删除图标
   const deleteMutation = useMutation({
     mutationFn: async (id: number) => {
-      const response = await client.homeIcons[":id"].$delete({
+      const response = await homeIconClient[":id"].$delete({
         param: { id: id.toString() }
       });
       if (response.status !== 200) throw new Error('删除图标失败');
@@ -99,7 +98,7 @@ const HomeIconsPage: React.FC = () => {
   // 切换启用状态
   const toggleEnabledMutation = useMutation({
     mutationFn: async ({ id, isEnabled }: { id: number; isEnabled: number }) => {
-      const response = await client.homeIcons[":id"].$put({
+      const response = await homeIconClient[":id"].$put({
         param: { id: id.toString() },
         json: { isEnabled }
       });
@@ -158,7 +157,7 @@ const HomeIconsPage: React.FC = () => {
     }
   };
 
-  const handleUploadSuccess = (fileKey: string, fileUrl: string, file: File) => {
+  const handleUploadSuccess = (fileKey: string, fileUrl: string, file: FileType) => {
     // 这里需要获取刚上传的文件ID
     // 实际项目中,上传成功后应该返回文件ID
     message.success('文件上传成功,请填写其他信息后提交');
@@ -170,10 +169,10 @@ const HomeIconsPage: React.FC = () => {
       dataIndex: 'file',
       key: 'file',
       width: 80,
-      render: (file: File) => (
-        <img 
-          src={file.path} 
-          alt={file.name} 
+      render: (file: FileType) => (
+        <img
+          src={file.path}
+          alt={file.name}
           style={{ width: 60, height: 60, objectFit: 'cover', borderRadius: 4 }}
         />
       )

+ 1 - 1
src/server/api/home-icons/index.ts

@@ -10,7 +10,7 @@ const homeIconRoutes = createCrudRoutes({
   getSchema: HomeIconSchema,
   listSchema: HomeIconSchema,
   searchFields: ['title', 'description'],
-  relations: ['file', 'file.uploadUser'],
+  relations: ['file.uploadUser'],
   middleware: [authMiddleware],
   userTracking: {
     createdByField: 'createdBy',

+ 2 - 2
src/server/modules/home/home-icon.entity.ts

@@ -32,10 +32,10 @@ export class HomeIcon {
   @Column({ name: 'description', type: 'text', nullable: true, comment: '描述信息' })
   description!: string | null;
 
-  @CreateDateColumn({ name: 'created_at', type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
+  @CreateDateColumn({ name: 'created_at', type: 'timestamp' })
   createdAt!: Date;
 
-  @UpdateDateColumn({ name: 'updated_at', type: 'timestamp', default: () => 'CURRENT_TIMESTAMP', onUpdate: 'CURRENT_TIMESTAMP' })
+  @UpdateDateColumn({ name: 'updated_at', type: 'timestamp' })
   updatedAt!: Date;
 }