首页只调用必须的接口
This commit is contained in:
@@ -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 () {
|
||||
|
||||
Reference in New Issue
Block a user