mobile_app.tsx 8.4 KB

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