Skip to content

Commit

Permalink
新增 窗口右上角的头像下拉菜单,切换当前用户所属岗位,并联动切换当前菜单权限
Browse files Browse the repository at this point in the history
  • Loading branch information
think-gem committed Nov 7, 2024
1 parent 2986547 commit ab8436d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 28 deletions.
12 changes: 10 additions & 2 deletions src/api/sys/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface LoginResult {
msgEnabled: boolean;
sysCode: string;
roleCode: string;
postCode: string;
title: string;
company: string;
version: string;
Expand All @@ -60,11 +61,18 @@ export const loginApi = (params: LoginParams, mode: ErrorMessageMode = 'none') =
};

export const switchSys = (sysCode: string) => {
return defHttp.get({ url: adminPath + '/switch/' + sysCode });
const params = sysCode ? '/' + sysCode : sysCode;
return defHttp.get({ url: adminPath + '/switch' + params });
};

export const switchRole = (roleCode: string) => {
return defHttp.get({ url: adminPath + '/switchRole/' + roleCode });
const params = roleCode ? '/' + roleCode : roleCode;
return defHttp.get({ url: adminPath + '/switchRole' + params });
};

export const switchPost = (postCode: string) => {
const params = postCode ? '/' + postCode : postCode;
return defHttp.get({ url: adminPath + '/switchPost' + params });
};

export const switchSkin = (name = '') => {
Expand Down
90 changes: 65 additions & 25 deletions src/layouts/default/header/components/user-dropdown/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,52 @@
:text="item.name"
:icon="sysCodeRef == item.value ? 'i-ant-design:check-outlined' : 'i-radix-icons:dot'"
/>
<MenuDivider v-if="getUserInfo.roleList.length > 0" />
<MenuItem
v-if="getUserInfo.roleList.length > 0"
:class="`${prefixCls}-menu-subtitle`"
:text="t('选择身份:')"
>
<template #menuItemAfter>
<Icon
v-if="roleCodeRef"
icon="i-ant-design:close-circle-outlined"
class="ml-1"
@click="handleMenuClick({ key: 'roleCode-' })"
:title="t('取消设置')"
/>
</template>
</MenuItem>
<MenuItem
v-for="item in getUserInfo.roleList"
:key="item.roleCode"
:value="'roleCode-' + item.roleCode"
:text="item.roleName"
:icon="roleCodeRef == item.roleCode ? 'i-ant-design:check-outlined' : 'i-radix-icons:dot'"
/>
<template v-if="getUserInfo.postList.length > 0">
<MenuDivider />
<MenuItem :class="`${prefixCls}-menu-subtitle`" :text="t('选择岗位:')">
<template #menuItemAfter>
<Icon
v-if="postCodeRef"
icon="i-ant-design:close-circle-outlined"
class="ml-1"
@click="handleMenuClick({ key: 'postCode-' })"
:title="t('取消设置')"
/>
</template>
</MenuItem>
<MenuItem
v-for="item in getUserInfo.postList"
:key="item.postCode"
:value="'postCode-' + item.postCode"
:text="item.postName"
:icon="
postCodeRef == item.postCode ? 'i-ant-design:check-outlined' : 'i-radix-icons:dot'
"
/>
</template>
<template v-else-if="getUserInfo.roleList.length > 0">
<MenuDivider />
<MenuItem :class="`${prefixCls}-menu-subtitle`" :text="t('选择身份:')">
<template #menuItemAfter>
<Icon
v-if="roleCodeRef"
icon="i-ant-design:close-circle-outlined"
class="ml-1"
@click="handleMenuClick({ key: 'roleCode-' })"
:title="t('取消设置')"
/>
</template>
</MenuItem>
<MenuItem
v-for="item in getUserInfo.roleList"
:key="item.roleCode"
:value="'roleCode-' + item.roleCode"
:text="item.roleName"
:icon="
roleCodeRef == item.roleCode ? 'i-ant-design:check-outlined' : 'i-radix-icons:dot'
"
/>
</template>
</Menu>
</template>
</Dropdown>
Expand All @@ -105,7 +128,7 @@
import { propTypes } from '/@/utils/propTypes';
import { openWindow } from '/@/utils';
import { useDict } from '/@/components/Dict';
import { switchSys, switchRole } from '/@/api/sys/login';
import { switchSys, switchRole, switchPost } from '/@/api/sys/login';
import { PageEnum } from '/@/enums/pageEnum';
import { Icon } from '/@/components/Icon';
import MenuItem from './DropMenuItem.vue';
Expand Down Expand Up @@ -139,21 +162,30 @@
const sysCodeRef = ref<string>('default');
const sysListRef = ref<Recordable[]>([]);
const roleCodeRef = ref<string>('');
const postCodeRef = ref<string>('');
const getUserInfo = computed(() => {
const { userName = '', avatarUrl, remarks, roleList } = userStore.getUserInfo || {};
const {
userName = '',
avatarUrl,
remarks,
roleList,
postList,
} = userStore.getUserInfo || {};
return {
userName,
avatarUrl,
remarks,
roleList: (roleList || []).filter((e) => e.isShow == '1'),
postList: postList || [],
};
});
if (!props.sidebar) {
onMounted(async () => {
sysCodeRef.value = userStore.getPageCacheByKey('sysCode', 'default');
roleCodeRef.value = userStore.getPageCacheByKey('roleCode', '');
postCodeRef.value = userStore.getPageCacheByKey('postCode', '');
const sysList = await useDict().initGetDictList('sys_menu_sys_code');
if (sysList.length > 1) {
var sysCodes: string[] = [];
Expand Down Expand Up @@ -224,6 +256,13 @@
await switchRole(roleCode);
location.reload();
}
const postCodePrefix = 'postCode-';
if (String(e.key).startsWith(postCodePrefix)) {
go(userStore.getUserInfo.homePath || PageEnum.BASE_HOME);
const postCode = String(e.key).substring(postCodePrefix.length);
await switchPost(postCode);
location.reload();
}
break;
}
}
Expand All @@ -240,6 +279,7 @@
sysCodeRef,
sysListRef,
roleCodeRef,
postCodeRef,
props,
};
},
Expand Down
2 changes: 2 additions & 0 deletions src/store/modules/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const useUserStore = defineStore({
info.avatarUrl = url || logoImg;
info.homePath = res.desktopUrl;
info.roleList = res.roleList;
info.postList = res.postList;
}
this.userInfo = info;
this.lastUpdateTime = new Date().getTime();
Expand Down Expand Up @@ -190,6 +191,7 @@ export const useUserStore = defineStore({
this.setPageCache('modifyPasswordMsg', res.modifyPasswordMsg);
this.setPageCache('sysCode', res.sysCode);
this.setPageCache('roleCode', res.roleCode);
this.setPageCache('postCode', res.postCode);
this.setPageCache('title', res.title);
this.setPageCache('company', res.company);
this.setPageCache('version', res.version);
Expand Down
2 changes: 1 addition & 1 deletion src/views/sys/empUser/list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
<Popconfirm :title="t('是否确认删除选中的用户吗?')" @confirm="handleDeleteSelected()">
<a-button
danger
class="ml-4"
type="default"
v-if="selectedRowKeysRef.length > 0"
v-auth="'sys:empUser:edit'"
class="ml-4"
>
<Icon icon="i-ant-design:delete-outlined" color="error" /> {{ t('删除') }} ({{
selectedRowKeysRef.length
Expand Down
1 change: 1 addition & 0 deletions types/store.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface UserInfo {
avatarUrl: string;
remarks?: string;
roleList?: any[];
postList?: any[];
homePath?: string;
// roles: RoleInfo[];
}
Expand Down

0 comments on commit ab8436d

Please sign in to comment.