添加最后一个 月 30天的 判定

This commit is contained in:
张成
2025-11-20 18:30:42 +08:00
parent 07c41e7fa9
commit 581743ffbf
4 changed files with 57 additions and 17 deletions

View File

@@ -19,6 +19,7 @@ function NTRPTestEntryCard(props: {
evaluateCallback?: EvaluateCallback;
}) {
const [testFlag, setTestFlag] = useState(false);
const [hasTestInLastMonth, setHasTestInLastMonth] = useState(false);
const { type, evaluateCallback } = props;
const userInfo = useUserInfo();
const { setCallback } = useEvaluate();
@@ -36,7 +37,10 @@ function NTRPTestEntryCard(props: {
}
// 获取测试结果
const res = await evaluateService.getLastResult();
setTestFlag(res.code === 0 && res.data.has_ntrp_level);
if (res.code === 0) {
setTestFlag(res.data.has_ntrp_level);
setHasTestInLastMonth(res.data.has_test_in_last_month);
}
};
init();
}, [userInfo.id]);
@@ -115,6 +119,11 @@ function NTRPTestEntryCard(props: {
[setCallback, testFlag, type, evaluateCallback]
);
// 如果最近一个月有测试记录,则不展示
if (hasTestInLastMonth) {
return null;
}
return type === EvaluateScene.list ? (
<View className={styles.higher} onClick={handleTest}>
<View className={styles.desc}>

View File

@@ -3,10 +3,12 @@ import ListCard from "@/components/ListCard";
import ListLoadError from "@/components/ListLoadError";
import ListCardSkeleton from "@/components/ListCardSkeleton";
import { useReachBottom } from "@tarojs/taro";
import { useUserInfo } from "@/store/userStore";
import { useUserInfo, useUserActions } from "@/store/userStore";
import { setStorage, getStorage } from "@/store/storage";
import { NTRPTestEntryCard } from "@/components";
import { EvaluateScene } from "@/store/evaluateStore";
import evaluateService from "@/services/evaluateService";
import { waitForAuthInit } from "@/utils/authInit";
import "./index.scss";
import { useRef, useEffect, useState, useMemo } from "react";
@@ -34,8 +36,10 @@ const ListContainer = (props) => {
const [showNumber, setShowNumber] = useState(0);
const [showSkeleton, setShowSkeleton] = useState(false);
const [hasTestInLastMonth, setHasTestInLastMonth] = useState(false);
const userInfo = useUserInfo();
const { fetchUserInfo } = useUserActions();
useReachBottom(() => {
// 加载更多方法
@@ -85,6 +89,25 @@ const ListContainer = (props) => {
};
}, []);
// 获取测试结果,判断最近一个月是否有测试记录
useEffect(() => {
const init = async () => {
if (!evaluateFlag) return;
// 先等待静默登录完成
await waitForAuthInit();
// 然后再获取用户信息
if (!userInfo.id) {
await fetchUserInfo();
}
// 获取测试结果
const res = await evaluateService.getLastResult();
if (res.code === 0) {
setHasTestInLastMonth(res.data.has_test_in_last_month);
}
};
init();
}, [evaluateFlag, userInfo.id]);
if (error) {
return <ListLoadError reload={reload} />;
}
@@ -99,32 +122,27 @@ const ListContainer = (props) => {
);
};
// 对于没有ntrp等级的用户每个月展示一次, 插在第个位置
// 对于没有ntrp等级的用户每个月展示一次, 插在第个位置后面
function insertEvaluateCard(list) {
if (!evaluateFlag) return list;
if (!list || list.length === 0) {
return list;
}
// if (userInfo.ntrp_level) {
// return list;
// }
// const lastShowTime = getStorage("list_evaluate_card");
// if (!lastShowTime) {
// setStorage("list_evaluate_card", Date.now());
// }
// if (Date.now() - Number(lastShowTime) < 30 * 24 * 60 * 60 * 1000) {
// return list;
// }
if (list.length <= 3) {
// 如果最近一个月有测试记录,则不插入 card
if (hasTestInLastMonth) {
return list;
}
if (list.length <= 2) {
return [...list, { type: "evaluateCard" }];
}
const [item1, item2, item3, ...rest] = list;
return [item1, item2, item3, { type: "evaluateCard" }, ...rest];
const [item1, item2, ...rest] = list;
return [item1, item2, { type: "evaluateCard" }, ...rest];
}
const memoizedList = useMemo(
() => insertEvaluateCard(data),
[evaluateFlag, data, userInfo.ntrp_level]
[evaluateFlag, data, hasTestInLastMonth]
);
// 渲染列表

View File

@@ -93,6 +93,7 @@ export interface LastTimeTestResult {
has_ntrp_level: boolean;
user_ntrp_level: string;
last_test_result: TestResult;
has_test_in_last_month: boolean; // 最近一个月是否有测试记录
}
class EvaluateService {