vue-vben-admin/src/hooks/web/usePermission.ts

109 lines
3.1 KiB
TypeScript
Raw Normal View History

2020-11-18 22:41:59 +08:00
import type { RouteRecordRaw } from 'vue-router';
2020-11-12 22:20:15 +08:00
import { appStore } from '/@/store/modules/app';
2020-09-28 20:19:10 +08:00
import { permissionStore } from '/@/store/modules/permission';
2020-11-18 22:41:59 +08:00
import { userStore } from '/@/store/modules/user';
2020-09-28 20:19:10 +08:00
import { useTabs } from './useTabs';
2020-11-18 22:41:59 +08:00
2020-09-28 20:19:10 +08:00
import router, { resetRouter } from '/@/router';
import { RootRoute } from '/@/router/routes';
2020-11-18 22:41:59 +08:00
2020-09-28 20:19:10 +08:00
import { PermissionModeEnum } from '/@/enums/appEnum';
2020-11-18 22:41:59 +08:00
import { RoleEnum } from '/@/enums/roleEnum';
2020-09-28 20:19:10 +08:00
import { intersection } from 'lodash-es';
2020-11-18 22:41:59 +08:00
import { isArray } from '/@/utils/is';
2020-09-28 20:19:10 +08:00
2020-11-18 22:41:59 +08:00
// User permissions related operations
2020-09-28 20:19:10 +08:00
export function usePermission() {
/**
2020-11-18 22:41:59 +08:00
* Change permission mode
2020-09-28 20:19:10 +08:00
*/
async function togglePermissionMode() {
appStore.commitProjectConfigState({
permissionMode:
appStore.getProjectConfig.permissionMode === PermissionModeEnum.BACK
? PermissionModeEnum.ROLE
: PermissionModeEnum.BACK,
});
resume();
// location.reload();
}
2020-11-18 22:41:59 +08:00
/**
* Reset and regain authority resource information
* @param id
*/
2020-09-28 20:19:10 +08:00
async function resume(id?: string | number) {
resetRouter();
const routes = await permissionStore.buildRoutesAction(id);
routes.forEach((route) => {
router.addRoute(RootRoute.name!, route as RouteRecordRaw);
});
permissionStore.commitLastBuildMenuTimeState();
const {
// closeAll,
closeOther,
} = useTabs();
// closeAll();
closeOther();
}
/**
2020-11-18 22:41:59 +08:00
* Determine whether there is permission
2020-09-28 20:19:10 +08:00
*/
function hasPermission(value?: RoleEnum | RoleEnum[] | string | string[], def = true): boolean {
const permMode = appStore.getProjectConfig.permissionMode;
if (PermissionModeEnum.ROLE === permMode) {
2020-11-18 22:41:59 +08:00
// Visible by default
2020-09-28 20:19:10 +08:00
if (!value) {
return def;
}
if (!isArray(value)) {
return userStore.getRoleListState.includes(value as RoleEnum);
}
return (intersection(value, userStore.getRoleListState) as RoleEnum[]).length > 0;
}
if (PermissionModeEnum.BACK === permMode) {
2020-11-18 22:41:59 +08:00
// Visible by default
2020-09-28 20:19:10 +08:00
if (!value) {
return def;
}
const allCodeList = permissionStore.getPermCodeListState;
if (!isArray(value)) {
return allCodeList.includes(value as string);
}
return (intersection(value, allCodeList) as string[]).length > 0;
}
return true;
}
/**
2020-11-18 22:41:59 +08:00
* Change roles
2020-09-28 20:19:10 +08:00
* @param roles
*/
async function changeRole(roles: RoleEnum | RoleEnum[]): Promise<void> {
if (appStore.getProjectConfig.permissionMode !== PermissionModeEnum.ROLE) {
2020-11-18 22:41:59 +08:00
throw new Error(
'Please switch PermissionModeEnum to ROLE mode in the configuration to operate!'
);
2020-09-28 20:19:10 +08:00
}
if (!isArray(roles)) {
roles = [roles];
}
userStore.commitRoleListState(roles);
await resume();
}
/**
2020-11-18 22:41:59 +08:00
* Change menu
2020-09-28 20:19:10 +08:00
*/
async function changeMenu(id?: string | number) {
2020-11-18 22:41:59 +08:00
// TODO The id passed in here is for testing. Actually, you dont need to pass it. The id of the login person will be automatically obtained.
2020-09-28 20:19:10 +08:00
resume(id);
}
return { changeRole, hasPermission, togglePermissionMode, changeMenu };
}