Procházet zdrojové kódy

✨ feat(avatar): 重构头像上传流程以支持新的用户资料系统

- 替换API客户端:从userClient迁移到silverUserProfileClient
- 添加用户资料处理逻辑:获取或创建用户资料并更新头像
- 优化错误处理:为资料获取和创建过程添加详细错误捕获
- 增强缓存管理:添加silverUserProfile查询缓存失效处理
- 修改头像存储方式:使用fileId代替直接存储URL
yourname před 7 měsíci
rodič
revize
349e0a2987
1 změnil soubory, kde provedl 40 přidání a 6 odebrání
  1. 40 6
      src/client/mobile/components/AvatarUpload.tsx

+ 40 - 6
src/client/mobile/components/AvatarUpload.tsx

@@ -1,11 +1,10 @@
 import React, { useState, useRef } from 'react';
 import { useMutation, useQueryClient } from '@tanstack/react-query';
-import { userClient } from '@/client/api';
+import { silverUserProfileClient } from '@/client/api';
 import { uploadMinIOWithPolicy } from '@/client/utils/minio';
 import { compressImage, isImageFile, checkFileSize } from '@/client/utils/upload';
 import { useAuth } from '../hooks/AuthProvider';
 import { AvatarCropper } from './AvatarCropper';
-import type { User } from '@/server/modules/users/user.entity';
 
 // 水墨风格色彩系统
 const COLORS = {
@@ -67,15 +66,49 @@ export const AvatarUpload: React.FC<AvatarUploadProps> = ({
         fileKey
       );
 
-      // 4. 更新用户头像
-      const updateResponse = await userClient[user.id].$put({
+      // 4. 获取用户资料ID
+      const profileResponse = await silverUserProfileClient.$get({
+        query: { filters: JSON.stringify({ userId: user.id }) }
+      });
+      
+      let profileId: number;
+      if (profileResponse.status === 200) {
+        const profileData = await profileResponse.json();
+        if (profileData.data && profileData.data.length > 0) {
+          profileId = profileData.data[0].id;
+        } else {
+          // 创建新的用户资料
+          const createResponse = await silverUserProfileClient.$post({
+            json: {
+              realName: user.username || '用户',
+              age: 30,
+              gender: 1,
+              phone: '',
+              avatarFileId: result.fileId,
+            }
+          });
+          
+          if (createResponse.status !== 201) {
+            throw new Error('创建用户资料失败');
+          }
+          
+          const createdProfile = await createResponse.json();
+          profileId = createdProfile.id;
+        }
+      } else {
+        throw new Error('获取用户资料失败');
+      }
+
+      // 5. 更新用户资料头像
+      const updateResponse = await silverUserProfileClient[':id'].$put({
+        param: { id: profileId },
         json: {
-          avatar: result.fileUrl,
+          avatarFileId: result.fileId,
         }
       });
 
       if (updateResponse.status !== 200) {
-        throw new Error('更新用户信息失败');
+        throw new Error('更新用户头像失败');
       }
 
       return result.fileUrl;
@@ -84,6 +117,7 @@ export const AvatarUpload: React.FC<AvatarUploadProps> = ({
       // 清除相关缓存
       queryClient.invalidateQueries({ queryKey: ['userProfile'] });
       queryClient.invalidateQueries({ queryKey: ['currentUser'] });
+      queryClient.invalidateQueries({ queryKey: ['silverUserProfile'] });
       
       if (onUploadSuccess) {
         onUploadSuccess(avatarUrl);