修改用户上传
This commit is contained in:
@@ -2,6 +2,7 @@ import { UserInfo } from '@/components/UserInfo';
|
||||
import Taro from '@tarojs/taro';
|
||||
import { API_CONFIG } from '@/config/api';
|
||||
import httpService from './httpService';
|
||||
import uploadFiles from './uploadFiles';
|
||||
|
||||
|
||||
// 用户详情接口
|
||||
@@ -158,8 +159,8 @@ export class UserService {
|
||||
}
|
||||
|
||||
// 处理距离 - 优先使用venue_dtl中的坐标,其次使用game中的坐标
|
||||
let latitude = game.latitude || 0;
|
||||
let longitude = game.longitude || 0;
|
||||
let latitude: number = typeof game.latitude === 'number' ? game.latitude : parseFloat(game.latitude || '0') || 0;
|
||||
let longitude: number = typeof game.longitude === 'number' ? game.longitude : parseFloat(game.longitude || '0') || 0;
|
||||
if (game.venue_dtl) {
|
||||
latitude = parseFloat(game.venue_dtl.latitude) || latitude;
|
||||
longitude = parseFloat(game.venue_dtl.longitude) || longitude;
|
||||
@@ -265,9 +266,9 @@ export class UserService {
|
||||
hosted: userData.stats?.hosted_games_count || 0,
|
||||
participated: userData.stats?.participated_games_count || 0
|
||||
},
|
||||
|
||||
|
||||
personal_profile: '',
|
||||
location:userData.province + userData.city || '',
|
||||
location: userData.province + userData.city || '',
|
||||
occupation: '',
|
||||
ntrp_level: '',
|
||||
phone: userData.phone || '',
|
||||
@@ -286,7 +287,28 @@ export class UserService {
|
||||
// 更新用户信息
|
||||
static async update_user_info(update_data: Partial<UserInfo>): Promise<void> {
|
||||
try {
|
||||
const response = await httpService.post(API_CONFIG.USER.UPDATE, update_data, {
|
||||
// 过滤掉空字段
|
||||
const filtered_data: Record<string, any> = {};
|
||||
|
||||
Object.keys(update_data).forEach(key => {
|
||||
const value = update_data[key as keyof UserInfo];
|
||||
// 只添加非空且非空字符串的字段
|
||||
if (value !== null && value !== undefined && value !== '') {
|
||||
if (typeof value === 'string' && value.trim() !== '') {
|
||||
filtered_data[key] = value.trim();
|
||||
} else if (typeof value !== 'string') {
|
||||
filtered_data[key] = value;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 如果没有需要更新的字段,直接返回
|
||||
if (Object.keys(filtered_data).length === 0) {
|
||||
console.log('没有需要更新的字段');
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await httpService.post(API_CONFIG.USER.UPDATE, filtered_data, {
|
||||
showLoading: true
|
||||
});
|
||||
|
||||
@@ -342,7 +364,7 @@ export class UserService {
|
||||
console.error('获取参与球局失败:', error);
|
||||
// 返回符合ListContainer data格式的模拟数据
|
||||
return [];
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -379,25 +401,41 @@ export class UserService {
|
||||
// 保存用户信息
|
||||
static async save_user_info(user_info: Partial<UserInfo> & { phone?: string; gender?: string }): Promise<boolean> {
|
||||
try {
|
||||
// 获取当前位置信息
|
||||
// const location = await Taro.getLocation({
|
||||
// type: 'wgs84'
|
||||
// });
|
||||
|
||||
const updateParams: UpdateUserParams = {
|
||||
nickname: user_info.nickname || '',
|
||||
avatar_url: user_info.avatar || '',
|
||||
gender: user_info.gender || '',
|
||||
phone: user_info.phone || '',
|
||||
latitude: user_info.latitude||'',
|
||||
longitude: user_info.longitude||'',
|
||||
city: user_info.location || '',
|
||||
province: '', // 需要从用户信息中获取
|
||||
country: '' // 需要从用户信息中获取
|
||||
// 字段映射配置
|
||||
const field_mapping: Record<string, string> = {
|
||||
nickname: 'nickname',
|
||||
avatar: 'avatar_url',
|
||||
gender: 'gender',
|
||||
phone: 'phone',
|
||||
latitude: 'latitude',
|
||||
longitude: 'longitude',
|
||||
province: 'province',
|
||||
country:"country",
|
||||
city:"city",
|
||||
personal_profile: 'personal_profile',
|
||||
occupation: 'occupation',
|
||||
ntrp_level: 'ntrp_level'
|
||||
};
|
||||
|
||||
const response = await httpService.post<any>(API_CONFIG.USER.UPDATE, updateParams, {
|
||||
// 构建更新参数,只包含非空字段
|
||||
const updateParams: Record<string, string> = {};
|
||||
|
||||
// 循环处理所有字段
|
||||
Object.keys(field_mapping).forEach(key => {
|
||||
const value = user_info[key as keyof typeof user_info];
|
||||
if (value && typeof value === 'string' && value.trim() !== '') {
|
||||
updateParams[field_mapping[key]] = value.trim();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// 如果没有需要更新的字段,直接返回成功
|
||||
if (Object.keys(updateParams).length === 0) {
|
||||
console.log('没有需要更新的字段');
|
||||
return true;
|
||||
}
|
||||
|
||||
const response = await httpService.post<any>(API_CONFIG.USER.UPDATE, updateParams, {
|
||||
showLoading: true,
|
||||
loadingText: '保存中...'
|
||||
});
|
||||
@@ -440,19 +478,12 @@ export class UserService {
|
||||
static async upload_avatar(file_path: string): Promise<string> {
|
||||
try {
|
||||
// 先上传文件到服务器
|
||||
const uploadResponse = await Taro.uploadFile({
|
||||
url: `${API_CONFIG.BASE_URL}${API_CONFIG.UPLOAD.AVATAR}`,
|
||||
filePath: file_path,
|
||||
name: 'file'
|
||||
});
|
||||
const result = await uploadFiles.upload_oss_img(file_path)
|
||||
|
||||
const result = JSON.parse(uploadResponse.data) as { code: number; message: string; data: UploadResponseData };
|
||||
if (result.code === 0) {
|
||||
// 使用新的响应格式中的file_url字段
|
||||
return result.data.file_url;
|
||||
} else {
|
||||
throw new Error(result.message || '头像上传失败');
|
||||
}
|
||||
await this.save_user_info({ avatar: result.ossPath })
|
||||
|
||||
// 使用新的响应格式中的file_url字段
|
||||
return result.ossPath;
|
||||
} catch (error) {
|
||||
console.error('头像上传失败:', error);
|
||||
// 如果上传失败,返回默认头像
|
||||
|
||||
Reference in New Issue
Block a user