authority.ts 1.25 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?: string): string | string[] {
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 {
9 10 11
    if (authorityString) {
      authority = JSON.parse(authorityString);
    }
12 13 14 15 16
  } catch (e) {
    authority = authorityString;
  }
  if (typeof authority === 'string') {
    return [authority];
17
  }
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
18 19 20 21
  console.log(
    'ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION',
    ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION,
  );
22 23
  // preview.pro.ant.design only do not use in your production.
  // preview.pro.ant.design ไธ“็”จ็Žฏๅขƒๅ˜้‡๏ผŒ่ฏทไธ่ฆๅœจไฝ ็š„้กน็›ฎไธญไฝฟ็”จๅฎƒใ€‚
24
  if (!authority && ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
25 26
    return ['admin'];
  }
27
  return authority;
ddcat1115's avatar
ddcat1115 committed
28 29
}

30
export function setAuthority(authority: string | string[]): void {
31 32
  const proAuthority = typeof authority === 'string' ? [authority] : authority;
  return localStorage.setItem('antd-pro-authority', JSON.stringify(proAuthority));
ddcat1115's avatar
ddcat1115 committed
33
}