2
0
Эх сурвалжийг харах

♻️ refactor(classroom): 重构IM和RTC token获取逻辑

- 替换ClassroomAPI为aliyunClient获取IM和RTC token
- 增加API响应状态检查和错误处理
- 移除重复的startAndPublishDefaultDevices调用

🐛 fix(classroom): 修复音视频设备发布逻辑

- 移除可能导致设备重复发布的冗余调用
- 优化音视频和屏幕共享状态切换逻辑
yourname 5 сар өмнө
parent
commit
a09505af99

+ 25 - 8
src/client/mobile/components/Classroom/useClassroom.ts

@@ -6,7 +6,7 @@ import { useParams } from 'react-router';
 import AliRtcEngine, { AliRtcSubscribeState, AliRtcVideoTrack } from 'aliyun-rtc-sdk';
 import AliRtcEngine, { AliRtcSubscribeState, AliRtcVideoTrack } from 'aliyun-rtc-sdk';
 import { toast } from 'react-toastify';
 import { toast } from 'react-toastify';
 import { User } from '../../hooks/AuthProvider';
 import { User } from '../../hooks/AuthProvider';
-import { classroomDataClient } from '@/client/api';
+import { classroomDataClient, aliyunClient } from '@/client/api';
 import "https://g.alicdn.com/apsara-media-box/imp-interaction/1.6.1/alivc-im.iife.js"
 import "https://g.alicdn.com/apsara-media-box/imp-interaction/1.6.1/alivc-im.iife.js"
 export enum Role {
 export enum Role {
   Teacher = 'admin',
   Teacher = 'admin',
@@ -127,7 +127,14 @@ export const useClassroom = ({ user }:{ user : User }) => {
       showMessage(`IM断开连接: ${code}`);
       showMessage(`IM断开连接: ${code}`);
       // 自动重连
       // 自动重连
       try {
       try {
-        const { token, nonce, timestamp } = await ClassroomAPI.getIMToken(userId, role);
+        const res = await aliyunClient.im_token.$post({
+          json: { role }
+        });
+        if(!res.ok) { 
+          const { message } = await res.json()
+          throw new Error(message)
+        }
+        const { token, nonce, timestamp } = await res.json()
         await imEngine.current!.login({
         await imEngine.current!.login({
           user: {
           user: {
             userId,
             userId,
@@ -406,7 +413,14 @@ export const useClassroom = ({ user }:{ user : User }) => {
 
 
     try {
     try {
       const { ImEngine: ImEngineClass } = window.AliVCInteraction;
       const { ImEngine: ImEngineClass } = window.AliVCInteraction;
-      const {appId, appSign, timestamp, nonce, token} = await ClassroomAPI.getIMToken(userId, role);
+      const res = await aliyunClient.im_token.$post({
+        json: { role }
+      });
+      if(!res.ok) { 
+        const { message } = await res.json()
+        throw new Error(message)
+      }
+      const {appId, appSign, timestamp, nonce, token} = await res.json();
       imEngine.current = ImEngineClass.createEngine();
       imEngine.current = ImEngineClass.createEngine();
       await imEngine.current.init({
       await imEngine.current.init({
         deviceId: 'xxxx',
         deviceId: 'xxxx',
@@ -674,8 +688,14 @@ export const useClassroom = ({ user }:{ user : User }) => {
       publishAudio = false,
       publishAudio = false,
       publishScreen = false,
       publishScreen = false,
     } = publishOptions || {};
     } = publishOptions || {};
-    
-    const {appId, token, timestamp} = await ClassroomAPI.getRTCToken(classId, userId);
+    const res = await aliyunClient.rtc_token.$post({
+      json: { channelId: classId }
+    });
+    if(!res.ok) { 
+      const { message } = await res.json()
+      throw new Error(message)
+    }
+    const { appId, token, timestamp } = await res.json()
     await aliRtcEngine.current.publishLocalVideoStream(publishVideo);
     await aliRtcEngine.current.publishLocalVideoStream(publishVideo);
     await aliRtcEngine.current.publishLocalAudioStream(publishAudio);
     await aliRtcEngine.current.publishLocalAudioStream(publishAudio);
     await aliRtcEngine.current.publishLocalScreenShareStream(publishScreen);
     await aliRtcEngine.current.publishLocalScreenShareStream(publishScreen);
@@ -715,7 +735,6 @@ export const useClassroom = ({ user }:{ user : User }) => {
         await aliRtcEngine.current?.publishLocalVideoStream(true)
         await aliRtcEngine.current?.publishLocalVideoStream(true)
    
    
       }
       }
-      await aliRtcEngine.current?.startAndPublishDefaultDevices()
       setIsCameraOn(!isCameraOn);
       setIsCameraOn(!isCameraOn);
     } catch (err) {
     } catch (err) {
       console.error('切换摄像头状态失败:', err);
       console.error('切换摄像头状态失败:', err);
@@ -737,7 +756,6 @@ export const useClassroom = ({ user }:{ user : User }) => {
       } else {
       } else {
         await aliRtcEngine.current?.publishLocalAudioStream(true);
         await aliRtcEngine.current?.publishLocalAudioStream(true);
       }
       }
-      await aliRtcEngine.current?.startAndPublishDefaultDevices();
       setIsAudioOn(!isAudioOn);
       setIsAudioOn(!isAudioOn);
     } catch (err) {
     } catch (err) {
       console.error('切换麦克风状态失败:', err);
       console.error('切换麦克风状态失败:', err);
@@ -763,7 +781,6 @@ export const useClassroom = ({ user }:{ user : User }) => {
           AliRtcVideoTrack.AliRtcVideoTrackScreen
           AliRtcVideoTrack.AliRtcVideoTrackScreen
         );
         );
       }
       }
-      await aliRtcEngine.current?.startAndPublishDefaultDevices()
       setIsScreenSharing(!isScreenSharing);
       setIsScreenSharing(!isScreenSharing);
     } catch (err) {
     } catch (err) {
       console.error('切换屏幕分享失败:', err);
       console.error('切换屏幕分享失败:', err);