From fea90487728849ce053716bbc556448fe189cef1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B8=85?= Date: Tue, 11 Sep 2018 22:20:55 +0800 Subject: [PATCH] fix #2157 add redirect when no login --- config/router.config.js | 3 +++ src/components/GlobalHeader/RightContent.js | 2 +- src/locales/en-US.js | 1 + src/locales/zh-CN.js | 1 + src/pages/Authorized.js | 29 ++++++++------------- 5 files changed, 17 insertions(+), 19 deletions(-) diff --git a/config/router.config.js b/config/router.config.js index cb6fd5f1..bef1be38 100644 --- a/config/router.config.js +++ b/config/router.config.js @@ -15,6 +15,7 @@ export default [ path: '/', component: '../layouts/BasicLayout', Routes: ['src/pages/Authorized'], + authority: ['admin', 'user'], routes: [ // dashboard { path: '/', redirect: '/dashboard/analysis' }, @@ -82,6 +83,7 @@ export default [ { path: '/form/advanced-form', name: 'advancedform', + authority: ['admin'], component: './Forms/AdvancedForm', }, ], @@ -149,6 +151,7 @@ export default [ { path: '/profile/advanced', name: 'advanced', + authority: ['admin'], component: './Profile/AdvancedProfile', }, ], diff --git a/src/components/GlobalHeader/RightContent.js b/src/components/GlobalHeader/RightContent.js index 3c882b9f..12d69516 100644 --- a/src/components/GlobalHeader/RightContent.js +++ b/src/components/GlobalHeader/RightContent.js @@ -74,7 +74,7 @@ export default class GlobalHeaderRight extends PureComponent { - 退出登录 + ); diff --git a/src/locales/en-US.js b/src/locales/en-US.js index 729836ec..c0a7c5ee 100644 --- a/src/locales/en-US.js +++ b/src/locales/en-US.js @@ -35,6 +35,7 @@ export default { 'menu.account.center': 'Account Center', 'menu.account.settings': 'Account Settings', 'menu.account.trigger': 'Trigger Error', + 'menu.account.logout': 'Logout', 'app.home.introduce': 'introduce', 'app.analysis.test': 'Gongzhuan No.{no} shop', 'app.analysis.introduce': 'Introduce', diff --git a/src/locales/zh-CN.js b/src/locales/zh-CN.js index 81d766a6..7c6f3632 100644 --- a/src/locales/zh-CN.js +++ b/src/locales/zh-CN.js @@ -37,6 +37,7 @@ export default { 'menu.account.center': '个人中心', 'menu.account.settings': '个人设置', 'menu.account.trigger': '触发报错', + 'menu.account.logout': '退出登录', 'app.home.introduce': '介绍', 'app.analysis.test': '工专路 {no} 号店', 'app.analysis.introduce': '指标说明', diff --git a/src/pages/Authorized.js b/src/pages/Authorized.js index e0e18d70..238888a7 100644 --- a/src/pages/Authorized.js +++ b/src/pages/Authorized.js @@ -2,24 +2,15 @@ import React from 'react'; import RenderAuthorized from '@/components/Authorized'; import Exception from '@/components/Exception'; import { getAuthority } from '@/utils/authority'; -import { matchRoutes } from 'react-router-config'; -import intersection from 'lodash/intersection'; import { formatMessage } from 'umi/locale'; import Link from 'umi/link'; +import Redirect from 'umi/redirect'; -const Authorized = RenderAuthorized(getAuthority()); +const Authority = getAuthority(); +const Authorized = RenderAuthorized(Authority); -export default ({ children, route, location }) => { - const routes = matchRoutes(route.routes, location.pathname); - const authorities = []; - routes.forEach(item => { - if (Array.isArray(item.route.authority) && item.route.authority.length) { - authorities.push(item.route.authority); - } else if (typeof item.route.authority === 'string' && item.route.authority) { - authorities.push([item.route.authority]); - } - }); - const noMatch = ( +export default ({ children }) => { + let noMatch = ( { backText={formatMessage({ id: 'app.exception.back' })} /> ); + // if Authority === ['guest'] redirect to /user/login + // You can implement the logic here. + if (Authority.join('') === 'guest') { + noMatch = ; + } return ( - + {children} ); -- GitLab