From 59cf11be7abea85708926f32aa7d3566e257daef Mon Sep 17 00:00:00 2001 From: zhangchuanlong Date: Thu, 17 Feb 2022 19:02:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=E5=88=87=E6=8D=A2?= =?UTF-8?q?=E8=A7=92=E8=89=B2=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/layout/components/header/HeaderAction.vue | 51 +++++++++++++++++-- src/layout/components/sidebar/SideMenu.vue | 5 +- src/router/index.js | 7 +-- src/store/modules/user.js | 3 ++ 4 files changed, 57 insertions(+), 9 deletions(-) diff --git a/src/layout/components/header/HeaderAction.vue b/src/layout/components/header/HeaderAction.vue index 6175360..18587c2 100644 --- a/src/layout/components/header/HeaderAction.vue +++ b/src/layout/components/header/HeaderAction.vue @@ -2,11 +2,17 @@ import { useUserStore } from '@/store/modules/user' import { useRouter } from 'vue-router' import { NDropdown } from 'naive-ui' +import { resetRouter } from '@/router' +import { usePermissionStore } from '@/store/modules/permission' const userStore = useUserStore() const router = useRouter() const options = [ + { + label: '切换角色', + key: 'switchRole', + }, { label: '退出登录', key: 'logout', @@ -15,11 +21,50 @@ const options = [ function handleSelect(key) { if (key === 'logout') { - userStore.logout() - $message.success('已退出登录') - router.push({ path: '/login' }) + logout() + } else if (key === 'switchRole') { + switchRole() } } + +function logout() { + userStore.logout() + $message.success('已退出登录') + router.push({ path: '/login' }) +} + +function switchRole() { + const permissionStore = usePermissionStore() + + const users = [ + { + id: 1, + name: '大脸怪(admin)', + avatar: 'https://gitee.com/zclzone/res/raw/master/qs-zone/blob/img/lADPDiQ3QDTwsz3NAarNAaw_428_426.jpg', + email: 'Ronnie@123.com', + role: ['admin'], + }, + { + id: 2, + name: '大脸怪(editor)', + avatar: 'https://gitee.com/zclzone/res/raw/master/qs-zone/blob/img/lADPDiQ3QDTwsz3NAarNAaw_428_426.jpg', + email: 'Ronnie@123.com', + role: ['editor'], + }, + { + id: 3, + name: '访客(guest)', + avatar: 'https://gitee.com/zclzone/res/raw/master/qs-zone/blob/img/lADPDiQ3QDTwsz3NAarNAaw_428_426.jpg', + role: [], + }, + ] + + const switchUser = users[+userStore.userId % users.length] + resetRouter() + userStore.setUserInfo(switchUser) + permissionStore.generateRoutes(switchUser.role) + $message.success(`${switchUser.name}`) +}