2
0
Просмотр исходного кода

♻️ refactor(exam): 统一userId数据类型为number

- 修改ExamAdmin.tsx中userId的使用方式,将其转为字符串后作为Map的键
- 调整ExamCard.tsx中调用answerManagement方法时userId的参数类型,移除String()转换
- 更新useSocketClient.ts中storeAnswer和getUserAnswers方法的userId参数类型为number
- 修改types.ts中CumulativeResult接口的userId类型为number
- 优化exam.service.ts中存储答案时的数据结构,移除冗余的username字段
yourname 6 месяцев назад
Родитель
Сommit
487d994721

+ 4 - 4
src/client/mobile/components/Exam/ExamAdmin.tsx

@@ -281,18 +281,18 @@ export default function ExamAdmin() {
         const profitAmount = answer.profitAmount || 0;
         const profitPercent = answer.profitPercent || 0;
 
-        if (!userResults.has(userId)) {
-          userResults.set(userId, {
+        if (!userResults.has(userId.toString())) {
+          userResults.set(userId.toString(), {
             userId,
             totalProfitAmount: 0,
             totalProfitPercent: 0
           });
         }
 
-        const currentResult = userResults.get(userId)!;
+        const currentResult = userResults.get(userId.toString())!;
         currentResult.totalProfitAmount += profitAmount;
         currentResult.totalProfitPercent += profitPercent;
-        userResults.set(userId, currentResult);
+        userResults.set(userId.toString(), currentResult);
       });
     });
 

+ 5 - 5
src/client/mobile/components/Exam/ExamCard.tsx

@@ -72,7 +72,7 @@ export default function ExamCard() {
     // 获取用户回答记录
     if (user?.id) {
       try {
-        const answers = await answerManagement.getUserAnswers(classroom, String(user.id));
+        const answers = await answerManagement.getUserAnswers(classroom, user.id);
         if (answers && answers.length > 0) {
           const lastAnswer = answers[answers.length - 1];
           setHoldingStock(lastAnswer.holdingStock);
@@ -136,7 +136,7 @@ export default function ExamCard() {
         date: currentDate,
         holdingStock: '1',
         holdingCash: '0',
-        userId: String(user.id),
+        userId: user.id,
         price: currentPrice
       };
       
@@ -144,7 +144,7 @@ export default function ExamCard() {
         await answerManagement.storeAnswer(
           classroom as string,
           currentDate,
-          String(user.id),
+          user.id,
           answer,
           (answers) => {
             const records = answers.map((answer: Answer, index: number): AnswerRecord => ({
@@ -174,7 +174,7 @@ export default function ExamCard() {
         date: currentDate,
         holdingStock: '0',
         holdingCash: '1',
-        userId: String(user.id),
+        userId: user.id,
         price: currentPrice
       };
       
@@ -182,7 +182,7 @@ export default function ExamCard() {
         await answerManagement.storeAnswer(
           classroom as string,
           currentDate,
-          String(user.id),
+          user.id,
           answer,
           (answers) => {
             const records = answers.map((answer: Answer, index: number): AnswerRecord => ({

+ 2 - 2
src/client/mobile/components/Exam/hooks/useSocketClient.ts

@@ -135,7 +135,7 @@ export function useSocketClient(roomId: string | null) {
   // }, [client]);
 
   // 存储答案
-  const storeAnswer = useCallback(async (roomId: string, questionId: string, userId: string, answer: QuizContent, callback?: (success: Answer[]) => void) => {
+  const storeAnswer = useCallback(async (roomId: string, questionId: string, userId: number, answer: QuizContent, callback?: (success: Answer[]) => void) => {
     if (!client) return;
 
     return handleAsyncOperation(async () => {
@@ -309,7 +309,7 @@ export function useSocketClient(roomId: string | null) {
   };
 
   // 获取用户答案
-  const getUserAnswers = useCallback(async (roomId: string, userId: string): Promise<Answer[]> => {
+  const getUserAnswers = useCallback(async (roomId: string, userId: number): Promise<Answer[]> => {
     if (!client || !roomId || !userId) return Promise.resolve([]);
 
     return handleAsyncOperation(async () => {

+ 1 - 1
src/client/mobile/components/Exam/types.ts

@@ -57,7 +57,7 @@ export interface ClassroomData {
 
 // 累计结果
 export interface CumulativeResult {
-  userId: string;
+  userId: number;
   totalProfitAmount: number;
   totalProfitPercent: number;
 } 

+ 1 - 1
src/server/socket/services/exam.service.ts

@@ -73,7 +73,7 @@ export class ExamService {
       if (!socket.user) throw new Error('用户未认证');
       
       const user = socket.user;
-      await redisService.storeAnswer(roomId, user.id, questionId, { ...answer, username: user.username });
+      await redisService.storeAnswer(roomId, user.id, questionId, answer);
       
       socket.to(roomId).emit('exam:answerUpdated', {
         roomId, questionId, userId: user.id, username: user.username