生产管理系统 - 拦截器业务逻辑增强

This commit is contained in:
2025-11-01 09:32:26 +08:00
parent a1b3335664
commit e99567e6b3
6 changed files with 185 additions and 56 deletions

View File

@@ -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验证失败清除用户信息');

View File

@@ -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}</>;
}

View File

@@ -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) {
<PopoverTrigger asChild>
<Button variant="ghost" className="gap-2">
<User className="w-5 h-5" />
<span className="text-sm hidden md:inline">{authState.user?.realName || '用户'}</span>
<span className="text-sm hidden md:inline">{user?.realName || '用户'}</span>
</Button>
</PopoverTrigger>
<PopoverContent className="w-72 p-0" align="end">
@@ -42,8 +42,8 @@ export function UserProfile({ onProfileClick }: UserProfileProps) {
<UserCircle className="w-6 h-6" />
</div>
<div className="flex-1">
<h4 className="mb-1">{authState.user?.realName}</h4>
<p className="text-xs text-muted-foreground">{authState.user?.role === 'admin' ? '系统管理员' : '普通用户'}</p>
<h4 className="mb-1">{user?.realName}</h4>
<p className="text-xs text-muted-foreground">{user?.is_superuser ? '系统管理员' : '普通用户'}</p>
</div>
</div>
</div>
@@ -51,30 +51,30 @@ export function UserProfile({ onProfileClick }: UserProfileProps) {
<div className="p-3 space-y-2 text-sm">
<div className="flex justify-between">
<span className="text-muted-foreground">:</span>
<span>{authState.user?.username}</span>
<span>{user?.username}</span>
</div>
<div className="flex justify-between">
<span className="text-muted-foreground">:</span>
<span>{authState.user?.phone}</span>
<span>{user?.phone}</span>
</div>
{authState.user?.enterpriseName && (
{user?.enterpriseName && (
<div className="flex justify-between">
<span className="text-muted-foreground">:</span>
<span className="truncate max-w-[140px]" title={authState.user?.enterpriseName}>
{authState.user?.enterpriseName}
<span className="truncate max-w-[140px]" title={user?.enterpriseName}>
{user?.enterpriseName}
</span>
</div>
)}
{authState.user?.department && (
{user?.department && (
<div className="flex justify-between">
<span className="text-muted-foreground">:</span>
<span>{authState.user?.department}</span>
<span>{user?.department}</span>
</div>
)}
{authState.user?.lastLoginTime && (
{user?.lastLoginTime && (
<div className="flex justify-between text-xs">
<span className="text-muted-foreground">:</span>
<span className="text-muted-foreground">{authState.user?.lastLoginTime}</span>
<span className="text-muted-foreground">{user?.lastLoginTime}</span>
</div>
)}
</div>