authority.js 811 B

12345678910111213141516171819202122
  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. const authorityString =
  5. typeof str === 'undefined' ? localStorage.getItem('antd-pro-authority') : str;
  6. // authorityString could be admin, "admin", ["admin"]
  7. let authority;
  8. try {
  9. authority = JSON.parse(authorityString);
  10. } catch (e) {
  11. authority = authorityString;
  12. }
  13. if (typeof authority === 'string') {
  14. return [authority];
  15. }
  16. return authority || ['admin'];
  17. }
  18. export function setAuthority(authority) {
  19. const proAuthority = typeof authority === 'string' ? [authority] : authority;
  20. return localStorage.setItem('antd-pro-authority', JSON.stringify(proAuthority));
  21. }