mobile_app.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. import React, { useEffect } from 'react';
  2. import { createRoot } from 'react-dom/client';
  3. import {
  4. createBrowserRouter,
  5. RouterProvider,
  6. Outlet,
  7. Navigate,
  8. useLocation,
  9. useNavigate,
  10. Link,
  11. useRouteError
  12. } from 'react-router';
  13. import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
  14. import dayjs from 'dayjs';
  15. import 'dayjs/locale/zh-cn';
  16. import { AuthProvider, ThemeProvider, useAuth } from './hooks.tsx';
  17. import HomePage from './pages_index.tsx';
  18. import LoginPage from './pages_login.tsx';
  19. import RegisterPage from './pages_register.tsx';
  20. import { GlobalConfig } from "../share/types.ts";
  21. import { ExclamationTriangleIcon, HomeIcon, BellIcon, UserIcon, AcademicCapIcon } from '@heroicons/react/24/outline';
  22. import { NotificationsPage } from './pages_messages.tsx';
  23. import { LivePage } from './pages_live.tsx';
  24. import { RTCPage } from './pages_rtc.tsx';
  25. import { ClassroomPage } from './pages_classroom.tsx';
  26. import ExamIndex from './components/Exam/ExamIndex.tsx';
  27. import ExamAdmin from './components/Exam/ExamAdmin.tsx';
  28. import ExamCard from './components/Exam/ExamCard.tsx';
  29. // 设置中文语言
  30. dayjs.locale('zh-cn');
  31. // 声明全局配置对象类型
  32. declare global {
  33. interface Window {
  34. CONFIG?: GlobalConfig;
  35. }
  36. }
  37. // 创建QueryClient实例
  38. const queryClient = new QueryClient();
  39. // 添加全局CSS(使用TailwindCSS的类)
  40. const injectGlobalStyles = () => {
  41. const style = document.createElement('style');
  42. style.innerHTML = `
  43. :root {
  44. --primary-color: #3B82F6;
  45. --background-color: #F9FAFB;
  46. --text-color: #111827;
  47. --border-radius: 8px;
  48. --font-size: 16px;
  49. }
  50. * {
  51. margin: 0;
  52. padding: 0;
  53. box-sizing: border-box;
  54. }
  55. body {
  56. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
  57. Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  58. background-color: var(--background-color);
  59. color: var(--text-color);
  60. font-size: var(--font-size);
  61. line-height: 1.5;
  62. -webkit-font-smoothing: antialiased;
  63. -moz-osx-font-smoothing: grayscale;
  64. }
  65. .line-clamp-1 {
  66. display: -webkit-box;
  67. -webkit-line-clamp: 1;
  68. -webkit-box-orient: vertical;
  69. overflow: hidden;
  70. }
  71. .line-clamp-2 {
  72. display: -webkit-box;
  73. -webkit-line-clamp: 2;
  74. -webkit-box-orient: vertical;
  75. overflow: hidden;
  76. }
  77. .line-clamp-3 {
  78. display: -webkit-box;
  79. -webkit-line-clamp: 3;
  80. -webkit-box-orient: vertical;
  81. overflow: hidden;
  82. }
  83. /* 暗色模式支持 */
  84. .dark {
  85. color-scheme: dark;
  86. }
  87. .dark body {
  88. background-color: #121212;
  89. color: #E5E7EB;
  90. }
  91. /* 滚动条美化 */
  92. ::-webkit-scrollbar {
  93. width: 6px;
  94. height: 6px;
  95. }
  96. ::-webkit-scrollbar-track {
  97. background: transparent;
  98. }
  99. ::-webkit-scrollbar-thumb {
  100. background: #BFDBFE;
  101. border-radius: 3px;
  102. }
  103. ::-webkit-scrollbar-thumb:hover {
  104. background: #93C5FD;
  105. }
  106. /* 移动端点击高亮颜色 */
  107. * {
  108. -webkit-tap-highlight-color: transparent;
  109. }
  110. `;
  111. document.head.appendChild(style);
  112. };
  113. // 授权路由守卫
  114. const ProtectedRoute = ({ children }: { children: React.ReactNode }) => {
  115. const { isAuthenticated, isLoading } = useAuth();
  116. const location = useLocation();
  117. if (isLoading) {
  118. return (
  119. <div className="flex items-center justify-center min-h-screen">
  120. <div className="w-12 h-12 border-4 border-blue-200 border-t-blue-600 rounded-full animate-spin"></div>
  121. </div>
  122. );
  123. }
  124. if (!isAuthenticated) {
  125. return <Navigate to="/mobile/login" state={{ from: location }} replace />;
  126. }
  127. return <>{children}</>;
  128. };
  129. // 页面组件
  130. const PageNotFound = () => (
  131. <div className="flex flex-col items-center justify-center min-h-screen p-6 text-center">
  132. <div className="text-6xl font-bold text-blue-600 mb-4">404</div>
  133. <h1 className="text-2xl font-medium mb-2">页面不存在</h1>
  134. <p className="text-gray-500 mb-6">您访问的页面不存在或已被移除</p>
  135. <a
  136. href="/mobile"
  137. className="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors"
  138. >
  139. 返回首页
  140. </a>
  141. </div>
  142. );
  143. // 添加错误页面组件
  144. const ErrorPage = () => {
  145. const error = useRouteError() as any;
  146. const errorMessage = error?.statusText || error?.message || '未知错误';
  147. return (
  148. <div className="flex flex-col items-center justify-center min-h-screen p-6 text-center bg-gray-50">
  149. <div className="max-w-md w-full bg-white rounded-lg shadow-lg p-8">
  150. <ExclamationTriangleIcon className="h-16 w-16 text-red-600 mx-auto mb-4" />
  151. <h1 className="text-2xl font-medium mb-2">出错了</h1>
  152. <div className="text-gray-500 mb-4">抱歉,页面加载过程中发生错误</div>
  153. <div className="bg-red-50 border border-red-200 rounded-md p-4 mb-6">
  154. <p className="text-red-700 text-sm font-medium">错误信息:</p>
  155. <p className="text-red-600 mt-1 text-sm break-all">{errorMessage}</p>
  156. {error?.stack && (
  157. <details className="mt-2">
  158. <summary className="text-red-700 text-sm cursor-pointer">查看详细信息</summary>
  159. <pre className="mt-2 text-xs text-red-600 overflow-auto p-2 bg-red-50 rounded">
  160. {error.stack}
  161. </pre>
  162. </details>
  163. )}
  164. </div>
  165. <a
  166. href="/mobile"
  167. className="inline-flex items-center justify-center px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 transition-colors w-full"
  168. >
  169. 返回首页
  170. </a>
  171. </div>
  172. </div>
  173. );
  174. };
  175. import ProfilePage from './pages_profile.tsx'
  176. import SettingsPage from './pages_settings.tsx'
  177. // 移动端布局组件 - 包含底部导航
  178. const MobileLayout = () => {
  179. const location = useLocation();
  180. return (
  181. <div className="flex flex-col min-h-screen">
  182. <div className="flex-1 pb-16">
  183. <Outlet />
  184. </div>
  185. {/* 底部导航栏 */}
  186. <nav className="fixed bottom-0 left-0 right-0 bg-white border-t border-gray-200 shadow-lg">
  187. <div className="flex justify-around">
  188. <Link
  189. to="/mobile"
  190. className={`flex flex-col items-center py-2 px-4 ${
  191. location.pathname === '/mobile' ? 'text-blue-600' : 'text-gray-500'
  192. }`}
  193. >
  194. <HomeIcon className="w-6 h-6 mb-1" />
  195. <span className="text-xs">首页</span>
  196. </Link>
  197. <Link
  198. to="/mobile/notifications"
  199. className={`flex flex-col items-center py-2 px-4 ${
  200. location.pathname === '/mobile/notifications' ? 'text-blue-600' : 'text-gray-500'
  201. }`}
  202. >
  203. <BellIcon className="w-6 h-6 mb-1" />
  204. <span className="text-xs">通知</span>
  205. </Link>
  206. <Link
  207. to="/mobile/classroom"
  208. className={`flex flex-col items-center py-2 px-4 ${
  209. location.pathname.startsWith('/mobile/classroom') ? 'text-blue-600' : 'text-gray-500'
  210. }`}
  211. >
  212. <AcademicCapIcon className="w-6 h-6 mb-1" />
  213. <span className="text-xs">课堂</span>
  214. </Link>
  215. <Link
  216. to="/mobile/exam"
  217. className={`flex flex-col items-center py-2 px-4 ${
  218. location.pathname.startsWith('/mobile/exam') ? 'text-blue-600' : 'text-gray-500'
  219. }`}
  220. >
  221. <AcademicCapIcon className="w-6 h-6 mb-1" />
  222. <span className="text-xs">答题</span>
  223. </Link>
  224. <Link
  225. to="/mobile/profile"
  226. className={`flex flex-col items-center py-2 px-4 ${
  227. location.pathname === '/mobile/profile' ? 'text-blue-600' : 'text-gray-500'
  228. }`}
  229. >
  230. <UserIcon className="w-6 h-6 mb-1" />
  231. <span className="text-xs">我的</span>
  232. </Link>
  233. </div>
  234. </nav>
  235. </div>
  236. );
  237. };
  238. // 主应用组件
  239. const App = () => {
  240. // 创建路由器配置
  241. const router = createBrowserRouter([
  242. {
  243. path: '/',
  244. element: <Navigate to="/mobile" replace />,
  245. errorElement: <ErrorPage />
  246. },
  247. {
  248. path: '/mobile/classroom/:id?/:role?',
  249. element: <ProtectedRoute><ClassroomPage /></ProtectedRoute>,
  250. errorElement: <ErrorPage />
  251. },
  252. {
  253. path: '/mobile/exam',
  254. element: <ProtectedRoute><ExamIndex /></ProtectedRoute>,
  255. errorElement: <ErrorPage />
  256. },
  257. {
  258. path: '/mobile/exam/:id',
  259. element: <ProtectedRoute><ExamAdmin /></ProtectedRoute>,
  260. errorElement: <ErrorPage />
  261. },
  262. {
  263. path: '/mobile/exam/card',
  264. element: <ProtectedRoute><ExamCard /></ProtectedRoute>,
  265. errorElement: <ErrorPage />
  266. },
  267. {
  268. path: '/mobile/exam',
  269. element: <ProtectedRoute><ExamIndex /></ProtectedRoute>,
  270. errorElement: <ErrorPage />
  271. },
  272. {
  273. path: '/mobile/exam/:id',
  274. element: <ProtectedRoute><ExamAdmin /></ProtectedRoute>,
  275. errorElement: <ErrorPage />
  276. },
  277. {
  278. path: '/mobile/live',
  279. element: <LivePage />,
  280. errorElement: <ErrorPage />
  281. },
  282. {
  283. path: '/mobile/rtc',
  284. element: <RTCPage />,
  285. errorElement: <ErrorPage />
  286. },
  287. {
  288. path: '/mobile/login',
  289. element: <LoginPage />,
  290. errorElement: <ErrorPage />
  291. },
  292. {
  293. path: '/mobile/register',
  294. element: <RegisterPage />,
  295. errorElement: <ErrorPage />
  296. },
  297. {
  298. path: '/mobile',
  299. element: (
  300. <ProtectedRoute>
  301. <MobileLayout />
  302. </ProtectedRoute>
  303. ),
  304. errorElement: <ErrorPage />,
  305. children: [
  306. {
  307. index: true,
  308. element: <HomePage />
  309. },
  310. {
  311. path: 'profile',
  312. element: <ProfilePage />
  313. },
  314. {
  315. path: 'notifications',
  316. element: <NotificationsPage />
  317. },
  318. {
  319. path: 'settings',
  320. element: <SettingsPage />
  321. }
  322. ]
  323. },
  324. {
  325. path: '*',
  326. element: <PageNotFound />
  327. }
  328. ]);
  329. return <RouterProvider router={router} />;
  330. };
  331. // 渲染应用到DOM
  332. const initApp = () => {
  333. // 注入全局样式
  334. injectGlobalStyles();
  335. // 渲染应用
  336. const root = createRoot(document.getElementById('root') as HTMLElement);
  337. root.render(
  338. <QueryClientProvider client={queryClient}>
  339. <ThemeProvider>
  340. <AuthProvider>
  341. <App />
  342. </AuthProvider>
  343. </ThemeProvider>
  344. </QueryClientProvider>
  345. );
  346. };
  347. // 初始化应用
  348. initApp();