mobile_app.tsx 11 KB

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