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
|
2022-04-01 20:06:34 +08:00
|
|
|
|
export function btnAuthority(allowedRoles: string[], type?: string) {
|
2022-03-10 14:24:13 +08:00
|
|
|
|
const roles = getRA();//当前登陆账号的角色数组
|
|
|
|
|
let control = true;
|
2022-04-01 20:06:34 +08:00
|
|
|
|
|
2022-03-10 14:24:13 +08:00
|
|
|
|
//根据session中的 采购经理是否可操作 属性,返回控制值
|
2022-04-01 20:06:34 +08:00
|
|
|
|
if (getPurchaseCanOperate() == null && type != '1') {
|
2022-03-10 14:24:13 +08:00
|
|
|
|
control = false;
|
|
|
|
|
}
|
|
|
|
|
//根据传入得可操作角色 返回控制值
|
2022-04-01 20:06:34 +08:00
|
|
|
|
if ((!control || type=='1') && allowedRoles?.length > 0 && roles != null) {
|
2022-03-10 14:24:13 +08:00
|
|
|
|
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
|
|
|
|
}
|