From d94cbe2aeeb8cdfa9492e28f9e099f9d3ae390fd Mon Sep 17 00:00:00 2001 From: Kven <2955163637@qq.com> Date: Thu, 6 Feb 2025 14:08:24 +0800 Subject: [PATCH] =?UTF-8?q?feat(user):=20=E6=B7=BB=E5=8A=A0=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E8=A7=92=E8=89=B2=E5=88=87=E6=8D=A2=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E5=B9=B6=E8=B0=83=E6=95=B4=E5=AF=BC=E8=88=AA=E6=A0=8F=E5=B8=83?= =?UTF-8?q?=E5=B1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 user store 中添加 switchRole action用于切换用户角色 - 在导航栏中加入角色切换功能,允许用户在管理员和经理角色之间切换 - 优化导航栏布局,暂时注释掉公告通知按钮- 更新 API 调用,添加新的 switchRole 函数 --- src/api/user.ts | 11 +++++-- src/components/navbar/index.vue | 58 +++++++++++++++++++++++---------- src/store/modules/user/index.ts | 14 ++++++++ 3 files changed, 63 insertions(+), 20 deletions(-) diff --git a/src/api/user.ts b/src/api/user.ts index 2ac9554..5088166 100644 --- a/src/api/user.ts +++ b/src/api/user.ts @@ -117,6 +117,11 @@ export function create(data: CreateRecord) { return axios.post('/api/rest/user', data); } +// 用户切换角色 +export function switchRole(id: number) { + return axios.patch(`/api/rest/user/switch/${id}`); +} + // 模糊查询用户列表 export function queryUserList(params: any) { return axios({ @@ -168,9 +173,9 @@ export function code(data: string) { return axios.get(`/api/rest/user/send-email?email=${data}`); } -export function switchRole(roleId: number) { - return axios.patch(`/api/user/self/switch-role/${roleId}`); -} +// export function switchRole(roleId: number) { +// return axios.patch(`/api/user/self/switch-role/${roleId}`); +// } export function getUserDetail(id: number) { return axios.get(`/api/user/${id}`); diff --git a/src/components/navbar/index.vue b/src/components/navbar/index.vue index a29e1e2..87500c6 100644 --- a/src/components/navbar/index.vue +++ b/src/components/navbar/index.vue @@ -21,21 +21,21 @@
    -
  • - - - + + + + + + + + + + + - - -
  • + + +
  • @@ -150,9 +150,28 @@
  • - - - +
  • + + + {{ userStore.role.name }} + + +
  • { triggerBtn.value.dispatchEvent(event); }; +// 切换角色 +const handleSwitchRole = (roleId: number) => { + userStore.switchRole(roleId); +}; + const toggleDrawerMenu = inject('toggleDrawerMenu') as () => void; diff --git a/src/store/modules/user/index.ts b/src/store/modules/user/index.ts index 498ed4e..62cbaf4 100644 --- a/src/store/modules/user/index.ts +++ b/src/store/modules/user/index.ts @@ -15,6 +15,7 @@ import { userDetail, deptAudit, code, + switchRole } from '@/api/user'; import { setToken, clearToken } from '@/utils/auth'; import { removeRouteListener } from '@/utils/route-listener'; @@ -45,6 +46,19 @@ const useUserStore = defineStore('user', { }, actions: { + // 用户切换角色 + async switchRole(role: number) { + const res = await switchRole(role); + this.resetInfo(); + // this.setInfo(res.data.user); + // // 检查并存储新的 Token + // if (res.data.token) { + // setToken(res.data.token); + // } + window.location.reload(); + }, + + // Set user's information setInfo(partial: Partial) { this.$patch(partial);