mobile_app.tsx 8.3 KB

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