Przeglądaj źródła

✨ feat(user): 集成头像文件功能

- 添加 file-module 依赖以支持文件关联
- 恢复 UserEntity 中的 avatarFile 关联关系
- 在用户 schema 中添加 avatarFile 字段定义
- 更新用户服务查询,添加 avatarFile 关系查询

♻️ refactor(user): 完善用户数据查询

- 在用户查询方法中统一添加 avatarFile 关联关系
- 移除代码中关于等待 file-module 创建的临时注释
yourname 4 tygodni temu
rodzic
commit
888c68558a

+ 1 - 0
packages/user-module/package.json

@@ -43,6 +43,7 @@
     "@d8d/shared-utils": "workspace:*",
     "@d8d/shared-test-util": "workspace:*",
     "@d8d/auth-module": "workspace:*",
+    "@d8d/file-module": "workspace:*",
     "@hono/zod-openapi": "1.0.2",
     "bcrypt": "^6.0.0",
     "hono": "^4.8.5",

+ 4 - 4
packages/user-module/src/entities/user.entity.ts

@@ -1,6 +1,7 @@
 import { Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable, CreateDateColumn, UpdateDateColumn, ManyToOne, JoinColumn } from 'typeorm';
 import { Role } from './role.entity';
 import { DeleteStatus, DisabledStatus } from '@d8d/shared-types';
+import { File } from '@d8d/file-module';
 
 @Entity({ name: 'users' })
 export class UserEntity {
@@ -28,10 +29,9 @@ export class UserEntity {
   @Column({ name: 'avatar_file_id', type: 'int', unsigned: true, nullable: true, comment: '头像文件ID' })
   avatarFileId!: number | null;
 
-  // 暂时移除对 File 的依赖,等待 file-module 创建
-  // @ManyToOne(() => File, { nullable: true })
-  // @JoinColumn({ name: 'avatar_file_id', referencedColumnName: 'id' })
-  // avatarFile!: File | null;
+  @ManyToOne(() => File, { nullable: true })
+  @JoinColumn({ name: 'avatar_file_id', referencedColumnName: 'id' })
+  avatarFile!: File | null;
 
   @Column({ name: 'is_disabled', type: 'int', default: DisabledStatus.ENABLED, comment: '是否禁用(0:启用,1:禁用)' })
   isDisabled!: DisabledStatus;

+ 9 - 10
packages/user-module/src/schemas/user.schema.ts

@@ -33,16 +33,15 @@ export const UserSchema = z.object({
     example: 1,
     description: '头像文件ID'
   }),
-  // 暂时移除 avatarFile 字段,等待 file-module 创建
-  // avatarFile: z.object({
-  //   id: z.number().int().positive().openapi({ description: '文件ID' }),
-  //   name: z.string().max(255).openapi({ description: '文件名', example: 'avatar.jpg' }),
-  //   fullUrl: z.string().openapi({ description: '文件完整URL', example: 'https://example.com/avatar.jpg' }),
-  //   type: z.string().nullable().openapi({ description: '文件类型', example: 'image/jpeg' }),
-  //   size: z.number().nullable().openapi({ description: '文件大小(字节)', example: 102400 })
-  // }).nullable().optional().openapi({
-  //   description: '头像文件信息'
-  // }),
+  avatarFile: z.object({
+    id: z.number().int().positive().openapi({ description: '文件ID' }),
+    name: z.string().max(255).openapi({ description: '文件名', example: 'avatar.jpg' }),
+    fullUrl: z.string().openapi({ description: '文件完整URL', example: 'https://example.com/avatar.jpg' }),
+    type: z.string().nullable().openapi({ description: '文件类型', example: 'image/jpeg' }),
+    size: z.number().nullable().openapi({ description: '文件大小(字节)', example: 102400 })
+  }).nullable().optional().openapi({
+    description: '头像文件信息'
+  }),
   openid: z.string().max(255).nullable().optional().openapi({
     example: 'oABCDEFGH123456789',
     description: '微信小程序openid'

+ 5 - 5
packages/user-module/src/services/user.service.ts

@@ -34,7 +34,7 @@ export class UserService {
     try {
       return await this.userRepository.findOne({
         where: { id },
-        relations: ['roles'] // 暂时移除 avatarFile 关系
+        relations: ['roles', 'avatarFile']
       });
     } catch (error) {
       console.error('Error getting user:', error);
@@ -46,7 +46,7 @@ export class UserService {
     try {
       return await this.userRepository.findOne({
         where: { username },
-        relations: ['roles'] // 暂时移除 avatarFile 关系
+        relations: ['roles', 'avatarFile']
       });
     } catch (error) {
       console.error('Error getting user:', error);
@@ -58,7 +58,7 @@ export class UserService {
     try {
       return await this.userRepository.findOne({
         where: { phone: phone },
-        relations: ['roles'] // 暂时移除 avatarFile 关系
+        relations: ['roles', 'avatarFile']
       });
     } catch (error) {
       console.error('Error getting user by phone:', error);
@@ -110,7 +110,7 @@ export class UserService {
   async getUsers(): Promise<User[]> {
     try {
       const users = await this.userRepository.find({
-        relations: ['roles'] // 暂时移除 avatarFile 关系
+        relations: ['roles', 'avatarFile']
       });
       return users;
     } catch (error) {
@@ -127,7 +127,7 @@ export class UserService {
     try {
       return await this.userRepository.findOne({
         where: [{ username: account }, { email: account }],
-        relations: ['roles'] // 暂时移除 avatarFile 关系
+        relations: ['roles', 'avatarFile']
       });
     } catch (error) {
       console.error('Error getting user by account:', error);

+ 3 - 0
pnpm-lock.yaml

@@ -522,6 +522,9 @@ importers:
       '@d8d/auth-module':
         specifier: workspace:*
         version: link:../auth-module
+      '@d8d/file-module':
+        specifier: workspace:*
+        version: link:../file-module
       '@d8d/shared-crud':
         specifier: workspace:*
         version: link:../shared-crud