diff --git a/src/game_pages/search/index.scss b/src/game_pages/search/index.scss index e1450ab..ad04b72 100644 --- a/src/game_pages/search/index.scss +++ b/src/game_pages/search/index.scss @@ -9,7 +9,7 @@ } .topSearch { - padding: 10px 16px 5px 12px; + padding: 10px 16px 10px 12px; display: flex; align-items: center; height: 44px; diff --git a/src/publish_pages/publishBall/index.tsx b/src/publish_pages/publishBall/index.tsx index 6bd8f29..a4e2787 100644 --- a/src/publish_pages/publishBall/index.tsx +++ b/src/publish_pages/publishBall/index.tsx @@ -422,7 +422,8 @@ const PublishBall: React.FC = () => { if (republish === "0") { Taro.navigateBack(); } else { - Taro.navigateTo({ + // 使用 redirectTo 替换当前页面,避免返回时回到发布页面 + Taro.redirectTo({ // @ts-expect-error: id url: `/game_pages/detail/index?id=${ id || 1 @@ -492,7 +493,8 @@ const PublishBall: React.FC = () => { republish === "0" ? (res as any).data?.id : (res as any).data?.[0]?.id; - Taro.navigateTo({ + // 使用 redirectTo 替换当前页面,避免返回时回到发布页面 + Taro.redirectTo({ // @ts-expect-error: id url: `/game_pages/detail/index?id=${ id || 1 diff --git a/src/user_pages/other/index.tsx b/src/user_pages/other/index.tsx index 73f75bf..db6e27c 100644 --- a/src/user_pages/other/index.tsx +++ b/src/user_pages/other/index.tsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from "react"; +import React, { useState, useEffect, useCallback } from "react"; import { View, Text, ScrollView, Image } from "@tarojs/components"; import ListContainer from "@/container/listContainer"; import Taro from "@tarojs/taro"; @@ -23,44 +23,36 @@ const OtherUserPage: React.FC = () => { // 获取页面参数 const instance = Taro.getCurrentInstance(); const user_id = instance.router?.params?.userid; - if (!user_id) { - Taro.showToast({ - title: "用户不存在", - icon: "none", - duration: 2000, - }); - Taro.navigateBack(); - } // 获取导航栏高度信息 const { statusNavbarHeightInfo } = useGlobalState() || {}; const { totalHeight = 98 } = statusNavbarHeightInfo || {}; - // 模拟用户数据 + // 用户数据 const [user_info, setUserInfo] = useState>({ - id: parseInt(user_id || "1") || 1, + id: user_id ? parseInt(user_id) : undefined, gender: "", - nickname: "网球爱好者", - avatar_url: require("@/static/userInfo/default_avatar.svg"), - join_date: "2024年3月加入", + nickname: "", + avatar_url: "", + join_date: "", stats: { following_count: 0, followers_count: 0, hosted_games_count: 0, participated_games_count: 0, }, - tags: ["北京朝阳", "金融从业者", "NTRP 3.5"], - bio: "热爱网球的金融从业者,周末喜欢约球\n技术还在提升中,欢迎一起切磋\n平时在朝阳公园附近活动", - city: "北京", - district: "朝阳", - occupation: "金融从业者", - ntrp_level: "NTRP 3.5", + tags: [], + bio: "", + city: "", + district: "", + occupation: "", + ntrp_level: "", is_following: false, ongoing_games: [], personal_profile: "", }); - // 模拟球局数据 + // 球局数据 const [game_records, setGameRecords] = useState([]); // 往期球局 const [ended_game_records, setEndedGameRecords] = useState([]); @@ -77,6 +69,19 @@ const OtherUserPage: React.FC = () => { const [collapseProfile, setCollapseProfile] = useState(false); + // 进入页面时检查 user_id,只在组件挂载时执行一次 + useEffect(() => { + if (!user_id) { + Taro.showToast({ + title: "用户不存在", + icon: "none", + duration: 2000, + }); + Taro.navigateBack(); + return; + } + }, []); // 空依赖数组,确保只在进入时执行一次 + // 页面加载时获取用户信息 useEffect(() => { const load_user_data = async () => { @@ -126,7 +131,8 @@ const OtherUserPage: React.FC = () => { load_user_data(); }, [user_id]); - const classifyGameRecords = ( + // 分类球局数据(使用 useCallback 包装,避免每次渲染都创建新函数) + const classifyGameRecords = useCallback(( game_records: GameRecord[] ): { notEndGames: GameRecord[]; finishedGames: GameRecord[] } => { const now = new Date().getTime(); @@ -144,13 +150,15 @@ const OtherUserPage: React.FC = () => { finishedGames: [] as GameRecord[], } ); - }; + }, []); - const load_game_data = async () => { + // 加载球局数据(使用 useCallback 包装,避免每次渲染都创建新函数) + const load_game_data = useCallback(async () => { + if (!user_id) return; try { set_loading(true); const games_data = await UserService.get_user_games( - user_id || "", + user_id, active_tab ); const sorted_games = games_data.sort((a, b) => { @@ -172,21 +180,22 @@ const OtherUserPage: React.FC = () => { } finally { set_loading(false); } - }; + }, [user_id, active_tab, classifyGameRecords]); useEffect(() => { load_game_data(); - }, [active_tab]); + }, [load_game_data]); - // 处理关注/取消关注 - const handle_follow = async () => { + // 处理关注/取消关注(使用 useCallback 包装,避免每次渲染都创建新函数) + const handle_follow = useCallback(async () => { + if (!user_info.id) return; try { const new_follow_status = await UserService.toggle_follow( - user_info.id || "", + user_info.id, is_following ); setIsFollowing(new_follow_status); - fetchUserInfo() + fetchUserInfo(); Taro.showToast({ title: new_follow_status ? "关注成功" : "已取消关注", icon: "success", @@ -199,24 +208,26 @@ const OtherUserPage: React.FC = () => { icon: "none", }); } - }; + }, [user_info.id, is_following, fetchUserInfo]); - // 处理发送消息 - const handle_send_message = () => { + // 处理发送消息(使用 useCallback 包装,避免每次渲染都创建新函数) + const handle_send_message = useCallback(() => { + if (!user_info.id) return; Taro.navigateTo({ - url: `/mode_user/message/chat/index?user_id=${user_info.id || "" - }&nickname=${user_info.nickname || ""}`, + url: `/mode_user/message/chat/index?user_id=${user_info.id}&nickname=${user_info.nickname || ""}`, }); - }; + }, [user_info.id, user_info.nickname]); - const handleOnTab = (tab) => { - setActiveTab(tab) - } + // 处理标签页切换(使用 useCallback 包装,避免每次渲染都创建新函数) + const handleOnTab = useCallback((tab) => { + setActiveTab(tab); + }, []); - const handleScroll = (event: any) => { + // 处理滚动事件(使用 useCallback 包装,避免每次渲染都创建新函数) + const handleScroll = useCallback((event: any) => { const scrollData = event.detail; setCollapseProfile(scrollData.scrollTop > 1); - } + }, []); // 处理球局详情 // const handle_game_detail = (game_id: string) => {