mobile_app.tsx 9.3 KB

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