mobile_app.tsx 8.6 KB

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