feat: 添加依赖,实现登录页面、跳转权限控制逻辑、通用页面布局及其菜单显示逻辑
This commit is contained in:
parent
0128fb9ba0
commit
56af8ed58a
4566
package-lock.json
generated
Normal file
4566
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
12
package.json
12
package.json
@ -10,8 +10,16 @@
|
|||||||
"preview": "vite preview"
|
"preview": "vite preview"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"react": "^19.1.0",
|
"@ant-design/icons": "^6.0.0",
|
||||||
"react-dom": "^19.1.0"
|
"@reduxjs/toolkit": "^2.8.2",
|
||||||
|
"@tailwindcss/vite": "^4.1.10",
|
||||||
|
"antd": "^5.26.1",
|
||||||
|
"axios": "^1.10.0",
|
||||||
|
"react": "^18.3.1",
|
||||||
|
"react-dom": "^18.3.1",
|
||||||
|
"react-redux": "^9.2.0",
|
||||||
|
"react-router-dom": "^7.6.2",
|
||||||
|
"tailwindcss": "^4.1.10"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/js": "^9.25.0",
|
"@eslint/js": "^9.25.0",
|
||||||
|
42
src/App.css
42
src/App.css
@ -1,42 +0,0 @@
|
|||||||
#root {
|
|
||||||
max-width: 1280px;
|
|
||||||
margin: 0 auto;
|
|
||||||
padding: 2rem;
|
|
||||||
text-align: center;
|
|
||||||
}
|
|
||||||
|
|
||||||
.logo {
|
|
||||||
height: 6em;
|
|
||||||
padding: 1.5em;
|
|
||||||
will-change: filter;
|
|
||||||
transition: filter 300ms;
|
|
||||||
}
|
|
||||||
.logo:hover {
|
|
||||||
filter: drop-shadow(0 0 2em #646cffaa);
|
|
||||||
}
|
|
||||||
.logo.react:hover {
|
|
||||||
filter: drop-shadow(0 0 2em #61dafbaa);
|
|
||||||
}
|
|
||||||
|
|
||||||
@keyframes logo-spin {
|
|
||||||
from {
|
|
||||||
transform: rotate(0deg);
|
|
||||||
}
|
|
||||||
to {
|
|
||||||
transform: rotate(360deg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@media (prefers-reduced-motion: no-preference) {
|
|
||||||
a:nth-of-type(2) .logo {
|
|
||||||
animation: logo-spin infinite 20s linear;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
.card {
|
|
||||||
padding: 2em;
|
|
||||||
}
|
|
||||||
|
|
||||||
.read-the-docs {
|
|
||||||
color: #888;
|
|
||||||
}
|
|
35
src/App.jsx
35
src/App.jsx
@ -1,35 +0,0 @@
|
|||||||
import { useState } from 'react'
|
|
||||||
import reactLogo from './assets/react.svg'
|
|
||||||
import viteLogo from '/vite.svg'
|
|
||||||
import './App.css'
|
|
||||||
|
|
||||||
function App() {
|
|
||||||
const [count, setCount] = useState(0)
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<div>
|
|
||||||
<a href="https://vite.dev" target="_blank">
|
|
||||||
<img src={viteLogo} className="logo" alt="Vite logo" />
|
|
||||||
</a>
|
|
||||||
<a href="https://react.dev" target="_blank">
|
|
||||||
<img src={reactLogo} className="logo react" alt="React logo" />
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
<h1>Vite + React</h1>
|
|
||||||
<div className="card">
|
|
||||||
<button onClick={() => setCount((count) => count + 1)}>
|
|
||||||
count is {count}
|
|
||||||
</button>
|
|
||||||
<p>
|
|
||||||
Edit <code>src/App.jsx</code> and save to test HMR
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
<p className="read-the-docs">
|
|
||||||
Click on the Vite and React logos to learn more
|
|
||||||
</p>
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
export default App
|
|
31
src/api/axios.js
Normal file
31
src/api/axios.js
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import { message } from "antd";
|
||||||
|
import axios, { Axios } from "axios";
|
||||||
|
|
||||||
|
const axiosInstance = axios.create({
|
||||||
|
baseURL: "http://127.0.0.1:8080",
|
||||||
|
timeout: 2000, // 2秒
|
||||||
|
});
|
||||||
|
|
||||||
|
axiosInstance.interceptors.request.use(
|
||||||
|
(config) => {
|
||||||
|
const token = localStorage.getItem("token");
|
||||||
|
if (token) {
|
||||||
|
config.headers.Authorization = `Bearer ${token}`;
|
||||||
|
}
|
||||||
|
return config;
|
||||||
|
},
|
||||||
|
(error) => Promise.reject(error)
|
||||||
|
);
|
||||||
|
|
||||||
|
axiosInstance.interceptors.response.use((response) => {
|
||||||
|
const { code, message: msg, data } = response.data;
|
||||||
|
|
||||||
|
if (code === 0) {
|
||||||
|
return data;
|
||||||
|
} else {
|
||||||
|
message.error(msg);
|
||||||
|
return Promise.reject(new Error(msg));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default axiosInstance;
|
12
src/config/menuConfig.js
Normal file
12
src/config/menuConfig.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { DesktopOutlined } from "@ant-design/icons";
|
||||||
|
|
||||||
|
const menuConfig = [
|
||||||
|
{
|
||||||
|
path: "/user/reserve",
|
||||||
|
label: "设备预约",
|
||||||
|
icon: DesktopOutlined,
|
||||||
|
roles: ["USER"],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
export default menuConfig;
|
43
src/features/auth/authSlice.js
Normal file
43
src/features/auth/authSlice.js
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import { createSlice } from "@reduxjs/toolkit";
|
||||||
|
import { login } from "./authThunk";
|
||||||
|
|
||||||
|
const authSlice = createSlice({
|
||||||
|
name: "authSlice",
|
||||||
|
initialState: {
|
||||||
|
userId: null,
|
||||||
|
name: null,
|
||||||
|
roles: [],
|
||||||
|
token: null,
|
||||||
|
},
|
||||||
|
reducers: {
|
||||||
|
logout(state) {
|
||||||
|
state.userId = null;
|
||||||
|
state.name = null;
|
||||||
|
state.token = null;
|
||||||
|
state.roles = [];
|
||||||
|
|
||||||
|
localStorage.removeItem("userId");
|
||||||
|
localStorage.removeItem("name");
|
||||||
|
localStorage.removeItem("roles");
|
||||||
|
localStorage.removeItem("token");
|
||||||
|
},
|
||||||
|
},
|
||||||
|
extraReducers: (builder) => {
|
||||||
|
builder.addCase(login.fulfilled, (state, action) => {
|
||||||
|
const payload = action.payload;
|
||||||
|
state.userId = payload.userId;
|
||||||
|
state.name = payload.name;
|
||||||
|
state.roles = payload.roles;
|
||||||
|
state.token = payload.token;
|
||||||
|
|
||||||
|
localStorage.setItem("userId", payload.userId);
|
||||||
|
localStorage.setItem("name", payload.name);
|
||||||
|
localStorage.setItem("roles", JSON.stringify(payload.roles));
|
||||||
|
localStorage.setItem("token", action.payload.token);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const { logout } = authSlice.actions;
|
||||||
|
export const selectUserRole = (state) => state.auth.roles;
|
||||||
|
export default authSlice.reducer;
|
11
src/features/auth/authThunk.js
Normal file
11
src/features/auth/authThunk.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { createAsyncThunk } from "@reduxjs/toolkit";
|
||||||
|
import axiosInstance from "../../api/axios";
|
||||||
|
|
||||||
|
export const login = createAsyncThunk(
|
||||||
|
"auth/login",
|
||||||
|
async (values, thunkAPI) => {
|
||||||
|
const res = await axiosInstance.post("/login", values);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
);
|
@ -1,68 +1 @@
|
|||||||
:root {
|
@import "tailwindcss";
|
||||||
font-family: system-ui, Avenir, Helvetica, Arial, sans-serif;
|
|
||||||
line-height: 1.5;
|
|
||||||
font-weight: 400;
|
|
||||||
|
|
||||||
color-scheme: light dark;
|
|
||||||
color: rgba(255, 255, 255, 0.87);
|
|
||||||
background-color: #242424;
|
|
||||||
|
|
||||||
font-synthesis: none;
|
|
||||||
text-rendering: optimizeLegibility;
|
|
||||||
-webkit-font-smoothing: antialiased;
|
|
||||||
-moz-osx-font-smoothing: grayscale;
|
|
||||||
}
|
|
||||||
|
|
||||||
a {
|
|
||||||
font-weight: 500;
|
|
||||||
color: #646cff;
|
|
||||||
text-decoration: inherit;
|
|
||||||
}
|
|
||||||
a:hover {
|
|
||||||
color: #535bf2;
|
|
||||||
}
|
|
||||||
|
|
||||||
body {
|
|
||||||
margin: 0;
|
|
||||||
display: flex;
|
|
||||||
place-items: center;
|
|
||||||
min-width: 320px;
|
|
||||||
min-height: 100vh;
|
|
||||||
}
|
|
||||||
|
|
||||||
h1 {
|
|
||||||
font-size: 3.2em;
|
|
||||||
line-height: 1.1;
|
|
||||||
}
|
|
||||||
|
|
||||||
button {
|
|
||||||
border-radius: 8px;
|
|
||||||
border: 1px solid transparent;
|
|
||||||
padding: 0.6em 1.2em;
|
|
||||||
font-size: 1em;
|
|
||||||
font-weight: 500;
|
|
||||||
font-family: inherit;
|
|
||||||
background-color: #1a1a1a;
|
|
||||||
cursor: pointer;
|
|
||||||
transition: border-color 0.25s;
|
|
||||||
}
|
|
||||||
button:hover {
|
|
||||||
border-color: #646cff;
|
|
||||||
}
|
|
||||||
button:focus,
|
|
||||||
button:focus-visible {
|
|
||||||
outline: 4px auto -webkit-focus-ring-color;
|
|
||||||
}
|
|
||||||
|
|
||||||
@media (prefers-color-scheme: light) {
|
|
||||||
:root {
|
|
||||||
color: #213547;
|
|
||||||
background-color: #ffffff;
|
|
||||||
}
|
|
||||||
a:hover {
|
|
||||||
color: #747bff;
|
|
||||||
}
|
|
||||||
button {
|
|
||||||
background-color: #f9f9f9;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
88
src/layouts/CommonLayout.jsx
Normal file
88
src/layouts/CommonLayout.jsx
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
import {
|
||||||
|
DesktopOutlined,
|
||||||
|
FileOutlined,
|
||||||
|
PieChartOutlined,
|
||||||
|
TeamOutlined,
|
||||||
|
UserOutlined,
|
||||||
|
} from "@ant-design/icons";
|
||||||
|
import { Layout, Menu, theme } from "antd";
|
||||||
|
import { Content, Header } from "antd/es/layout/layout";
|
||||||
|
import Sider from "antd/es/layout/Sider";
|
||||||
|
import React, { useState } from "react";
|
||||||
|
import { Outlet, useLocation } from "react-router-dom";
|
||||||
|
import menuConfig from "../config/menuConfig";
|
||||||
|
import { useSelector } from "react-redux";
|
||||||
|
import { selectUserRole } from "../features/auth/authSlice";
|
||||||
|
|
||||||
|
function getItem(label, key, icon, children) {
|
||||||
|
return {
|
||||||
|
key,
|
||||||
|
icon,
|
||||||
|
children,
|
||||||
|
label,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const items = [
|
||||||
|
getItem("Option 1", "1", <PieChartOutlined />),
|
||||||
|
getItem("Option 2", "2", <DesktopOutlined />),
|
||||||
|
getItem("User", "sub1", <UserOutlined />, [
|
||||||
|
getItem("Tom", "3"),
|
||||||
|
getItem("Bill", "4"),
|
||||||
|
getItem("Alex", "5"),
|
||||||
|
]),
|
||||||
|
getItem("Team", "sub2", <TeamOutlined />, [
|
||||||
|
getItem("Team 1", "6"),
|
||||||
|
getItem("Team 2", "8"),
|
||||||
|
]),
|
||||||
|
getItem("Files", "9", <FileOutlined />),
|
||||||
|
];
|
||||||
|
|
||||||
|
export default function CommonLayout() {
|
||||||
|
const location = useLocation();
|
||||||
|
|
||||||
|
const [collapsed, setCollapsed] = useState(false);
|
||||||
|
const {
|
||||||
|
token: { colorBgContainer, borderRadiusLG },
|
||||||
|
} = theme.useToken();
|
||||||
|
|
||||||
|
const roles = useSelector(selectUserRole);
|
||||||
|
|
||||||
|
const menu = menuConfig.filter(
|
||||||
|
(item) => !item.roles || item.roles.some((role) => roles.includes(role))
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Layout style={{ minHeight: "100vh" }}>
|
||||||
|
<Sider
|
||||||
|
collapsible
|
||||||
|
collapsed={collapsed}
|
||||||
|
onCollapse={(value) => setCollapsed(value)}
|
||||||
|
>
|
||||||
|
<div className="demo-logo-vertical" />
|
||||||
|
<Menu
|
||||||
|
theme="dark"
|
||||||
|
selectedKeys={[location.pathname]}
|
||||||
|
mode="inline"
|
||||||
|
items={menu.map((item) => ({
|
||||||
|
key: item.path,
|
||||||
|
label: item.label,
|
||||||
|
icon: React.createElement(item.icon),
|
||||||
|
}))}
|
||||||
|
/>
|
||||||
|
</Sider>
|
||||||
|
<Layout>
|
||||||
|
<Header style={{ padding: 0, background: colorBgContainer }} />
|
||||||
|
<Content
|
||||||
|
style={{
|
||||||
|
margin: "16px 16px",
|
||||||
|
background: colorBgContainer,
|
||||||
|
borderRadius: borderRadiusLG,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Outlet />
|
||||||
|
</Content>
|
||||||
|
</Layout>
|
||||||
|
</Layout>
|
||||||
|
);
|
||||||
|
}
|
21
src/main.jsx
21
src/main.jsx
@ -1,10 +1,15 @@
|
|||||||
import { StrictMode } from 'react'
|
import { StrictMode } from "react";
|
||||||
import { createRoot } from 'react-dom/client'
|
import { createRoot } from "react-dom/client";
|
||||||
import './index.css'
|
import { Provider } from "react-redux";
|
||||||
import App from './App.jsx'
|
import { RouterProvider } from "react-router-dom";
|
||||||
|
import "./index.css";
|
||||||
|
import router from "./router/index.jsx";
|
||||||
|
import { store } from "./store/index.js";
|
||||||
|
|
||||||
createRoot(document.getElementById('root')).render(
|
createRoot(document.getElementById("root")).render(
|
||||||
<StrictMode>
|
<StrictMode>
|
||||||
<App />
|
<Provider store={store}>
|
||||||
</StrictMode>,
|
<RouterProvider router={router} />
|
||||||
)
|
</Provider>
|
||||||
|
</StrictMode>
|
||||||
|
);
|
||||||
|
52
src/pages/Login.jsx
Normal file
52
src/pages/Login.jsx
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import { LockOutlined, UserOutlined } from "@ant-design/icons";
|
||||||
|
import { Button, Flex, Form, Input, message } from "antd";
|
||||||
|
import { useDispatch } from "react-redux";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import { login } from "../features/auth/authThunk";
|
||||||
|
|
||||||
|
export default function Login() {
|
||||||
|
const dispatch = useDispatch();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
const onFinish = async (values) => {
|
||||||
|
const res = await dispatch(login(values)).unwrap();
|
||||||
|
message.success("登录成功");
|
||||||
|
navigate("/user/reserve");
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="w-screen h-screen flex justify-center items-center flex-col">
|
||||||
|
<h1 className="m-6 text-gray-800 font-mono text-4xl">xxxx设备预约系统</h1>
|
||||||
|
<Form
|
||||||
|
name="login"
|
||||||
|
autoComplete="off"
|
||||||
|
initialValues={{ size: "large" }}
|
||||||
|
size="large"
|
||||||
|
onFinish={onFinish}
|
||||||
|
className="w-1/4"
|
||||||
|
>
|
||||||
|
<Form.Item
|
||||||
|
name="username"
|
||||||
|
rules={[{ required: true, message: "请输入用户名" }]}
|
||||||
|
>
|
||||||
|
<Input prefix={<UserOutlined />} placeholder="请输入用户名" />
|
||||||
|
</Form.Item>
|
||||||
|
|
||||||
|
<Form.Item
|
||||||
|
name="password"
|
||||||
|
rules={[{ required: true, message: "请输入密码" }]}
|
||||||
|
>
|
||||||
|
<Input.Password prefix={<LockOutlined />} placeholder="请输入密码" />
|
||||||
|
</Form.Item>
|
||||||
|
|
||||||
|
<Flex justify="flex-end">
|
||||||
|
<Form.Item label={null}>
|
||||||
|
<Button type="primary" htmlType="submit">
|
||||||
|
登录
|
||||||
|
</Button>
|
||||||
|
</Form.Item>
|
||||||
|
</Flex>
|
||||||
|
</Form>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
3
src/pages/user/Reserve.jsx
Normal file
3
src/pages/user/Reserve.jsx
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export default function Reserve() {
|
||||||
|
return <h1>Reserve</h1>;
|
||||||
|
}
|
15
src/router/ProtectedRoute.jsx
Normal file
15
src/router/ProtectedRoute.jsx
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import { useSelector } from "react-redux";
|
||||||
|
import { Navigate, Outlet } from "react-router-dom";
|
||||||
|
import { selectUserRole } from "../features/auth/authSlice";
|
||||||
|
|
||||||
|
export default function ProtectedRoute({ allowedRoles }) {
|
||||||
|
const roles = useSelector(selectUserRole);
|
||||||
|
|
||||||
|
if (roles.length === 0) return <Navigate to="/login" replace />;
|
||||||
|
|
||||||
|
if (roles.some((role) => allowedRoles.includes(role))) {
|
||||||
|
return <Outlet />;
|
||||||
|
} else {
|
||||||
|
return <Navigate to="/unauthorized" replace />;
|
||||||
|
}
|
||||||
|
}
|
34
src/router/index.jsx
Normal file
34
src/router/index.jsx
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import { createBrowserRouter, Navigate } from "react-router-dom";
|
||||||
|
import Login from "../pages/Login";
|
||||||
|
import ProtectedRoute from "./ProtectedRoute";
|
||||||
|
import CommonLayout from "../layouts/CommonLayout";
|
||||||
|
import Reserve from "../pages/user/Reserve";
|
||||||
|
|
||||||
|
const router = createBrowserRouter([
|
||||||
|
{
|
||||||
|
path: "/",
|
||||||
|
element: <Navigate to="/login" replace />,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "/login",
|
||||||
|
element: <Login />,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "/",
|
||||||
|
element: <CommonLayout />,
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
path: "user",
|
||||||
|
element: <ProtectedRoute allowedRoles={["USER"]} />,
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
path: "reserve",
|
||||||
|
element: <Reserve />,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
export default router;
|
23
src/store/index.js
Normal file
23
src/store/index.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import { configureStore } from "@reduxjs/toolkit";
|
||||||
|
import authReducer from "../features/auth/authSlice";
|
||||||
|
|
||||||
|
const userId = localStorage.getItem("userId");
|
||||||
|
const name = localStorage.getItem("name");
|
||||||
|
const roles = JSON.parse(localStorage.getItem("roles") || "[]");
|
||||||
|
const token = localStorage.getItem("token");
|
||||||
|
|
||||||
|
const preloadedState = {
|
||||||
|
auth: {
|
||||||
|
userId,
|
||||||
|
name,
|
||||||
|
roles,
|
||||||
|
token,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export const store = configureStore({
|
||||||
|
reducer: {
|
||||||
|
auth: authReducer,
|
||||||
|
},
|
||||||
|
preloadedState,
|
||||||
|
});
|
@ -1,7 +1,8 @@
|
|||||||
import { defineConfig } from 'vite'
|
import { defineConfig } from "vite";
|
||||||
import react from '@vitejs/plugin-react'
|
import react from "@vitejs/plugin-react";
|
||||||
|
import tailwindcss from "@tailwindcss/vite";
|
||||||
|
|
||||||
// https://vite.dev/config/
|
// https://vite.dev/config/
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [react()],
|
plugins: [react(), tailwindcss()],
|
||||||
})
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user