mobile_app.tsx 8.5 KB

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