2
0

mobile_app.tsx 8.8 KB

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