mobile_app.tsx 10 KB

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