Browse Source

处理URL中的role参数

yourname 6 months ago
parent
commit
5528ca0b35
1 changed files with 13 additions and 2 deletions
  1. 13 2
      client/mobile/pages_classroom.tsx

+ 13 - 2
client/mobile/pages_classroom.tsx

@@ -1,6 +1,6 @@
 import React, { useState, useEffect } from 'react';
 import { useAuth } from './hooks.tsx';
-import { useNavigate } from 'react-router';
+import { useNavigate, useSearchParams } from 'react-router';
 import { Role, ClassStatus } from './components/Classroom/useClassroom.ts';
 // import { TeacherView } from './components/Classroom/TeacherView.tsx';
 // import { StudentView } from './components/Classroom/StudentView.tsx';
@@ -112,7 +112,18 @@ const CreateClassSection = () => {
 
 const Classroom = () => {
   const context = useClassroomContext();
-  const { role, classStatus, isLoggedIn, login, classId, joinClass } = context;
+  const { role, classStatus, isLoggedIn, login, classId, joinClass, setRole } = context;
+  const [searchParams] = useSearchParams();
+
+  useEffect(() => {
+    // 处理URL中的role参数
+    const urlRole = searchParams.get('role');
+    if (urlRole && !role) {
+      const roleValue = urlRole === 'admin' ? Role.Teacher : Role.Student;
+      setRole(roleValue);
+      login(roleValue);
+    }
+  }, [searchParams]);
 
   useEffect(() => {