mobile_app.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. // 移动端布局组件 - 包含底部导航
  170. const MobileLayout = () => {
  171. const location = useLocation();
  172. return (
  173. <div className="flex flex-col min-h-screen">
  174. <div className="flex-1 pb-16">
  175. <Outlet />
  176. </div>
  177. {/* 底部导航栏 */}
  178. <nav className="fixed bottom-0 left-0 right-0 bg-white border-t border-gray-200 shadow-lg">
  179. <div className="flex justify-around">
  180. <Link
  181. to="/mobile"
  182. className={`flex flex-col items-center py-2 px-4 ${
  183. location.pathname === '/mobile' ? 'text-blue-600' : 'text-gray-500'
  184. }`}
  185. >
  186. <HomeIcon className="w-6 h-6 mb-1" />
  187. <span className="text-xs">首页</span>
  188. </Link>
  189. <Link
  190. to="/mobile/notifications"
  191. className={`flex flex-col items-center py-2 px-4 ${
  192. location.pathname === '/mobile/notifications' ? 'text-blue-600' : 'text-gray-500'
  193. }`}
  194. >
  195. <BellIcon className="w-6 h-6 mb-1" />
  196. <span className="text-xs">通知</span>
  197. </Link>
  198. <Link
  199. to="/mobile/profile"
  200. className={`flex flex-col items-center py-2 px-4 ${
  201. location.pathname === '/mobile/profile' ? 'text-blue-600' : 'text-gray-500'
  202. }`}
  203. >
  204. <UserIcon className="w-6 h-6 mb-1" />
  205. <span className="text-xs">我的</span>
  206. </Link>
  207. </div>
  208. </nav>
  209. </div>
  210. );
  211. };
  212. // 主应用组件
  213. const App = () => {
  214. // 创建路由器配置
  215. const router = createBrowserRouter([
  216. {
  217. path: '/',
  218. element: <Navigate to="/mobile" replace />,
  219. errorElement: <ErrorPage />
  220. },
  221. {
  222. path: '/mobile/login',
  223. element: <LoginPage />,
  224. errorElement: <ErrorPage />
  225. },
  226. {
  227. path: '/mobile',
  228. element: (
  229. <ProtectedRoute>
  230. <MobileLayout />
  231. </ProtectedRoute>
  232. ),
  233. errorElement: <ErrorPage />,
  234. children: [
  235. {
  236. index: true,
  237. element: <HomePage />
  238. },
  239. {
  240. path: 'profile',
  241. element: <ProfilePage />
  242. },
  243. {
  244. path: 'notifications',
  245. element: <NotificationsPage />
  246. }
  247. ]
  248. },
  249. {
  250. path: '*',
  251. element: <PageNotFound />
  252. }
  253. ]);
  254. return <RouterProvider router={router} />;
  255. };
  256. // 渲染应用到DOM
  257. const initApp = () => {
  258. // 注入全局样式
  259. injectGlobalStyles();
  260. // 渲染应用
  261. const root = createRoot(document.getElementById('root') as HTMLElement);
  262. root.render(
  263. <QueryClientProvider client={queryClient}>
  264. <ThemeProvider>
  265. <AuthProvider>
  266. <App />
  267. </AuthProvider>
  268. </ThemeProvider>
  269. </QueryClientProvider>
  270. );
  271. };
  272. // 初始化应用
  273. initApp();