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);