Files
fe_service_ebtp_frontend/src/utils/authority.ts
2022-04-01 20:06:34 +08:00

56 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//import { reloadAuthorized } from './Authorized';
import { getRA, getPurchaseCanOperate } from './session';
// 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 =
typeof str === 'undefined' && sessionStorage ? sessionStorage.getItem('roleAuthority') : str;
// 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 专用环境变量,请不要在你的项目中使用它。
// if (!authority && ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
// return ['admin'];
// }
return authority;
}
// 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[], type?: string) {
const roles = getRA();//当前登陆账号的角色数组
let control = true;
//根据session中的 采购经理是否可操作 属性,返回控制值
if (getPurchaseCanOperate() == null && type != '1') {
control = false;
}
//根据传入得可操作角色 返回控制值
if ((!control || type=='1') && 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;
}