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';
|
2020-12-03 21:49:32 +08:00
|
|
|
|
// import { RootRoute } from '/@/router/routes';
|
2020-11-18 22:41:59 +08:00
|
|
|
|
|
2021-03-08 21:19:09 +08:00
|
|
|
|
import projectSetting from '/@/settings/projectSetting';
|
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-12-03 21:49:32 +08:00
|
|
|
|
import { tabStore } from '/@/store/modules/tab';
|
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:
|
2021-03-08 21:19:09 +08:00
|
|
|
|
projectSetting.permissionMode === PermissionModeEnum.BACK
|
2020-09-28 20:19:10 +08:00
|
|
|
|
? PermissionModeEnum.ROLE
|
|
|
|
|
|
: PermissionModeEnum.BACK,
|
|
|
|
|
|
});
|
2020-12-03 21:49:32 +08:00
|
|
|
|
location.reload();
|
2020-09-28 20:19:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
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) {
|
2020-12-03 21:49:32 +08:00
|
|
|
|
tabStore.commitClearCache();
|
2020-09-28 20:19:10 +08:00
|
|
|
|
resetRouter();
|
|
|
|
|
|
const routes = await permissionStore.buildRoutesAction(id);
|
|
|
|
|
|
routes.forEach((route) => {
|
2021-02-25 20:17:08 +08:00
|
|
|
|
router.addRoute((route as unknown) as RouteRecordRaw);
|
2020-09-28 20:19:10 +08:00
|
|
|
|
});
|
|
|
|
|
|
permissionStore.commitLastBuildMenuTimeState();
|
2020-12-03 21:49:32 +08:00
|
|
|
|
const { closeAll } = useTabs();
|
|
|
|
|
|
closeAll();
|
2020-09-28 20:19:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
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 {
|
2021-03-08 21:19:09 +08:00
|
|
|
|
const permMode = projectSetting.permissionMode;
|
2020-09-28 20:19:10 +08:00
|
|
|
|
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)) {
|
2021-02-27 23:08:12 +08:00
|
|
|
|
return userStore.getRoleListState?.includes(value as RoleEnum);
|
2020-09-28 20:19:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
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> {
|
2021-03-08 21:19:09 +08:00
|
|
|
|
if (projectSetting.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 don’t 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 };
|
|
|
|
|
|
}
|