首页只调用必须的接口

This commit is contained in:
张成
2025-12-08 13:32:12 +08:00
parent 175e5814e3
commit a8dca0dd71
10 changed files with 245 additions and 92 deletions

View File

@@ -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);