import React, { useState, useEffect } from 'react'; import { View, Text, Image, ScrollView } from '@tarojs/components'; import Taro from '@tarojs/taro'; import './index.scss'; import GuideBar from '@/components/GuideBar' import { UserInfoCard, UserInfo } from '@/components/UserInfo/index' import { UserService } from '@/services/userService' import ListContainer from '@/container/listContainer' import { TennisMatch } from '../../../../types/list/types' import { withAuth } from '@/components'; const MyselfPage: React.FC = () => { // 获取页面参数 const instance = Taro.getCurrentInstance(); const user_id = instance.router?.params?.userid || ''; // 判断是否为当前用户 const is_current_user = !user_id; // 用户信息状态 const [user_info, set_user_info] = useState({ id: '1', nickname: '加载中...', avatar: require('../../../static/userInfo/default_avatar.svg'), join_date: '加载中...', stats: { following: 0, friends: 0, hosted: 0, participated: 0 }, bio: '加载中...', location: '加载中...', occupation: '加载中...', ntrp_level: 'NTRP 3.0' }); // 球局记录状态 const [game_records, set_game_records] = useState([]); const [loading, set_loading] = useState(true); // 关注状态 const [is_following, setIsFollowing] = useState(false); // 当前激活的标签页 const [active_tab, setActiveTab] = useState<'hosted' | 'participated'>('hosted'); // 加载用户数据 const load_user_data = async () => { try { set_loading(true); // 获取用户信息(包含统计数据) const user_data = await UserService.get_user_info(user_id); set_user_info(user_data); // 获取球局记录 let games_data; if (active_tab === 'hosted') { games_data = await UserService.get_hosted_games(user_id); } else { games_data = await UserService.get_participated_games(user_id); } set_game_records(games_data); } catch (error) { console.error('加载用户数据失败:', error); Taro.showToast({ title: '加载失败,请重试', icon: 'error', duration: 2000 }); } finally { set_loading(false); } }; // 页面加载时获取数据 useEffect(() => { load_user_data(); }, [user_id]); // 切换标签页时重新加载球局数据 useEffect(() => { if (!loading) { load_game_data(); } }, [active_tab]); // 加载球局数据 const load_game_data = async () => { try { let games_data; if (active_tab === 'hosted') { games_data = await UserService.get_hosted_games(user_id); } else { games_data = await UserService.get_participated_games(user_id); } set_game_records(games_data); } catch (error) { console.error('加载球局数据失败:', error); } }; // 处理关注/取消关注 const handle_follow = async () => { try { const new_following_state = await UserService.toggle_follow(user_id, is_following); setIsFollowing(new_following_state); Taro.showToast({ title: new_following_state ? '关注成功' : '已取消关注', icon: 'success', duration: 1500 }); } catch (error) { console.error('关注操作失败:', error); Taro.showToast({ title: '操作失败,请重试', icon: 'error', duration: 2000 }); } }; // 处理球局订单 const handle_game_orders = () => { Taro.navigateTo({ url: '/mod_user/pages/orders/index' }); }; // 处理收藏 const handle_favorites = () => { Taro.navigateTo({ url: '/mod_user/pages/favorites/index' }); }; return ( {/* 主要内容 */} {/* 用户信息区域 */} {loading ? ( 加载中... ) : ( )} {/* 球局订单和收藏功能 */} 我的订单 收藏 {/* 球局类型标签页 */} setActiveTab('hosted')}> 我主办的 setActiveTab('participated')}> 我参与的 {/* 球局列表 */} ); }; export default withAuth(MyselfPage);