浏览代码

✨ feat(exam): 添加累计收益率显示功能

- 新增totalProfitRate状态管理累计收益率数据
- 在回答记录更新时计算累计收益率(单日涨幅相加)
- 在考试重置时重置累计收益率数据
- 在考试卡片UI中添加累计收益率显示,红色表示正收益,绿色表示负收益
yourname 6 月之前
父节点
当前提交
0e3e5e05be
共有 1 个文件被更改,包括 22 次插入3 次删除
  1. 22 3
      src/client/mobile/components/Exam/ExamCard.tsx

+ 22 - 3
src/client/mobile/components/Exam/ExamCard.tsx

@@ -27,6 +27,7 @@ export default function ExamCard() {
   const [holdingCash, setHoldingCash] = useState('0');
   const [isStarted, setIsStarted] = useState(false);
   const [answerRecords, setAnswerRecords] = useState<AnswerRecord[]>([]);
+  const [totalProfitRate, setTotalProfitRate] = useState(0);
 
   const { data: classroomData, isLoading } = useQuery({
     queryKey: ['classroom', classroom],
@@ -89,6 +90,10 @@ export default function ExamCard() {
           }));
           
           setAnswerRecords(records);
+          
+          // 计算累计收益率(单日涨幅相加)
+          const totalRate = answers.reduce((sum, answer) => sum + (answer.profitPercent || 0), 0);
+          setTotalProfitRate(totalRate);
         }
       } catch (error) {
         console.error('获取用户回答记录失败:', error);
@@ -157,6 +162,10 @@ export default function ExamCard() {
               index: index + 1
             }));
             setAnswerRecords(records);
+            
+            // 计算累计收益率(单日涨幅相加)
+            const totalRate = answers.reduce((sum, answer) => sum + (answer.profitPercent || 0), 0);
+            setTotalProfitRate(totalRate);
           }
         );
       } catch (error) {
@@ -195,6 +204,10 @@ export default function ExamCard() {
               index: index + 1
             }));
             setAnswerRecords(records);
+            
+            // 计算累计收益率(单日涨幅相加)
+            const totalRate = answers.reduce((sum, answer) => sum + (answer.profitPercent || 0), 0);
+            setTotalProfitRate(totalRate);
           }
         );
       } catch (error) {
@@ -222,7 +235,7 @@ export default function ExamCard() {
   // 监听重开
   useEffect(() => {
     if (!client ) return;
-
+  
     const handleCleaned = () => {
       setCurrentDate('');
       setCurrentPrice('0');
@@ -230,8 +243,9 @@ export default function ExamCard() {
       setHoldingCash('0');
       setIsStarted(false);
       setAnswerRecords([]);
+      setTotalProfitRate(0);
     };
-
+  
     client.on('exam:cleaned', handleCleaned);
     return () => {
       client.off('exam:cleaned', handleCleaned);
@@ -318,8 +332,13 @@ export default function ExamCard() {
 
         {/* 信息显示 */}
         <div className="bg-white p-6 rounded-lg shadow-md">
-          <div className="grid grid-cols-1 gap-4 mb-4">
+          <div className="grid grid-cols-2 gap-4 mb-4">
             <div className="text-gray-600">昵称: {user?.username || '未知用户'}</div>
+            <div className="text-gray-600 text-right">
+              累计收益率: <span className={totalProfitRate >= 0 ? 'text-red-500' : 'text-green-500'}>
+                {totalProfitRate.toFixed(2)}%
+              </span>
+            </div>
           </div>
 
           {/* 表格头部 */}