|
|
@@ -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);
|