authority.js 1.03 KB
Newer Older
ddcat1115's avatar
ddcat1115 committed
1
// use localStorage to store the authority info, which might be sent from server in actual project.
2
export function getAuthority(str) {
3
  // return localStorage.getItem('antd-pro-authority') || ['admin', 'user'];
4 5 6
  const authorityString =
    typeof str === 'undefined' ? localStorage.getItem('antd-pro-authority') : str;
  // authorityString could be admin, "admin", ["admin"]
afc163's avatar
afc163 committed
7
  let authority;
8
  try {
afc163's avatar
afc163 committed
9
    authority = JSON.parse(authorityString);
10 11 12 13 14
  } catch (e) {
    authority = authorityString;
  }
  if (typeof authority === 'string') {
    return [authority];
陈帅's avatar
陈帅 committed
15
  }
16 17
  // 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') {
陈帅's avatar
陈帅 committed
18 19
    return ['admin'];
  }
陈小聪's avatar
陈小聪 committed
20
  return authority;
ddcat1115's avatar
ddcat1115 committed
21 22
}
export function setAuthority(authority) {
23 24
  const proAuthority = typeof authority === 'string' ? [authority] : authority;
  return localStorage.setItem('antd-pro-authority', JSON.stringify(proAuthority));
ddcat1115's avatar
ddcat1115 committed
25
}