Commit 77d714f2 authored by 陈帅's avatar 陈帅

fix authority parse error

parent 1dd1fb4f
...@@ -9,6 +9,7 @@ export interface IExceptionProps { ...@@ -9,6 +9,7 @@ export interface IExceptionProps {
style?: React.CSSProperties; style?: React.CSSProperties;
className?: string; className?: string;
backText?: React.ReactNode; backText?: React.ReactNode;
redirect?: string;
} }
export default class Exception extends React.Component<IExceptionProps, any> {} export default class Exception extends React.Component<IExceptionProps, any> {}
...@@ -17,3 +17,4 @@ desc | supplementary description | ReactNode | - ...@@ -17,3 +17,4 @@ desc | supplementary description | ReactNode | -
img | the url of background image | string | - img | the url of background image | string | -
actions | suggested operations, a default 'Home' link will show if not set | ReactNode | - actions | suggested operations, a default 'Home' link will show if not set | ReactNode | -
linkElement | to specify the element of link | string\|ReactElement | 'a' linkElement | to specify the element of link | string\|ReactElement | 'a'
redirect | redirect path | string | '/'
\ No newline at end of file
...@@ -7,6 +7,7 @@ import styles from './index.less'; ...@@ -7,6 +7,7 @@ import styles from './index.less';
class Exception extends React.PureComponent { class Exception extends React.PureComponent {
static defaultProps = { static defaultProps = {
backText: 'back to home', backText: 'back to home',
redirect: '/',
}; };
constructor(props) { constructor(props) {
...@@ -24,6 +25,7 @@ class Exception extends React.PureComponent { ...@@ -24,6 +25,7 @@ class Exception extends React.PureComponent {
desc, desc,
img, img,
actions, actions,
redirect,
...rest ...rest
} = this.props; } = this.props;
const pageType = type in config ? type : '404'; const pageType = type in config ? type : '404';
...@@ -44,8 +46,8 @@ class Exception extends React.PureComponent { ...@@ -44,8 +46,8 @@ class Exception extends React.PureComponent {
createElement( createElement(
linkElement, linkElement,
{ {
to: '/', to: redirect,
href: '/', href: redirect,
}, },
<Button type="primary">{backText}</Button> <Button type="primary">{backText}</Button>
)} )}
......
...@@ -18,3 +18,4 @@ order: 5 ...@@ -18,3 +18,4 @@ order: 5
| img | 背景图片地址 | string| -| | img | 背景图片地址 | string| -|
| actions | 建议操作,配置此属性时默认的『返回首页』按钮不生效| ReactNode| -| | actions | 建议操作,配置此属性时默认的『返回首页』按钮不生效| ReactNode| -|
| linkElement | 定义链接的元素 | string\|ReactElement | 'a' | | linkElement | 定义链接的元素 | string\|ReactElement | 'a' |
| redirect | 返回按钮的跳转地址 | string | '/'
...@@ -15,12 +15,13 @@ export default ({ children }) => { ...@@ -15,12 +15,13 @@ export default ({ children }) => {
type="403" type="403"
desc={formatMessage({ id: 'app.exception.description.403' })} desc={formatMessage({ id: 'app.exception.description.403' })}
linkElement={Link} linkElement={Link}
backText={formatMessage({ id: 'app.exception.back' })} redirect="/user/login"
backText="back to login"
/> />
); );
// if Authority === ['guest'] redirect to /user/login // if Authority === ['guest'] redirect to /user/login
// You can implement the logic here. // You can implement the logic here.
if (Authority.join('') === 'guest') { if (Authority === 'guest' || Authority.join('') === 'guest') {
noMatch = <Redirect to="/user/login" />; noMatch = <Redirect to="/user/login" />;
} }
return ( return (
......
import { isJsonString } from '@/utils/utils';
// use localStorage to store the authority info, which might be sent from server in actual project. // use localStorage to store the authority info, which might be sent from server in actual project.
export function getAuthority() { export function getAuthority() {
// return localStorage.getItem('antd-pro-authority') || ['admin', 'user']; // return localStorage.getItem('antd-pro-authority') || ['admin', 'user'];
const authorityString = localStorage.getItem('antd-pro-authority'); const authorityString = localStorage.getItem('antd-pro-authority');
let authority; let authority;
try { if (isJsonString(authorityString)) {
authority = JSON.parse(authorityString); authority = JSON.parse(authorityString);
} catch (e) { } else {
authority = [authorityString]; authority = [authorityString];
} }
return authority || ['admin']; return authority || ['admin'];
} }
export function setAuthority(authority) { export function setAuthority(authority) {
return localStorage.setItem('antd-pro-authority', JSON.stringify(authority)); let authorityString;
if (isJsonString(authority)) {
authorityString = JSON.stringify(authority);
} else {
authorityString = [authority];
}
return localStorage.setItem('antd-pro-authority', authorityString);
} }
...@@ -181,3 +181,14 @@ export function formatWan(val) { ...@@ -181,3 +181,14 @@ export function formatWan(val) {
export function isAntdPro() { export function isAntdPro() {
return window.location.hostname === 'preview.pro.ant.design'; return window.location.hostname === 'preview.pro.ant.design';
} }
export function isJsonString(str) {
try {
if (typeof JSON.parse(str) === 'object') {
return true;
}
} catch (e) {
return false;
}
return false;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment