authority.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import { reloadAuthorized } from './Authorized' // use localStorage to store the authority info, which might be sent from server in actual project.
  2. export function getAuthority(str) {
  3. const authorityString =
  4. typeof str === 'undefined' && localStorage ? localStorage.getItem('antd-pro-authority') : str // authorityString could be admin, "admin", ["admin"]
  5. let authority
  6. try {
  7. if (authorityString) {
  8. authority = JSON.parse(authorityString)
  9. }
  10. } catch (e) {
  11. authority = authorityString
  12. }
  13. if (typeof authority === 'string') {
  14. return [ authority ]
  15. } // preview.pro.ant.design only do not use in your production.
  16. // 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. localStorage.setItem('antd-pro-authority', JSON.stringify(proAuthority)) // auto reload
  25. reloadAuthorized()
  26. }