登录框架搭建完成

This commit is contained in:
mmc
2026-04-21 14:57:52 +08:00
parent 79f8252b08
commit 262ef5e24f
16 changed files with 278 additions and 161 deletions

View File

@@ -9,7 +9,8 @@ import {
ConfigProvider,
theme,
Breadcrumb,
Badge
Badge,
message
} from 'antd';
import {
DashboardOutlined,
@@ -26,6 +27,8 @@ import {
} from '@ant-design/icons';
import { MOCK_USER } from '../constants';
import logo from '../assets/logo.png';
import { useSelector } from 'react-redux';
import { RootState } from '../store';
const { Header, Sider, Content } = Layout;
export default function AppLayout() {
@@ -33,10 +36,7 @@ export default function AppLayout() {
const [time, setTime] = useState(new Date().toLocaleString());
const navigate = useNavigate();
const location = useLocation();
// 从 localStorage 获取真实用户信息
const userJson = localStorage.getItem('user');
const user = userJson ? JSON.parse(userJson) : MOCK_USER;
const { userInfo } = useSelector((state: RootState) => state.user);
useEffect(() => {
const timer = setInterval(() => setTime(new Date().toLocaleString()), 1000);
@@ -61,7 +61,7 @@ export default function AppLayout() {
{ key: '/serialNumGenerate', icon: <UserOutlined />, label: '序列号生成', roles: ['admin'] },
];
const filteredMenu = menuItems.filter(item => item.roles.includes(user.role));
const filteredMenu = menuItems.filter(item => item.roles.includes(userInfo.role));
const currentPathLabel = menuItems.find(i => i.key === location.pathname)?.label || '概览控制台';
@@ -85,6 +85,7 @@ export default function AppLayout() {
}
}}
>
<Layout style={{ minHeight: '100vh' }}>
<Sider trigger={null} collapsible collapsed={collapsed} width={220} theme="dark">
<div className="h-16 flex items-center justify-center border-b border-white/10 overflow-hidden">
@@ -100,11 +101,7 @@ export default function AppLayout() {
items={filteredMenu}
onClick={({ key }) => navigate(key)}
/>
{!collapsed && (
<div className="absolute bottom-4 left-4 right-4 p-2 bg-white/5 rounded text-[12px] text-gray-400">
当前权限: <span className="text-white font-bold">{user.role === 'admin' ? '系统管理员' : user.role === 'operator' ? '操作员' : '观察员'}</span>
</div>
)}
</Sider>
<Layout>
<Header className="px-6 flex items-center justify-between shadow-sm border-b border-gray-100 h-16 bg-white shrink-0 z-10">
@@ -127,8 +124,7 @@ export default function AppLayout() {
<div className="flex items-center gap-3 pr-6 border-r border-gray-100">
<span className="text-gray-400 font-mono text-[13px]">{time}</span>
<Badge status="success" />
<span className="text-gray-800 font-medium">{user.name}</span>
<Avatar src={user.avatar} size="small" icon={<UserOutlined />} />
<span className="text-gray-800 font-medium">{userInfo.username}</span>
</div>
<Button
type="text"

View File

@@ -1,14 +1,53 @@
import React from 'react';
import React, { useEffect } from 'react';
import { useSelector } from 'react-redux';
import { Navigate, Outlet } from 'react-router-dom';
import { Navigate, Outlet, useLocation, useNavigate } from 'react-router-dom';
import Cookies from 'js-cookie';
const whiteList = ['/login', '/auth-redirect', '/bind', '/register', '/oauthLogin'];
export default function ProtectedRoute() {
const isAuthenticated = localStorage.getItem('isAuthenticated') === 'true';
//const { isAuthenticated } = useSelector(state => state.user);
const { isAuthenticated } = useSelector((state) => state.user);
const location = useLocation();
const navigate = useNavigate();
const { pathname, search } = location;
// 白名单
if (whiteList.includes(pathname)) {
return <Outlet />;
}
// 未登录
if (!isAuthenticated) {
return <Navigate to="/login" replace />;
return <Navigate to={`/login?redirect=${pathname}`} replace />;
}
// 已登录,访问登录页 → 跳首页
if (pathname === '/login') {
const queryParams = new URLSearchParams(search);
const goRemote = queryParams.get('goRemote');
const clear = queryParams.get('clear');
if (goRemote) {
if (clear === '1') {
Cookies.remove('userId');
window.location.href = 'http://ri.satabot.com/#/login?goRemote=1';
return null;
}
const rawUserId = Cookies.get('userId');
if (!rawUserId) {
navigate('/');
} else {
const encoded = btoa(unescape(encodeURIComponent(rawUserId)));
window.location.href = `http://ai.satabot.com/?userId=${encoded}`;
}
return null;
}
return <Navigate to="/" replace />;
}
return <Outlet />;