mobile_app.tsx 8.5 KB

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