mobile_app.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. // 添加个人页面组件
  169. const ProfilePage = () => {
  170. const { logout, user } = useAuth();
  171. const navigate = useNavigate();
  172. const handleLogout = async () => {
  173. if (confirm('确定要退出登录吗?')) {
  174. await logout();
  175. navigate('/mobile/login');
  176. }
  177. };
  178. return (
  179. <div className="p-4">
  180. <h1 className="text-2xl font-bold mb-4">我的</h1>
  181. <div className="bg-white rounded-lg shadow p-4 mb-4">
  182. <div className="flex items-center mb-4">
  183. <div className="w-16 h-16 bg-blue-100 rounded-full flex items-center justify-center mr-4">
  184. {user?.avatar ? (
  185. <img
  186. src={user.avatar}
  187. alt={user?.nickname || user?.username || '用户'}
  188. className="w-16 h-16 rounded-full object-cover"
  189. />
  190. ) : (
  191. <span className="text-2xl text-blue-600">用户</span>
  192. )}
  193. </div>
  194. <div>
  195. <h2 className="text-xl font-semibold">{user?.nickname || user?.username || '未登录用户'}</h2>
  196. <p className="text-gray-500">个人信息</p>
  197. </div>
  198. </div>
  199. </div>
  200. <div className="bg-white rounded-lg shadow mb-4">
  201. <div className="p-4 border-b">
  202. <span className="font-medium">设置</span>
  203. </div>
  204. <div className="divide-y">
  205. <div className="p-4 flex justify-between items-center">
  206. <span>账号安全</span>
  207. <span className="text-gray-400">›</span>
  208. </div>
  209. <div className="p-4 flex justify-between items-center">
  210. <span>通知设置</span>
  211. <span className="text-gray-400">›</span>
  212. </div>
  213. <div className="p-4 flex justify-between items-center">
  214. <span>隐私</span>
  215. <span className="text-gray-400">›</span>
  216. </div>
  217. <div className="p-4 flex justify-between items-center">
  218. <span>关于</span>
  219. <span className="text-gray-400">›</span>
  220. </div>
  221. </div>
  222. </div>
  223. <button
  224. onClick={handleLogout}
  225. className="w-full py-3 bg-red-600 text-white rounded-lg hover:bg-red-700 transition-colors"
  226. >
  227. 退出登录
  228. </button>
  229. </div>
  230. );
  231. };
  232. // 移动端布局组件 - 包含底部导航
  233. const MobileLayout = () => {
  234. const location = useLocation();
  235. return (
  236. <div className="flex flex-col min-h-screen">
  237. <div className="flex-1 pb-16">
  238. <Outlet />
  239. </div>
  240. {/* 底部导航栏 */}
  241. <nav className="fixed bottom-0 left-0 right-0 bg-white border-t border-gray-200 shadow-lg">
  242. <div className="flex justify-around">
  243. <Link
  244. to="/mobile"
  245. className={`flex flex-col items-center py-2 px-4 ${
  246. location.pathname === '/mobile' ? 'text-blue-600' : 'text-gray-500'
  247. }`}
  248. >
  249. <HomeIcon className="w-6 h-6 mb-1" />
  250. <span className="text-xs">首页</span>
  251. </Link>
  252. <Link
  253. to="/mobile/notifications"
  254. className={`flex flex-col items-center py-2 px-4 ${
  255. location.pathname === '/mobile/notifications' ? 'text-blue-600' : 'text-gray-500'
  256. }`}
  257. >
  258. <BellIcon className="w-6 h-6 mb-1" />
  259. <span className="text-xs">通知</span>
  260. </Link>
  261. <Link
  262. to="/mobile/profile"
  263. className={`flex flex-col items-center py-2 px-4 ${
  264. location.pathname === '/mobile/profile' ? 'text-blue-600' : 'text-gray-500'
  265. }`}
  266. >
  267. <UserIcon className="w-6 h-6 mb-1" />
  268. <span className="text-xs">我的</span>
  269. </Link>
  270. </div>
  271. </nav>
  272. </div>
  273. );
  274. };
  275. // 主应用组件
  276. const App = () => {
  277. // 创建路由器配置
  278. const router = createBrowserRouter([
  279. {
  280. path: '/',
  281. element: <Navigate to="/mobile" replace />,
  282. errorElement: <ErrorPage />
  283. },
  284. {
  285. path: '/mobile/login',
  286. element: <LoginPage />,
  287. errorElement: <ErrorPage />
  288. },
  289. {
  290. path: '/mobile',
  291. element: (
  292. <ProtectedRoute>
  293. <MobileLayout />
  294. </ProtectedRoute>
  295. ),
  296. errorElement: <ErrorPage />,
  297. children: [
  298. {
  299. index: true,
  300. element: <HomePage />
  301. },
  302. {
  303. path: 'profile',
  304. element: <ProfilePage />
  305. },
  306. {
  307. path: 'notifications',
  308. element: <NotificationsPage />
  309. }
  310. ]
  311. },
  312. {
  313. path: '*',
  314. element: <PageNotFound />
  315. }
  316. ]);
  317. return <RouterProvider router={router} />;
  318. };
  319. // 渲染应用到DOM
  320. const initApp = () => {
  321. // 注入全局样式
  322. injectGlobalStyles();
  323. // 渲染应用
  324. const root = createRoot(document.getElementById('root') as HTMLElement);
  325. root.render(
  326. <QueryClientProvider client={queryClient}>
  327. <ThemeProvider>
  328. <AuthProvider>
  329. <App />
  330. </AuthProvider>
  331. </ThemeProvider>
  332. </QueryClientProvider>
  333. );
  334. };
  335. // 初始化应用
  336. initApp();