mobile_app.tsx 8.3 KB

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