From e99567e6b3312b77048fec1d8d8b6bcbd0dfc4f4 Mon Sep 17 00:00:00 2001 From: peng Date: Sat, 1 Nov 2025 09:32:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E4=BA=A7=E7=AE=A1=E7=90=86=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=20-=20=E6=8B=A6=E6=88=AA=E5=99=A8=E4=B8=9A=E5=8A=A1?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=A2=9E=E5=BC=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../personal-center/personal-info/page.tsx | 57 +++++--- crop-x/src/app/not-found.tsx | 132 +++++++++++++++--- crop-x/src/app/page.tsx | 7 +- crop-x/src/components/auth/AuthContext.tsx | 9 +- .../components/auth/ClientAuthInterceptor.tsx | 8 +- .../layouts/components/UserProfile.tsx | 28 ++-- 6 files changed, 185 insertions(+), 56 deletions(-) diff --git a/crop-x/src/app/(app)/central-config/personal-center/personal-info/page.tsx b/crop-x/src/app/(app)/central-config/personal-center/personal-info/page.tsx index 51b4c0c..77ecdbe 100644 --- a/crop-x/src/app/(app)/central-config/personal-center/personal-info/page.tsx +++ b/crop-x/src/app/(app)/central-config/personal-center/personal-info/page.tsx @@ -13,28 +13,31 @@ import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { UserProfile, PasswordChange } from '@/types/profile'; import { User, Mail, Phone, Building, Briefcase, Lock, Save, Shield } from 'lucide-react'; import { toast } from 'sonner'; +import { useAuth } from '@/components/auth/AuthContext'; export default function PersonalInfo() { + const { user } = useAuth(); + const [profile, setProfile] = useState({ - id: 'user-1', - username: 'admin', - name: '系统管理员', - email: 'admin@smart-agriculture.com', - phone: '13800138000', + id: user?.id || '', + username: user?.username || '', + name: user?.realName || '', + email: user?.email || '', + phone: user?.phone || '', avatar: '', - gender: 'male', - birthday: '1990-01-01', - department: '技术部', - position: '系统管理员', - enterpriseId: 'ent-1', - enterpriseName: '智慧农业科技有限公司', - roleIds: ['role-1'], - roleNames: ['超级管理员'], - bio: '负责系统整体架构和技术管理', - address: '北京市海淀区中关村大街1号', - createdAt: '2024-01-01T00:00:00', - lastLoginTime: '2024-10-14T09:30:00', - lastLoginIp: '192.168.1.100', + gender: '', + birthday: '', + department: '', + position: '', + enterpriseId: user?.enterpriseId || '', + enterpriseName: user?.enterpriseName || '', + roleIds: [], + roleNames: user?.is_superuser ? ['超级管理员'] : ['普通用户'], + bio: '', + address: '', + createdAt: user?.createdAt || '', + lastLoginTime: '', + lastLoginIp: '', }); const [showPasswordDialog, setShowPasswordDialog] = useState(false); @@ -50,6 +53,24 @@ export default function PersonalInfo() { loadProfile(); }, []); + // 当用户信息变化时,更新 profile 状态 + useEffect(() => { + if (user) { + setProfile(prev => ({ + ...prev, + id: user.id || '', + username: user.username || '', + name: user.realName || '', + email: user.email || '', + phone: user.phone || '', + enterpriseId: user.enterpriseId || '', + enterpriseName: user.enterpriseName || '', + roleNames: user.is_superuser ? ['超级管理员'] : ['普通用户'], + createdAt: user.createdAt || '', + })); + } + }, [user]); + const loadProfile = () => { const data = localStorage.getItem('smart_agriculture_user_profile'); if (data) { diff --git a/crop-x/src/app/not-found.tsx b/crop-x/src/app/not-found.tsx index dd7c171..ee3e45e 100644 --- a/crop-x/src/app/not-found.tsx +++ b/crop-x/src/app/not-found.tsx @@ -1,21 +1,119 @@ +'use client'; +import "@/styles/globals.css" +import Link from 'next/link'; +import { Button } from '@/components/ui/button'; +import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card'; +import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'; +import { Badge } from '@/components/ui/badge'; +import { Home, ArrowLeft, RefreshCw, Search, AlertTriangle, ExternalLink } from 'lucide-react'; + export default function NotFound() { + const handleRefresh = () => { + window.location.reload(); + }; + + const handleGoBack = () => { + window.history.back(); + }; + return ( -
-
-

404

-

- 页面未找到 -

-

- 抱歉,您访问的页面不存在 -

- - 返回首页 - +
+ + {/* 主要内容 */} +
+ {/* 404 状态卡片 */} + + +
+
+
404
+
+ + + Error + +
+
+
+ + 页面未找到 + + + 抱歉,您访问的页面似乎不存在或已被移动 + +
+ + + {/* 错误信息提示 */} + + + 发生了什么? + + 您访问的页面可能已经删除、移动或暂时不可用。请检查URL是否正确,或尝试以下操作。 + + + + {/* 可能的原因列表 */} +
+

+ + 可能的原因: +

+
+
+ + 页面地址输入错误或链接已失效 +
+
+ + 页面已被移动或删除 +
+
+ + 您没有访问此页面的权限 +
+
+ + 服务器暂时无法响应 +
+
+
+ + {/* 快速操作按钮 */} +
+ + + + + +
+
+ + + {/* 帮助信息 */} +
+

需要帮助?请联系系统管理员

+
+ + {/* 错误详情 */} +
+ 错误代码: 404 + 时间: {new Date().toLocaleString('zh-CN')} +
+
+
- ) -} \ No newline at end of file + ); +} \ No newline at end of file diff --git a/crop-x/src/app/page.tsx b/crop-x/src/app/page.tsx index c6e2fda..cd1c42b 100644 --- a/crop-x/src/app/page.tsx +++ b/crop-x/src/app/page.tsx @@ -18,7 +18,12 @@ export default function HomePage() { redirect('/login'); } - // 用户已认证,显示主页内容 + // 如果已认证,重定向到个人中心 + if (isAuthenticated) { + redirect('/central-config/personal-center/personal-info'); + } + + // 兜底显示(一般不会执行到这里) return (
); diff --git a/crop-x/src/components/auth/AuthContext.tsx b/crop-x/src/components/auth/AuthContext.tsx index 03dd8a5..dc2565c 100644 --- a/crop-x/src/components/auth/AuthContext.tsx +++ b/crop-x/src/components/auth/AuthContext.tsx @@ -65,6 +65,11 @@ export function AuthProvider({ children }: AuthProviderProps) { localStorage.removeItem('user'); removeTokenCookie(); // 清除 cookie setLoading(false); + + // 跳转到登录页 + if (typeof window !== 'undefined') { + window.location.href = '/login'; + } }; // 验证当前用户信息 @@ -89,9 +94,9 @@ export function AuthProvider({ children }: AuthProviderProps) { // 更新用户信息(可能包含最新的权限、角色等) setUser({ ...userData, - ...response.data.data, // 合并最新的用户信息 + ...response.data, // 合并最新的用户信息 }); - console.log('✅ 用户验证成功,最新用户信息:', response.data.data); + console.log('✅ 用户验证成功,最新用户信息:', response.data); } else { // Token无效,清除用户信息 console.warn('⚠️ Token验证失败,清除用户信息'); diff --git a/crop-x/src/components/auth/ClientAuthInterceptor.tsx b/crop-x/src/components/auth/ClientAuthInterceptor.tsx index 546d759..c7a057b 100644 --- a/crop-x/src/components/auth/ClientAuthInterceptor.tsx +++ b/crop-x/src/components/auth/ClientAuthInterceptor.tsx @@ -20,8 +20,8 @@ export function ClientAuthInterceptor({ children }: ClientAuthInterceptorProps) const currentPath = window.location.pathname; - // 如果已经在登录页面,不需要重定向 - if (currentPath === '/login' || currentPath === '/register') { + // 如果已经在认证页面(包括 /login 开头的所有路径),不需要重定向 + if (currentPath.startsWith('/login')) { console.log(`📄 已在认证页面,跳过拦截: ${currentPath}`); return; } @@ -53,8 +53,8 @@ export function ClientAuthInterceptor({ children }: ClientAuthInterceptorProps) ); } - // 如果在认证页面(登录/注册),直接渲染子组件,不需要认证检查 - if (currentPath === '/login' || currentPath === '/register') { + // 如果在认证页面(包括 /login 开头的所有路径),直接渲染子组件,不需要认证检查 + if (currentPath.startsWith('/login')) { return <>{children}; } diff --git a/crop-x/src/components/layouts/components/UserProfile.tsx b/crop-x/src/components/layouts/components/UserProfile.tsx index a844c3f..85f7e67 100644 --- a/crop-x/src/components/layouts/components/UserProfile.tsx +++ b/crop-x/src/components/layouts/components/UserProfile.tsx @@ -2,7 +2,7 @@ import { useState } from 'react'; import { User, UserCircle, LogOut } from 'lucide-react'; -import { useAuth } from './auth/AuthContext'; +import { useAuth } from '@/components/auth/AuthContext'; import { toast } from 'sonner'; import { Button } from './ui/button'; import { Popover, PopoverContent, PopoverTrigger } from './ui/popover'; @@ -12,7 +12,7 @@ interface UserProfileProps { } export function UserProfile({ onProfileClick }: UserProfileProps) { - const { authState, logout } = useAuth(); + const { user, logout } = useAuth(); const [showUserMenu, setShowUserMenu] = useState(false); const handleProfileClick = () => { @@ -32,7 +32,7 @@ export function UserProfile({ onProfileClick }: UserProfileProps) { @@ -42,8 +42,8 @@ export function UserProfile({ onProfileClick }: UserProfileProps) {
-

{authState.user?.realName}

-

{authState.user?.role === 'admin' ? '系统管理员' : '普通用户'}

+

{user?.realName}

+

{user?.is_superuser ? '系统管理员' : '普通用户'}

@@ -51,30 +51,30 @@ export function UserProfile({ onProfileClick }: UserProfileProps) {
用户名: - {authState.user?.username} + {user?.username}
手机号: - {authState.user?.phone} + {user?.phone}
- {authState.user?.enterpriseName && ( + {user?.enterpriseName && (
所属企业: - - {authState.user?.enterpriseName} + + {user?.enterpriseName}
)} - {authState.user?.department && ( + {user?.department && (
部门: - {authState.user?.department} + {user?.department}
)} - {authState.user?.lastLoginTime && ( + {user?.lastLoginTime && (
上次登录: - {authState.user?.lastLoginTime} + {user?.lastLoginTime}
)}