首页只调用必须的接口
This commit is contained in:
@@ -231,13 +231,13 @@ const HomeNavbar = (props: IProps) => {
|
||||
|
||||
// 处理城市切换(仅刷新数据,不保存缓存)
|
||||
const handleCityChangeWithoutCache = async () => {
|
||||
// 切换城市后,同时更新两个列表接口获取数据
|
||||
// 切换城市后,同时更新两个列表接口获取数据,传入当前的 area
|
||||
if (refreshBothLists) {
|
||||
await refreshBothLists();
|
||||
await refreshBothLists(area);
|
||||
}
|
||||
// 更新球局数量
|
||||
// 更新球局数量,传入当前的 area,确保接口请求的地址与界面显示一致
|
||||
if (fetchGetGamesCount) {
|
||||
await fetchGetGamesCount();
|
||||
await fetchGetGamesCount(area);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -2,9 +2,9 @@ import React, { useState, useEffect, useCallback, memo } from "react";
|
||||
import { View, Image, Text } from "@tarojs/components";
|
||||
import { requireLoginWithPhone } from "@/utils/helper";
|
||||
import Taro from "@tarojs/taro";
|
||||
import { useUserInfo, useUserActions } from "@/store/userStore";
|
||||
import { useUserInfo, useUserActions, useLastTestResult } from "@/store/userStore";
|
||||
// import { getCurrentFullPath } from "@/utils";
|
||||
import evaluateService, { StageType } from "@/services/evaluateService";
|
||||
import { StageType } from "@/services/evaluateService";
|
||||
import { waitForAuthInit } from "@/utils/authInit";
|
||||
import DocCopy from "@/static/ntrp/ntrp_doc_copy.svg";
|
||||
import ArrowRight from "@/static/ntrp/ntrp_arrow_right_color.svg";
|
||||
@@ -19,15 +19,16 @@ function NTRPTestEntryCard(props: {
|
||||
type: EvaluateScene;
|
||||
evaluateCallback?: EvaluateCallback;
|
||||
}) {
|
||||
const [testFlag, setTestFlag] = useState(false);
|
||||
const [hasTestInLastMonth, setHasTestInLastMonth] = useState(false);
|
||||
const { type, evaluateCallback } = props;
|
||||
const userInfo = useUserInfo();
|
||||
const { setCallback } = useEvaluate();
|
||||
const { fetchUserInfo } = useUserActions();
|
||||
const { fetchUserInfo, fetchLastTestResult } = useUserActions();
|
||||
// 使用全局状态中的测试结果,避免重复调用接口
|
||||
const lastTestResult = useLastTestResult();
|
||||
|
||||
console.log(userInfo);
|
||||
|
||||
// 从全局状态中获取测试结果,如果不存在则调用接口(使用请求锁避免重复调用)
|
||||
useEffect(() => {
|
||||
const init = async () => {
|
||||
// 先等待静默登录完成
|
||||
@@ -36,15 +37,17 @@ function NTRPTestEntryCard(props: {
|
||||
if (!userInfo.id) {
|
||||
await fetchUserInfo();
|
||||
}
|
||||
// 获取测试结果
|
||||
const res = await evaluateService.getLastResult();
|
||||
if (res.code === 0) {
|
||||
setTestFlag(res.data.has_test_record);
|
||||
setHasTestInLastMonth(res.data.has_test_in_last_month);
|
||||
// 如果全局状态中没有测试结果,则调用接口(使用请求锁,多个组件同时调用时只会请求一次)
|
||||
if (!lastTestResult) {
|
||||
await fetchLastTestResult();
|
||||
}
|
||||
};
|
||||
init();
|
||||
}, [userInfo]);
|
||||
}, [userInfo.id, fetchUserInfo, fetchLastTestResult, lastTestResult]);
|
||||
|
||||
// 从全局状态中计算标志位
|
||||
const testFlag = lastTestResult?.has_test_record || false;
|
||||
const hasTestInLastMonth = lastTestResult?.has_test_in_last_month || false;
|
||||
|
||||
const handleTest = useCallback(
|
||||
function () {
|
||||
|
||||
@@ -6,7 +6,7 @@ import "./index.scss";
|
||||
import { EditModal } from "@/components";
|
||||
import { UserService, PickerOption } from "@/services/userService";
|
||||
import { PopupPicker } from "@/components/Picker/index";
|
||||
import { useUserActions, useNicknameChangeStatus } from "@/store/userStore";
|
||||
import { useUserActions, useNicknameChangeStatus, useLastTestResult } from "@/store/userStore";
|
||||
import { UserInfoType } from "@/services/userService";
|
||||
import {
|
||||
useCities,
|
||||
@@ -15,7 +15,6 @@ import {
|
||||
} from "@/store/pickerOptionsStore";
|
||||
import { formatNtrpDisplay } from "@/utils/helper";
|
||||
import { useGlobalState } from "@/store/global";
|
||||
import evaluateService from "@/services/evaluateService";
|
||||
|
||||
// 用户信息接口
|
||||
// export interface UserInfo {
|
||||
@@ -83,9 +82,10 @@ const UserInfoCardComponent: React.FC<UserInfoCardProps> = ({
|
||||
}) => {
|
||||
const nickname_change_status = useNicknameChangeStatus();
|
||||
const { setShowGuideBar } = useGlobalState();
|
||||
const { updateUserInfo, updateNickname } = useUserActions();
|
||||
const { updateUserInfo, updateNickname, fetchLastTestResult } = useUserActions();
|
||||
const ntrpLevels = useNtrpLevels();
|
||||
const [ntrpTested, setNtrpTested] = useState(false);
|
||||
// 使用全局状态中的测试结果,避免重复调用接口
|
||||
const lastTestResult = useLastTestResult();
|
||||
|
||||
// 使用 useRef 记录上一次的 user_info,只在真正变化时打印
|
||||
const prevUserInfoRef = useRef<Partial<UserInfoType>>();
|
||||
@@ -98,15 +98,14 @@ const UserInfoCardComponent: React.FC<UserInfoCardProps> = ({
|
||||
console.log("UserInfoCard 用户信息变化:", user_info);
|
||||
prevUserInfoRef.current = user_info;
|
||||
}
|
||||
const getLastResult = async () => {
|
||||
// 获取测试结果
|
||||
const res = await evaluateService.getLastResult();
|
||||
if (res.code === 0) {
|
||||
setNtrpTested(res.data.has_test_in_last_month);
|
||||
}
|
||||
};
|
||||
getLastResult();
|
||||
}, [user_info]);
|
||||
// 如果全局状态中没有测试结果,则调用接口(使用请求锁,多个组件同时调用时只会请求一次)
|
||||
if (!lastTestResult && user_info?.id) {
|
||||
fetchLastTestResult();
|
||||
}
|
||||
}, [user_info?.id, lastTestResult, fetchLastTestResult]);
|
||||
|
||||
// 从全局状态中获取测试状态
|
||||
const ntrpTested = lastTestResult?.has_test_in_last_month || false;
|
||||
|
||||
// 编辑个人简介弹窗状态
|
||||
const [edit_modal_visible, setEditModalVisible] = useState(false);
|
||||
|
||||
Reference in New Issue
Block a user