소스 검색

🐛 fix(classroom): 优化课堂消息发送流程

- 修改文本消息发送逻辑,先发送IM消息再保存数据库,确保权限检查优先
- 修改图片消息发送逻辑,采用先IM后数据库的串行流程
- 移除Promise.allSettled并行操作,避免权限检查不及时的问题
- 数据库保存失败时改为静默处理,不影响用户体验但记录错误日志
- 统一消息发送错误处理流程,确保禁言等权限检查能正确生效
yourname 6 달 전
부모
커밋
effce55842
1개의 변경된 파일32개의 추가작업 그리고 47개의 파일을 삭제
  1. 32 47
      src/client/mobile/components/Classroom/useClassroom.ts

+ 32 - 47
src/client/mobile/components/Classroom/useClassroom.ts

@@ -878,6 +878,15 @@ export const useClassroom = ({ user }:{ user : User }) => {
     }
 
     try {
+      // 先发送IM消息(包含禁言等权限检查)
+      await imMessageManager.current.sendGroupMessage({
+        groupId: classId,
+        data: msgText,
+        type: 88888,
+        level: NORMAL,
+      });
+      
+      // IM发送成功后再保存到数据库
       const message: Message = {
         type: 'text',
         content: msgText,
@@ -887,32 +896,16 @@ export const useClassroom = ({ user }:{ user : User }) => {
         timestamp: Date.now()
       };
       
-      // 并行执行数据库保存和IM发送
-      const [dbResult, imResult] = await Promise.allSettled([
-        saveMessageToDatabase(message),
-        retryOperation(() => 
-          imMessageManager.current!.sendGroupMessage({
-            groupId: classId,
-            data: msgText,
-            type: 88888,
-            level: NORMAL,
-          })
-        )
-      ]);
+      try {
+        await saveMessageToDatabase(message);
+      } catch (dbError) {
+        console.error('数据库保存失败(消息已发送):', dbError);
+        // 静默失败,不影响用户体验
+      }
       
       setMsgText('');
       setErrorMessage('');
       
-      // 如果数据库保存失败,记录日志但继续
-      if (dbResult.status === 'rejected') {
-        console.error('数据库保存失败(消息已发送):', dbResult.reason);
-      }
-      
-      // 如果IM发送失败,抛出错误
-      if (imResult.status === 'rejected') {
-        throw imResult.reason;
-      }
-      
     } catch (err: any) {
       // 检查是否为禁言错误 (错误码442)
       if (err.message?.includes('code:442') || err.message?.includes('not allowed to send message')) {
@@ -927,7 +920,18 @@ export const useClassroom = ({ user }:{ user : User }) => {
     if (!imMessageManager.current || !classId) return;
 
     try {
-      // 保存消息到数据库,保存fileId到fileId字段
+      // 先发送IM消息(包含禁言等权限检查)
+      await imMessageManager.current!.sendGroupMessage({
+        groupId: classId,
+        data: JSON.stringify({
+          type: 'image',
+          fileId: fileId
+        }),
+        type: 88895, // 使用单一消息类型用于图片
+        level: NORMAL,
+      });
+      
+      // IM发送成功后再保存到数据库
       const message: Message = {
         type: 'image',
         content: '[图片]', // 内容字段保存描述文本
@@ -938,30 +942,11 @@ export const useClassroom = ({ user }:{ user : User }) => {
         fileId: fileId // 新增fileId字段
       };
       
-      // 并行执行数据库保存和IM发送
-      const [dbResult, imResult] = await Promise.allSettled([
-        saveMessageToDatabase(message),
-        retryOperation(() => 
-          imMessageManager.current!.sendGroupMessage({
-            groupId: classId,
-            data: JSON.stringify({
-              type: 'image',
-              fileId: fileId
-            }),
-            type: 88895, // 使用单一消息类型用于图片
-            level: NORMAL,
-          })
-        )
-      ]);
-      
-      // 如果数据库保存失败,记录日志但继续
-      if (dbResult.status === 'rejected') {
-        console.error('数据库保存失败(图片消息已发送):', dbResult.reason);
-      }
-      
-      // 如果IM发送失败,抛出错误
-      if (imResult.status === 'rejected') {
-        throw imResult.reason;
+      try {
+        await saveMessageToDatabase(message);
+      } catch (dbError) {
+        console.error('数据库保存失败(图片消息已发送):', dbError);
+        // 静默失败,不影响用户体验
       }
       
     } catch (err: any) {