Selaa lähdekoodia

课堂增加支持从链接直接自动登录

yourname 7 kuukautta sitten
vanhempi
sitoutus
441f32c71b

+ 2 - 2
client/mobile/components/Classroom/ClassroomProvider.tsx

@@ -15,8 +15,8 @@ export const ClassroomProvider: React.FC<{children: React.ReactNode}> = ({ child
       classroom.setClassId(classId);
     }
     
-    if (pathRole && ['teacher', 'student'].includes(pathRole)) {
-      classroom.setRole(pathRole === 'teacher' ? Role.Teacher : Role.Student);
+    if (pathRole && (pathRole === Role.Teacher || pathRole === Role.Student)) {
+      classroom.setRole(pathRole === Role.Teacher ? Role.Teacher : Role.Student);
     }
   }, [classId, pathRole]);
 

+ 35 - 18
client/mobile/components/Classroom/useClassroom.ts

@@ -421,7 +421,10 @@ export const useClassroom = () => {
 
   // 课堂操作方法
   const login = async (userId: string): Promise<void> => {
-    if(!role) return;
+    if(!role) {
+      showToast('error', '角色不存在');
+      return;
+    }
 
     try {
       const { ImEngine: ImEngineClass } = window.AliVCInteraction;
@@ -462,32 +465,34 @@ export const useClassroom = () => {
     }
   };
 
-  const joinClass = async (classId?: string): Promise<void> => {
+  const joinClass = async (classId: string): Promise<void> => {
     if (!imEngine.current || !aliRtcEngine.current) return;
     
-    // 优先使用URL参数中的classId和role
-    const { id: pathClassId, role: pathRole } = useParams();
-    const finalClassId = (classId || pathClassId) as string;
-    if (pathRole && ['teacher', 'student'].includes(pathRole)) {
-      setRole(pathRole === 'teacher' ? Role.Teacher : Role.Student);
-    }
+    // // 优先使用URL参数中的classId和role
+    // const { id: pathClassId, role: pathRole } = useParams();
+    // const finalClassId = (classId || pathClassId) as string;
+    // if (pathRole && ['teacher', 'student'].includes(pathRole)) {
+    //   setRole(pathRole === 'teacher' ? Role.Teacher : Role.Student);
+    // }
     
-    if (!finalClassId) {
-      setErrorMessage('课堂ID不能为空');
-      showToast('error', '请输入有效的课堂ID');
-      return;
-    }
+    // if (!finalClassId) {
+    //   setErrorMessage('课堂ID不能为空');
+    //   showToast('error', '请输入有效的课堂ID');
+    //   return;
+    // }
 
     try {
       const gm = imEngine.current.getGroupManager();
       const mm = imEngine.current.getMessageManager();
       imGroupManager.current = gm || null;
       imMessageManager.current = mm || null;
-      await gm!.joinGroup(finalClassId);
+      await gm!.joinGroup(classId);
       listenGroupEvents();
       listenMessageEvents();
 
-      await joinRtcChannel(finalClassId);
+      await joinRtcChannel(classId);
+
+      buildShareLink(classId)
 
       setIsJoinedClass(true);
       setErrorMessage('');
@@ -498,7 +503,7 @@ export const useClassroom = () => {
       
       if (imGroupManager.current) {
         try {
-          await imGroupManager.current.leaveGroup(finalClassId);
+          await imGroupManager.current.leaveGroup(classId);
         } catch (leaveErr) {
           console.error('离开IM群组失败:', leaveErr);
         }
@@ -601,6 +606,17 @@ export const useClassroom = () => {
     }
   };
 
+  const buildShareLink = (classId: string) => {
+    const getBaseUrl = () => {
+      const protocol = window.location.protocol;
+      const host = window.location.host;
+      return `${protocol}//${host}`;
+  }
+    // const baseUrl = window.location.href.split('?')[0].replace(/\/[^/]*$/, '');
+    const baseUrl = getBaseUrl();
+    setShareLink(`${baseUrl}/mobile/classroom/${classId}/student`);
+  }
+
   const createClass = async (className: string, maxMembers = 200): Promise<string | null> => {
     if (!imEngine.current || !isLoggedIn || role !== Role.Teacher) {
       showToast('error', '只有老师可以创建课堂');
@@ -646,8 +662,9 @@ export const useClassroom = () => {
         
         await joinRtcChannel(response.groupId);
         
-        const baseUrl = window.location.href.split('?')[0].replace(/\/[^/]*$/, '');
-        setShareLink(`${baseUrl}/mobile/classroom/${response.groupId}/student`);
+        // const baseUrl = window.location.href.split('?')[0].replace(/\/[^/]*$/, '');
+        // setShareLink(`${baseUrl}/mobile/classroom/${response.groupId}/student`);
+        buildShareLink(response.groupId)
         
         return response.groupId;
       } catch (joinErr: any) {

+ 18 - 8
client/mobile/pages_classroom_new.tsx

@@ -72,7 +72,7 @@ const CreateClassSection = () => {
     if (!className.trim()) return;
     const classId = await createClass(className);
     if (classId) {
-      navigate(`/mobile/classroom/${classId}/${role === Role.Teacher ? 'teacher' : 'student'}`, { replace: true });
+      navigate(`/mobile/classroom/${classId}/${role === Role.Teacher ? Role.Teacher : Role.Student}`, { replace: true });
     }
   };
 
@@ -103,14 +103,24 @@ const CreateClassSection = () => {
 
 const Classroom = () => {
   const context = useClassroomContext();
-  const { role, classStatus, isLoggedIn, login } = context;
+  const { role, classStatus, isLoggedIn, login, classId, joinClass } = context;
   const { user, isAuthenticated } = useAuth();
 
   useEffect(() => {
-    if (isAuthenticated && user && !isLoggedIn) {
-      login(user.id.toString());
+    if(user)
+      context.setUserId(user?.id.toString())
+  },[ user ])
+
+  useEffect(() => {
+    
+    if (isAuthenticated && user && !isLoggedIn && role && classId) {
+      (async () => {
+        await login(user.id.toString());
+        await joinClass(classId);
+      })()
+      
     }
-  }, [isAuthenticated, user, isLoggedIn]);
+  }, [isAuthenticated, user, isLoggedIn, role , classId]);
 
   if (!role) {
     return (
@@ -130,7 +140,7 @@ const Classroom = () => {
     );
   }
 
-  if (role === Role.Teacher && !context.isJoinedClass) {
+  if (role === Role.Teacher && !context.isJoinedClass && !classId) {
     return (
       <>
         <AuthLayout>
@@ -140,7 +150,7 @@ const Classroom = () => {
     );
   }
 
-  if (role === Role.Student && !context.isJoinedClass) {
+  if (role === Role.Student && !context.isJoinedClass && !classId) {
     return (
       <>
         <AuthLayout>
@@ -174,7 +184,7 @@ export const ClassroomPage = () => {
       </ClassroomProvider>
       <ToastContainer
         position="top-right"
-        autoClose={5000}
+        autoClose={500}
         hideProgressBar={false}
         newestOnTop={false}
         closeOnClick