mobile_app.tsx 11 KB

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