authority.js 991 Bytes
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
	const authorityString =
		typeof str === 'undefined' ? localStorage.getItem('antd-pro-authority') : str; // authorityString could be admin, "admin", ["admin"]
徐立's avatar
徐立 committed
5

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

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

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

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

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