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

✨ feat(files): 优化文件列表中上传用户显示信息

- 后端:添加文件与上传用户的关联关系,查询时返回用户详细信息
- 前端:将"上传用户ID"列改为显示用户名或昵称,优先显示昵称
- 类型修正:将editingKey状态类型从string改为number,匹配实际ID类型
yourname 8 месяцев назад
Родитель
Сommit
ba4c0e3732

+ 6 - 3
src/client/admin/pages/Files.tsx

@@ -18,7 +18,7 @@ const Files: React.FC = () => {
   const { message } = App.useApp();
   const [form] = Form.useForm();
   const [modalVisible, setModalVisible] = useState(false);
-  const [editingKey, setEditingKey] = useState<string | null>(null);
+  const [editingKey, setEditingKey] = useState<number | null>(null);
   const [searchText, setSearchText] = useState('');
   const [pagination, setPagination] = useState({
     current: 1,
@@ -173,8 +173,11 @@ const Files: React.FC = () => {
     },
     {
       title: '上传用户',
-      dataIndex: 'uploadUserId',
-      key: 'uploadUserId',
+      dataIndex: 'uploadUser',
+      key: 'uploadUser',
+      render: (uploadUser?: { username: string; nickname?: string }) => {
+        return uploadUser ? (uploadUser.nickname || uploadUser.username) : '-';
+      },
     },
     {
       title: '操作',

+ 1 - 0
src/server/api/files/index.ts

@@ -18,6 +18,7 @@ const fileRoutes = createCrudRoutes({
   getSchema: FileSchema,
   listSchema: FileSchema,
   searchFields: ['name', 'type', 'description'],
+  relations: ['uploadUser'],
   middleware: [authMiddleware]
 })
 

+ 20 - 19
src/server/modules/files/file.entity.ts

@@ -1,6 +1,6 @@
 import { Entity, PrimaryGeneratedColumn, Column, Index, ManyToOne, JoinColumn } from 'typeorm';
 import { z } from '@hono/zod-openapi';
-import { UserEntity } from '@/server/modules/users/user.entity';
+import { UserEntity, UserSchema } from '@/server/modules/users/user.entity';
 
 @Entity('file')
 export class File {
@@ -52,45 +52,46 @@ export const FileSchema = z.object({
     description: '文件ID',
     example: 1
   }),
-  name: z.string().max(255).openapi({ 
+  name: z.string().max(255).openapi({
     description: '文件名称',
-    example: '项目计划书.pdf' 
+    example: '项目计划书.pdf'
   }),
-  type: z.string().max(50).nullable().openapi({ 
+  type: z.string().max(50).nullable().openapi({
     description: '文件类型',
-    example: 'application/pdf' 
+    example: 'application/pdf'
   }),
-  size: z.number().int().positive().nullable().openapi({ 
+  size: z.number().int().positive().nullable().openapi({
     description: '文件大小,单位字节',
-    example: 102400 
+    example: 102400
   }),
-  path: z.string().max(512).openapi({ 
+  path: z.string().max(512).openapi({
     description: '文件存储路径',
-    example: '/uploads/documents/2023/project-plan.pdf' 
+    example: '/uploads/documents/2023/project-plan.pdf'
   }),
-  description: z.string().nullable().openapi({ 
+  description: z.string().nullable().openapi({
     description: '文件描述',
-    example: '2023年度项目计划书' 
+    example: '2023年度项目计划书'
   }),
   uploadUserId: z.number().int().positive().openapi({
     description: '上传用户ID',
     example: 1
   }),
-  uploadTime: z.date().openapi({ 
+  uploadUser: UserSchema,
+  uploadTime: z.date().openapi({
     description: '上传时间',
-    example: '2023-01-15T10:30:00Z' 
+    example: '2023-01-15T10:30:00Z'
   }),
-  lastUpdated: z.date().nullable().openapi({ 
+  lastUpdated: z.date().nullable().openapi({
     description: '最后更新时间',
-    example: '2023-01-16T14:20:00Z' 
+    example: '2023-01-16T14:20:00Z'
   }),
-  createdAt: z.date().openapi({ 
+  createdAt: z.date().openapi({
     description: '创建时间',
-    example: '2023-01-15T10:30:00Z' 
+    example: '2023-01-15T10:30:00Z'
   }),
-  updatedAt: z.date().openapi({ 
+  updatedAt: z.date().openapi({
     description: '更新时间',
-    example: '2023-01-16T14:20:00Z' 
+    example: '2023-01-16T14:20:00Z'
   })
 });
 

+ 4 - 0
src/server/modules/users/user.entity.ts

@@ -2,6 +2,7 @@ import { Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable, CreateDa
 import { Role, RoleSchema } from './role.entity';
 import { z } from '@hono/zod-openapi';
 import { DeleteStatus, DisabledStatus } from '@/share/types';
+import { File } from '@/server/modules/files/file.entity';
 
 @Entity({ name: 'users' })
 export class UserEntity {
@@ -39,6 +40,9 @@ export class UserEntity {
   @JoinTable()
   roles!: Role[];
 
+  @OneToMany(() => File, file => file.uploadUser)
+  uploadFiles!: File[];
+
   @CreateDateColumn({ name: 'created_at', type: 'timestamp' })
   createdAt!: Date;