11
This commit is contained in:
251
src/services/userService.ts
Normal file
251
src/services/userService.ts
Normal file
@@ -0,0 +1,251 @@
|
||||
import { UserInfo, GameRecord } from '@/components/UserInfo';
|
||||
import Taro from '@tarojs/taro';
|
||||
import { API_CONFIG, REQUEST_CONFIG } from '@/config/api';
|
||||
|
||||
// API响应接口
|
||||
interface ApiResponse<T> {
|
||||
code: number;
|
||||
message: string;
|
||||
data: T;
|
||||
}
|
||||
|
||||
// 用户详情接口
|
||||
interface UserDetailData {
|
||||
openid: string;
|
||||
user_code: string;
|
||||
unionid: string;
|
||||
session_key: string;
|
||||
nickname: string;
|
||||
avatar_url: string;
|
||||
gender: string;
|
||||
country: string;
|
||||
province: string;
|
||||
city: string;
|
||||
language: string;
|
||||
phone: string;
|
||||
is_subscribed: string;
|
||||
latitude: string;
|
||||
longitude: string;
|
||||
subscribe_time: string;
|
||||
last_login_time: string;
|
||||
}
|
||||
|
||||
// 更新用户信息参数接口
|
||||
interface UpdateUserParams {
|
||||
nickname: string;
|
||||
avatar_url: string;
|
||||
gender: string;
|
||||
phone: string;
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
city: string;
|
||||
province: string;
|
||||
country: string;
|
||||
}
|
||||
|
||||
// 上传响应接口
|
||||
interface UploadResponseData {
|
||||
create_time: string;
|
||||
last_modify_time: string;
|
||||
duration: string;
|
||||
thumbnail_url: string;
|
||||
view_count: string;
|
||||
download_count: string;
|
||||
is_delete: number;
|
||||
id: number;
|
||||
user_id: number;
|
||||
resource_type: string;
|
||||
file_name: string;
|
||||
original_name: string;
|
||||
file_path: string;
|
||||
file_url: string;
|
||||
file_size: number;
|
||||
mime_type: string;
|
||||
description: string;
|
||||
tags: string;
|
||||
is_public: string;
|
||||
width: number;
|
||||
height: number;
|
||||
uploadInfo: {
|
||||
success: boolean;
|
||||
name: string;
|
||||
path: string;
|
||||
ossPath: string;
|
||||
fileType: string;
|
||||
fileSize: number;
|
||||
originalName: string;
|
||||
suffix: string;
|
||||
storagePath: string;
|
||||
};
|
||||
}
|
||||
|
||||
// 用户服务类
|
||||
export class UserService {
|
||||
// 获取用户信息
|
||||
static async get_user_info(user_id?: string): Promise<UserInfo> {
|
||||
try {
|
||||
const response = await Taro.request<ApiResponse<UserDetailData>>({
|
||||
url: `${API_CONFIG.BASE_URL}${API_CONFIG.USER.DETAIL}`,
|
||||
method: 'POST',
|
||||
data: user_id ? { user_id } : {},
|
||||
...REQUEST_CONFIG
|
||||
});
|
||||
|
||||
if (response.data.code === 0) {
|
||||
const userData = response.data.data;
|
||||
return {
|
||||
id: userData.user_code || user_id || '1',
|
||||
nickname: userData.nickname || '用户',
|
||||
avatar: userData.avatar_url || require('../../static/userInfo/default_avatar.svg'),
|
||||
join_date: userData.subscribe_time ? `${new Date(userData.subscribe_time).getFullYear()}年${new Date(userData.subscribe_time).getMonth() + 1}月加入` : '未知时间加入',
|
||||
stats: {
|
||||
following: 0, // 这些数据需要其他接口获取
|
||||
friends: 0,
|
||||
hosted: 0,
|
||||
participated: 0
|
||||
},
|
||||
tags: [
|
||||
userData.city || '未知地区',
|
||||
userData.province || '未知省份',
|
||||
'NTRP 3.0' // 默认等级,需要其他接口获取
|
||||
],
|
||||
bio: '这个人很懒,什么都没有写...',
|
||||
location: userData.city || '未知地区',
|
||||
occupation: '未知职业', // 需要其他接口获取
|
||||
ntrp_level: 'NTRP 3.0' // 需要其他接口获取
|
||||
};
|
||||
} else {
|
||||
throw new Error(response.data.message || '获取用户信息失败');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取用户信息失败:', error);
|
||||
// 返回默认用户信息
|
||||
return {
|
||||
id: user_id || '1',
|
||||
nickname: '用户',
|
||||
avatar: require('../../static/userInfo/default_avatar.svg'),
|
||||
join_date: '未知时间加入',
|
||||
stats: {
|
||||
following: 0,
|
||||
friends: 0,
|
||||
hosted: 0,
|
||||
participated: 0
|
||||
},
|
||||
tags: ['未知地区', '未知职业', 'NTRP 3.0'],
|
||||
bio: '这个人很懒,什么都没有写...',
|
||||
location: '未知地区',
|
||||
occupation: '未知职业',
|
||||
ntrp_level: 'NTRP 3.0'
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 获取用户球局记录
|
||||
static async get_user_games(user_id: string, type: 'hosted' | 'participated'): Promise<GameRecord[]> {
|
||||
// 模拟API调用 - 这里应该调用真实的球局接口
|
||||
console.log(`获取用户 ${user_id} 的${type === 'hosted' ? '主办' : '参与'}球局`);
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
const mock_games: GameRecord[] = [
|
||||
{
|
||||
id: '1',
|
||||
title: type === 'hosted' ? '女生轻松双打' : '周末双打练习',
|
||||
date: '明天(周五)',
|
||||
time: '下午5点',
|
||||
duration: '2小时',
|
||||
location: '仁恒河滨花园网球场',
|
||||
type: '室外',
|
||||
distance: '3.5km',
|
||||
participants: [
|
||||
{ avatar: require('../../static/userInfo/user1.svg'), nickname: '用户1' },
|
||||
{ avatar: require('../../static/userInfo/user2.svg'), nickname: '用户2' }
|
||||
],
|
||||
max_participants: 4,
|
||||
current_participants: 2,
|
||||
level_range: '2.0 至 2.5',
|
||||
game_type: '双打',
|
||||
images: [
|
||||
require('../../static/userInfo/game1.svg'),
|
||||
require('../../static/userInfo/game2.svg'),
|
||||
require('../../static/userInfo/game3.svg')
|
||||
]
|
||||
}
|
||||
];
|
||||
resolve(mock_games);
|
||||
}, 300);
|
||||
});
|
||||
}
|
||||
|
||||
// 关注/取消关注用户
|
||||
static async toggle_follow(user_id: string, is_following: boolean): Promise<boolean> {
|
||||
// 模拟API调用 - 这里应该调用真实的关注接口
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
console.log(`用户 ${user_id} 关注状态变更:`, !is_following);
|
||||
resolve(!is_following);
|
||||
}, 200);
|
||||
});
|
||||
}
|
||||
|
||||
// 保存用户信息
|
||||
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: location.latitude,
|
||||
longitude: location.longitude,
|
||||
city: user_info.location || '',
|
||||
province: '', // 需要从用户信息中获取
|
||||
country: '' // 需要从用户信息中获取
|
||||
};
|
||||
|
||||
const response = await Taro.request<ApiResponse<any>>({
|
||||
url: `${API_CONFIG.BASE_URL}${API_CONFIG.USER.UPDATE}`,
|
||||
method: 'POST',
|
||||
data: updateParams,
|
||||
...REQUEST_CONFIG
|
||||
});
|
||||
|
||||
if (response.data.code === 0) {
|
||||
return true;
|
||||
} else {
|
||||
throw new Error(response.data.message || '更新用户信息失败');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('保存用户信息失败:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 上传头像
|
||||
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 = JSON.parse(uploadResponse.data) as ApiResponse<UploadResponseData>;
|
||||
if (result.code === 0) {
|
||||
// 使用新的响应格式中的file_url字段
|
||||
return result.data.file_url;
|
||||
} else {
|
||||
throw new Error(result.message || '头像上传失败');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('头像上传失败:', error);
|
||||
// 如果上传失败,返回默认头像
|
||||
return require('../../static/userInfo/default_avatar.svg');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user