feat: 实现右上角用户名显示及登出
This commit is contained in:
parent
7e03278c37
commit
57505a0b40
@ -40,4 +40,6 @@ const authSlice = createSlice({
|
|||||||
|
|
||||||
export const { logout } = authSlice.actions;
|
export const { logout } = authSlice.actions;
|
||||||
export const selectUserRole = (state) => state.auth.roles;
|
export const selectUserRole = (state) => state.auth.roles;
|
||||||
|
export const selectUserName = (state) => state.auth.name;
|
||||||
|
export const selectUserId = (state) => state.auth.userId;
|
||||||
export default authSlice.reducer;
|
export default authSlice.reducer;
|
||||||
|
@ -1,22 +1,22 @@
|
|||||||
import {
|
import { DownOutlined } from "@ant-design/icons";
|
||||||
DesktopOutlined,
|
import { Dropdown, Layout, Menu, message, Space, theme } from "antd";
|
||||||
FileOutlined,
|
|
||||||
PieChartOutlined,
|
|
||||||
TeamOutlined,
|
|
||||||
UserOutlined,
|
|
||||||
} from "@ant-design/icons";
|
|
||||||
import { Layout, Menu, theme } from "antd";
|
|
||||||
import { Content, Header } from "antd/es/layout/layout";
|
import { Content, Header } from "antd/es/layout/layout";
|
||||||
import Sider from "antd/es/layout/Sider";
|
import Sider from "antd/es/layout/Sider";
|
||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
import { Outlet, useLocation, useNavigate } from "react-router-dom";
|
import { Outlet, useLocation, useNavigate } from "react-router-dom";
|
||||||
import menuConfig from "../config/menuConfig";
|
import menuConfig from "../config/menuConfig";
|
||||||
import { useSelector } from "react-redux";
|
import {
|
||||||
import { selectUserRole } from "../features/auth/authSlice";
|
logout,
|
||||||
|
selectUserName,
|
||||||
|
selectUserRole,
|
||||||
|
} from "../features/auth/authSlice";
|
||||||
|
|
||||||
export default function CommonLayout() {
|
export default function CommonLayout() {
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const naviagte = useNavigate();
|
const naviagte = useNavigate();
|
||||||
|
const dispatch = useDispatch();
|
||||||
|
const userName = useSelector(selectUserName);
|
||||||
|
|
||||||
const [collapsed, setCollapsed] = useState(false);
|
const [collapsed, setCollapsed] = useState(false);
|
||||||
const {
|
const {
|
||||||
@ -29,6 +29,21 @@ export default function CommonLayout() {
|
|||||||
(item) => !item.roles || item.roles.some((role) => roles.includes(role))
|
(item) => !item.roles || item.roles.some((role) => roles.includes(role))
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const dropDownItems = [
|
||||||
|
{
|
||||||
|
key: "logout",
|
||||||
|
label: "退出登录",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const handleMenuClick = ({ key }) => {
|
||||||
|
if (key === "logout") {
|
||||||
|
dispatch(logout());
|
||||||
|
message.success("已退出登录");
|
||||||
|
naviagte("/login");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Layout style={{ minHeight: "100vh" }}>
|
<Layout style={{ minHeight: "100vh" }}>
|
||||||
<Sider
|
<Sider
|
||||||
@ -50,7 +65,17 @@ export default function CommonLayout() {
|
|||||||
/>
|
/>
|
||||||
</Sider>
|
</Sider>
|
||||||
<Layout>
|
<Layout>
|
||||||
<Header style={{ padding: 0, background: colorBgContainer }} />
|
<Header
|
||||||
|
style={{ padding: 0, background: colorBgContainer }}
|
||||||
|
className="flex justify-end"
|
||||||
|
>
|
||||||
|
<Dropdown menu={{ items: dropDownItems, onClick: handleMenuClick }}>
|
||||||
|
<Space>
|
||||||
|
{userName}
|
||||||
|
<DownOutlined />
|
||||||
|
</Space>
|
||||||
|
</Dropdown>
|
||||||
|
</Header>
|
||||||
<Content
|
<Content
|
||||||
style={{
|
style={{
|
||||||
margin: "16px 16px",
|
margin: "16px 16px",
|
||||||
|
@ -4,6 +4,7 @@ import axiosInstance from "../../api/axios";
|
|||||||
import { useSelector } from "react-redux";
|
import { useSelector } from "react-redux";
|
||||||
import Column from "antd/es/table/Column";
|
import Column from "antd/es/table/Column";
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
|
import { selectUserId } from "../../features/auth/authSlice";
|
||||||
|
|
||||||
export default function MyReservation() {
|
export default function MyReservation() {
|
||||||
const [reservations, setReservations] = useState([]);
|
const [reservations, setReservations] = useState([]);
|
||||||
@ -13,7 +14,7 @@ export default function MyReservation() {
|
|||||||
total: 0,
|
total: 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
const userId = useSelector((state) => state.auth.userId);
|
const userId = useSelector(selectUserId);
|
||||||
|
|
||||||
const fetchData = async (pagination) => {
|
const fetchData = async (pagination) => {
|
||||||
const data = await axiosInstance.get(`/reservation/${userId}`, {
|
const data = await axiosInstance.get(`/reservation/${userId}`, {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user