authority.js 1.0 KB

12345678910111213141516171819202122232425
  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. // preview.pro.ant.design only do not use in your production ; preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。
  17. if (!authority && ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
  18. return ['admin'];
  19. }
  20. return authority;
  21. }
  22. export function setAuthority(authority) {
  23. const proAuthority = typeof authority === 'string' ? [authority] : authority;
  24. return localStorage.setItem('antd-pro-authority', JSON.stringify(proAuthority));
  25. }