authority.js 1.0 KB
Newer Older
徐立's avatar
徐立 committed
1 2
// use localStorage to store the authority info, which might be sent from server in actual project.
export function getAuthority(str) {
3 4 5
	// return localStorage.getItem('antd-pro-authority') || ['admin', 'user'];
	const authorityString =
		typeof str === 'undefined' ? localStorage.getItem('antd-pro-authority') : str; // authorityString could be admin, "admin", ["admin"]
徐立's avatar
徐立 committed
6

7
	let authority;
徐立's avatar
徐立 committed
8

9 10 11 12 13 14 15
	try {
		if (authorityString) {
			authority = JSON.parse(authorityString);
		}
	} catch (e) {
		authority = authorityString;
	}
徐立's avatar
徐立 committed
16

17 18 19 20
	if (typeof authority === 'string') {
		return [authority];
	} // preview.pro.ant.design only do not use in your production.
	// preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。
徐立's avatar
徐立 committed
21

22 23 24
	if (!authority && ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
		return ['admin'];
	}
徐立's avatar
徐立 committed
25

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