mobile_app.tsx 11 KB

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