|
|
@@ -1,24 +1,22 @@
|
|
|
-import { isJsonString } from '@/utils/utils';
|
|
|
-
|
|
|
// use localStorage to store the authority info, which might be sent from server in actual project.
|
|
|
-export function getAuthority() {
|
|
|
+export function getAuthority(str) {
|
|
|
// return localStorage.getItem('antd-pro-authority') || ['admin', 'user'];
|
|
|
- const authorityString = localStorage.getItem('antd-pro-authority');
|
|
|
+ const authorityString =
|
|
|
+ typeof str === 'undefined' ? localStorage.getItem('antd-pro-authority') : str;
|
|
|
+ // authorityString could be admin, "admin", ["admin"]
|
|
|
let authority;
|
|
|
- if (isJsonString(authorityString)) {
|
|
|
+ try {
|
|
|
authority = JSON.parse(authorityString);
|
|
|
- } else {
|
|
|
- authority = [authorityString];
|
|
|
+ } catch (e) {
|
|
|
+ authority = authorityString;
|
|
|
+ }
|
|
|
+ if (typeof authority === 'string') {
|
|
|
+ return [authority];
|
|
|
}
|
|
|
return authority || ['admin'];
|
|
|
}
|
|
|
|
|
|
export function setAuthority(authority) {
|
|
|
- let authorityString;
|
|
|
- if (isJsonString(authority)) {
|
|
|
- authorityString = JSON.stringify(authority);
|
|
|
- } else {
|
|
|
- authorityString = [authority];
|
|
|
- }
|
|
|
- return localStorage.setItem('antd-pro-authority', authorityString);
|
|
|
+ const proAuthority = typeof authority === 'string' ? [authority] : authority;
|
|
|
+ return localStorage.setItem('antd-pro-authority', JSON.stringify(proAuthority));
|
|
|
}
|