Files
fe_service_ebtp_frontend/src/utils/authority.ts

55 lines
2.0 KiB
TypeScript
Raw Normal View History

2022-03-10 14:24:13 +08:00
//import { reloadAuthorized } from './Authorized';
import { getRA, getPurchaseCanOperate } from './session';
2020-12-23 11:14:35 +08:00
// use localStorage to store the authority info, which might be sent from server in actual project.
export function getAuthority(str?: string): string | string[] {
const authorityString =
2022-03-10 14:24:13 +08:00
typeof str === 'undefined' && sessionStorage ? sessionStorage.getItem('roleAuthority') : str;
2020-12-23 11:14:35 +08:00
// authorityString could be admin, "admin", ["admin"]
let authority;
try {
if (authorityString) {
authority = JSON.parse(authorityString);
}
} catch (e) {
authority = authorityString;
}
if (typeof authority === 'string') {
return [authority];
}
// preview.pro.ant.design only do not use in your production.
// preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。
2022-03-10 14:24:13 +08:00
// if (!authority && ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
// return ['admin'];
// }
2020-12-23 11:14:35 +08:00
return authority;
}
2022-03-10 14:24:13 +08:00
// export function setAuthority(authority: string | string[]): void {
// const proAuthority = typeof authority === 'string' ? [authority] : authority;
// sessionStorage.setItem('roleAuthority', JSON.stringify(proAuthority));
// // auto reload
// reloadAuthorized();
// }
//全局按钮控制 登陆角色是否显示功能 不显示return true
export function btnAuthority(allowedRoles: string[]) {
const roles = getRA();//当前登陆账号的角色数组
let control = true;
//根据session中的 采购经理是否可操作 属性,返回控制值
if (getPurchaseCanOperate() == null) {
control = false;
}
//根据传入得可操作角色 返回控制值
if (!control && allowedRoles?.length > 0 && roles != null) {
for (let index = 0; index < allowedRoles.length; index++) {
const element = allowedRoles[index];
control = !roles.includes(element);//传入的角色有一个有权限就跳出反false(显示)
if (!control) {
break;
}
}
}
return control;
2020-12-23 11:14:35 +08:00
}