authority.ts 1.12 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
  }
18 19
  // preview.pro.ant.design only do not use in your production.
  // preview.pro.ant.design ไธ“็”จ็Žฏๅขƒๅ˜้‡๏ผŒ่ฏทไธ่ฆๅœจไฝ ็š„้กน็›ฎไธญไฝฟ็”จๅฎƒใ€‚
20
  if (!authority && ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
21 22
    return ['admin'];
  }
23
  return authority;
ddcat1115's avatar
ddcat1115 committed
24 25
}

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