This commit is contained in:
张成
2025-09-07 10:06:00 +08:00
parent 955f1003dd
commit 4c36986ade
11 changed files with 403 additions and 220 deletions

View File

@@ -1,18 +1,14 @@
import { UserInfo, GameRecord } from '@/components/UserInfo';
import { UserInfo } from '@/components/UserInfo';
import Taro from '@tarojs/taro';
import { API_CONFIG, REQUEST_CONFIG } from '@/config/api';
import { API_CONFIG } from '@/config/api';
import httpService from './httpService';
// API响应接口
interface ApiResponse<T> {
code: number;
message: string;
data: T;
}
// 用户详情接口
interface UserDetailData {
id: number;
openid: string;
user_code: string;
user_code: string | null;
unionid: string;
session_key: string;
nickname: string;
@@ -24,10 +20,18 @@ interface UserDetailData {
language: string;
phone: string;
is_subscribed: string;
latitude: string;
longitude: string;
latitude: number;
longitude: number;
subscribe_time: string;
last_login_time: string;
create_time: string;
last_modify_time: string;
stats: {
followers_count: number;
following_count: number;
hosted_games_count: number;
participated_games_count: number;
};
}
// 更新用户信息参数接口
@@ -84,25 +88,23 @@ 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
const response = await httpService.post<UserDetailData>(API_CONFIG.USER.DETAIL, user_id ? { user_id } : {}, {
needAuth: false,
showLoading: false
});
if (response.data.code === 0) {
const userData = response.data.data;
if (response.code === 0) {
const userData = response.data;
return {
id: userData.user_code || user_id || '1',
nickname: userData.nickname || '用户',
avatar: userData.avatar_url || require('../../static/userInfo/default_avatar.svg'),
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
following: userData.stats?.following_count || 0,
friends: userData.stats?.followers_count || 0,
hosted: userData.stats?.hosted_games_count || 0,
participated: userData.stats?.participated_games_count || 0
},
tags: [
userData.city || '未知地区',
@@ -112,10 +114,12 @@ export class UserService {
bio: '这个人很懒,什么都没有写...',
location: userData.city || '未知地区',
occupation: '未知职业', // 需要其他接口获取
ntrp_level: 'NTRP 3.0' // 需要其他接口获取
ntrp_level: 'NTRP 3.0', // 需要其他接口获取
phone: userData.phone || '',
gender: userData.gender || ''
};
} else {
throw new Error(response.data.message || '获取用户信息失败');
throw new Error(response.message || '获取用户信息失败');
}
} catch (error) {
console.error('获取用户信息失败:', error);
@@ -123,7 +127,7 @@ export class UserService {
return {
id: user_id || '1',
nickname: '用户',
avatar: require('../../static/userInfo/default_avatar.svg'),
avatar: require('../static/userInfo/default_avatar.svg'),
join_date: '未知时间加入',
stats: {
following: 0,
@@ -135,56 +139,120 @@ export class UserService {
bio: '这个人很懒,什么都没有写...',
location: '未知地区',
occupation: '未知职业',
ntrp_level: 'NTRP 3.0'
ntrp_level: 'NTRP 3.0',
phone: '',
gender: ''
};
}
}
// 获取用户球局记录
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 get_hosted_games(user_id: string): Promise<any[]> {
try {
const response = await httpService.post<any>(API_CONFIG.USER.HOSTED_GAMES, {
user_id
}, {
needAuth: false,
showLoading: false
});
if (response.code === 0) {
return response.data.rows || [];
} else {
throw new Error(response.message || '获取主办球局失败');
}
} catch (error) {
console.error('获取主办球局失败:', error);
// 返回模拟数据
return [
{
id: 1,
title: '女生轻松双打',
dateTime: '明天(周五) 下午5点',
location: '仁恒河滨花园网球场',
distance: '3.5km',
registeredCount: 2,
maxCount: 4,
skillLevel: '2.0-2.5',
matchType: '双打',
images: [
require('../static/userInfo/game1.svg'),
require('../static/userInfo/game2.svg'),
require('../static/userInfo/game3.svg')
],
shinei: '室外'
}
];
}
}
// 获取用户参与的球局
static async get_participated_games(user_id: string): Promise<any[]> {
try {
const response = await httpService.post<any>(API_CONFIG.USER.PARTICIPATED_GAMES, {
user_id
}, {
needAuth: false,
showLoading: false
});
if (response.code === 0) {
return response.data.rows || [];
} else {
throw new Error(response.message || '获取参与球局失败');
}
} catch (error) {
console.error('获取参与球局失败:', error);
// 返回模拟数据
return [
{
id: 2,
title: '周末双打练习',
dateTime: '后天(周六) 上午10点',
location: '上海网球中心',
distance: '5.2km',
registeredCount: 6,
maxCount: 8,
skillLevel: '3.0-3.5',
matchType: '双打',
images: [
require('../static/userInfo/game2.svg'),
require('../static/userInfo/game3.svg')
],
shinei: '室内'
}
];
}
}
// 获取用户球局记录(兼容旧方法)
static async get_user_games(user_id: string, type: 'hosted' | 'participated'): Promise<any[]> {
if (type === 'hosted') {
return this.get_hosted_games(user_id);
} else {
return this.get_participated_games(user_id);
}
}
// 关注/取消关注用户
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);
});
try {
const endpoint = is_following ? API_CONFIG.USER.UNFOLLOW : API_CONFIG.USER.FOLLOW;
const response = await httpService.post<any>(endpoint, { user_id }, {
needAuth: false,
showLoading: true,
loadingText: is_following ? '取消关注中...' : '关注中...'
});
if (response.code === 0) {
return !is_following;
} else {
throw new Error(response.message || '操作失败');
}
} catch (error) {
console.error('关注操作失败:', error);
throw error;
}
}
// 保存用户信息
@@ -207,17 +275,16 @@ export class UserService {
country: '' // 需要从用户信息中获取
};
const response = await Taro.request<ApiResponse<any>>({
url: `${API_CONFIG.BASE_URL}${API_CONFIG.USER.UPDATE}`,
method: 'POST',
data: updateParams,
...REQUEST_CONFIG
const response = await httpService.post<any>(API_CONFIG.USER.UPDATE, updateParams, {
needAuth: false,
showLoading: true,
loadingText: '保存中...'
});
if (response.data.code === 0) {
if (response.code === 0) {
return true;
} else {
throw new Error(response.data.message || '更新用户信息失败');
throw new Error(response.message || '更新用户信息失败');
}
} catch (error) {
console.error('保存用户信息失败:', error);
@@ -225,6 +292,29 @@ export class UserService {
}
}
// 获取用户动态
static async get_user_activities(user_id: string, page: number = 1, limit: number = 10): Promise<any[]> {
try {
const response = await httpService.post<any>('/user/activities', {
user_id,
page,
limit
}, {
needAuth: false,
showLoading: false
});
if (response.code === 0) {
return response.data.activities || [];
} else {
throw new Error(response.message || '获取用户动态失败');
}
} catch (error) {
console.error('获取用户动态失败:', error);
return [];
}
}
// 上传头像
static async upload_avatar(file_path: string): Promise<string> {
try {
@@ -235,7 +325,7 @@ export class UserService {
name: 'file'
});
const result = JSON.parse(uploadResponse.data) as ApiResponse<UploadResponseData>;
const result = JSON.parse(uploadResponse.data) as { code: number; message: string; data: UploadResponseData };
if (result.code === 0) {
// 使用新的响应格式中的file_url字段
return result.data.file_url;
@@ -245,7 +335,7 @@ export class UserService {
} catch (error) {
console.error('头像上传失败:', error);
// 如果上传失败,返回默认头像
return require('../../static/userInfo/default_avatar.svg');
return require('../static/userInfo/default_avatar.svg');
}
}
}