diff --git a/src/components/Exception/index.d.ts b/src/components/Exception/index.d.ts index cf40d6ac8bae2ef2f35dda7c553b9674ccbc8e43..d62c177193d067f19484b3b40b591de7a3ccb86b 100644 --- a/src/components/Exception/index.d.ts +++ b/src/components/Exception/index.d.ts @@ -9,6 +9,7 @@ export interface IExceptionProps { style?: React.CSSProperties; className?: string; backText?: React.ReactNode; + redirect?: string; } export default class Exception extends React.Component {} diff --git a/src/components/Exception/index.en-US.md b/src/components/Exception/index.en-US.md index 10f23a0a6a83f0d6a54b25c7225de76ae9ff763b..37e7e80756fb7517239658775e4ce5ef170dcb21 100644 --- a/src/components/Exception/index.en-US.md +++ b/src/components/Exception/index.en-US.md @@ -16,4 +16,5 @@ title | title | ReactNode | - desc | supplementary description | ReactNode | - img | the url of background image | string | - actions | suggested operations, a default 'Home' link will show if not set | ReactNode | - -linkElement | to specify the element of link | string\|ReactElement | 'a' \ No newline at end of file +linkElement | to specify the element of link | string\|ReactElement | 'a' +redirect | redirect path | string | '/' \ No newline at end of file diff --git a/src/components/Exception/index.js b/src/components/Exception/index.js index 5291d579fb1f6deaeb95fb2dcefa36be4e2d3ca4..2c7223cc7655ce457ef1c597dc3fe8ef86b49c49 100644 --- a/src/components/Exception/index.js +++ b/src/components/Exception/index.js @@ -7,6 +7,7 @@ import styles from './index.less'; class Exception extends React.PureComponent { static defaultProps = { backText: 'back to home', + redirect: '/', }; constructor(props) { @@ -24,6 +25,7 @@ class Exception extends React.PureComponent { desc, img, actions, + redirect, ...rest } = this.props; const pageType = type in config ? type : '404'; @@ -44,8 +46,8 @@ class Exception extends React.PureComponent { createElement( linkElement, { - to: '/', - href: '/', + to: redirect, + href: redirect, }, )} diff --git a/src/components/Exception/index.zh-CN.md b/src/components/Exception/index.zh-CN.md index bc7e5f1d8971498db76fb030a07cd362052464b5..2e64399fcbe4fe2c0eca7b9c4cb7742ccef3ed9a 100644 --- a/src/components/Exception/index.zh-CN.md +++ b/src/components/Exception/index.zh-CN.md @@ -18,3 +18,4 @@ order: 5 | img | 背景图片地址 | string| -| | actions | 建议操作,配置此属性时默认的『返回首页』按钮不生效| ReactNode| -| | linkElement | 定义链接的元素 | string\|ReactElement | 'a' | +| redirect | 返回按钮的跳转地址 | string | '/' diff --git a/src/pages/Authorized.js b/src/pages/Authorized.js index 238888a7bc750647e1fb25aaec763e7782eb03b7..e1ad24a17e88f2b88a08dd8eb296073548474ff5 100644 --- a/src/pages/Authorized.js +++ b/src/pages/Authorized.js @@ -15,12 +15,13 @@ export default ({ children }) => { type="403" desc={formatMessage({ id: 'app.exception.description.403' })} linkElement={Link} - backText={formatMessage({ id: 'app.exception.back' })} + redirect="/user/login" + backText="back to login" /> ); // if Authority === ['guest'] redirect to /user/login // You can implement the logic here. - if (Authority.join('') === 'guest') { + if (Authority === 'guest' || Authority.join('') === 'guest') { noMatch = ; } return ( diff --git a/src/utils/authority.js b/src/utils/authority.js index d595df32faf2c36dea985b1f9c3cf60cfdd6d9b5..8cd98658aadd4494c98ab201af39679bf9e7311a 100644 --- a/src/utils/authority.js +++ b/src/utils/authority.js @@ -1,16 +1,24 @@ +import { isJsonString } from '@/utils/utils'; + // use localStorage to store the authority info, which might be sent from server in actual project. export function getAuthority() { // return localStorage.getItem('antd-pro-authority') || ['admin', 'user']; const authorityString = localStorage.getItem('antd-pro-authority'); let authority; - try { + if (isJsonString(authorityString)) { authority = JSON.parse(authorityString); - } catch (e) { + } else { authority = [authorityString]; } return authority || ['admin']; } 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); } diff --git a/src/utils/utils.js b/src/utils/utils.js index 3c795c2221ff1e2f03b603c2c2bed5491f2d3e55..bd47dbf7163c8f2efad2c604c63cf0410335bfdd 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -181,3 +181,14 @@ export function formatWan(val) { export function isAntdPro() { 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; +}