修复个人页每次跳转都 返回问题

This commit is contained in:
张成
2025-11-28 23:14:31 +08:00
parent eb7c9983f5
commit f39aa39b73
3 changed files with 58 additions and 45 deletions

View File

@@ -9,7 +9,7 @@
} }
.topSearch { .topSearch {
padding: 10px 16px 5px 12px; padding: 10px 16px 10px 12px;
display: flex; display: flex;
align-items: center; align-items: center;
height: 44px; height: 44px;

View File

@@ -422,7 +422,8 @@ const PublishBall: React.FC = () => {
if (republish === "0") { if (republish === "0") {
Taro.navigateBack(); Taro.navigateBack();
} else { } else {
Taro.navigateTo({ // 使用 redirectTo 替换当前页面,避免返回时回到发布页面
Taro.redirectTo({
// @ts-expect-error: id // @ts-expect-error: id
url: `/game_pages/detail/index?id=${ url: `/game_pages/detail/index?id=${
id || 1 id || 1
@@ -492,7 +493,8 @@ const PublishBall: React.FC = () => {
republish === "0" republish === "0"
? (res as any).data?.id ? (res as any).data?.id
: (res as any).data?.[0]?.id; : (res as any).data?.[0]?.id;
Taro.navigateTo({ // 使用 redirectTo 替换当前页面,避免返回时回到发布页面
Taro.redirectTo({
// @ts-expect-error: id // @ts-expect-error: id
url: `/game_pages/detail/index?id=${ url: `/game_pages/detail/index?id=${
id || 1 id || 1

View File

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