From 22ef377097ab9636e93d97e71fe8770d57e5207d Mon Sep 17 00:00:00 2001 From: ibuler Date: Mon, 1 Jun 2020 15:22:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=B8=8D=E6=96=AD?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/NavHeader/AccountDropdown.vue | 2 +- src/permission.js | 4 +- src/utils/request.js | 6 +-- src/utils/startup.js | 44 +++++++------------ vue.config.js | 2 +- 5 files changed, 22 insertions(+), 36 deletions(-) diff --git a/src/layout/components/NavHeader/AccountDropdown.vue b/src/layout/components/NavHeader/AccountDropdown.vue index 6f0422163..b011d8273 100644 --- a/src/layout/components/NavHeader/AccountDropdown.vue +++ b/src/layout/components/NavHeader/AccountDropdown.vue @@ -65,7 +65,7 @@ export default { window.location.href = `/` break case 'logout': - window.location.href = `/core/auth/logout/?next=${this.$route.fullPath}` + window.location.href = `${process.env.VUE_APP_LOGOUT_PATH}?next=${this.$route.fullPath}` break case 'apiKey': this.$refs.api.showApi() diff --git a/src/permission.js b/src/permission.js index 1efffa081..413ab907f 100644 --- a/src/permission.js +++ b/src/permission.js @@ -13,11 +13,11 @@ router.beforeEach(async(to, from, next) => { NProgress.start() try { await startup({ to, from, next }) - await getUserRoleAndSetRoutes({ to, from, next }) + // await getUserRoleAndSetRoutes({ to, from, next }) } catch (e) { console.log('Start service error: ', e) } - NProgress.done() + next() }) router.afterEach(() => { diff --git a/src/utils/request.js b/src/utils/request.js index 007e91221..051387b99 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -51,10 +51,10 @@ service.interceptors.request.use( ) function ifUnauthorized({ response, error }) { - if (response.status === 401) { + if (response.status === 401 && response.config.url.indexOf('/users/profile') === -1) { // 未授权重定向到登录页面 - const title = i18n.t('common.Info').String() - const msg = i18n.t('auth.LoginRequiredMsg').String() + const title = i18n.t('common.Info') + const msg = i18n.t('auth.LoginRequiredMsg') MessageBox.confirm(msg, title, { confirmButtonText: i18n.t('auth.ReLogin'), cancelButtonText: i18n.t('common.Cancel'), diff --git a/src/utils/startup.js b/src/utils/startup.js index f2c1534e0..7dbec3599 100644 --- a/src/utils/startup.js +++ b/src/utils/startup.js @@ -5,32 +5,29 @@ import { Message } from 'element-ui' import 'nprogress/nprogress.css' // progress bar style import { getPermission, getToken, setPermission } from '@/utils/auth' -const whiteList = ['/login'] // no redirect whitelist +const whiteList = ['/login', process.env.VUE_APP_LOGIN_PATH] // no redirect whitelist let initial = false function setHeadTitle({ to, from, next }) { document.title = getPageTitle(to.meta.title) } -function checkLogin({ to, from, next }) { +async function checkLogin({ to, from, next }) { + if (whiteList.indexOf(to.path) !== -1) { + next() + } // determine whether the user has logged in const hasToken = getToken() if (!hasToken) { - /* has no token*/ - - if (whiteList.indexOf(to.path) !== -1) { - next() - } else { - // other pages that do not have permission to access are redirected to the login page. - next(process.env.LOGIN_PATH) - return - } + window.location = process.env.VUE_APP_LOGIN_PATH + return } - if (to.path === '/login') { - // if is logged in, redirect to the home page - next({ path: '/' }) - return + try { + return await store.dispatch('users/getProfile') + } catch (e) { + // return false + window.location = process.env.VUE_APP_LOGIN_PATH } } @@ -44,12 +41,7 @@ async function getPublicSetting({ to, from, next }) { export async function getUserRoleAndSetRoutes({ to, from, next }) { // determine whether the user has obtained his permission roles through getProfile - const currentUser = store.getters.currentUser - const hasRoles = currentUser && currentUser.current_org_roles && currentUser.current_org_roles.length > 0 - if (hasRoles) { - next() - return - } + try { // try get user profile // note: roles must be a object array! such as: ['admin'] or ,['developer','editor'] @@ -76,8 +68,6 @@ export async function getUserRoleAndSetRoutes({ to, from, next }) { // await store.dispatch('user/resetToken') Message.error(error || 'Has Error') console.log('Error occur: ', error) - next(`/core/auth/login/`) - // next() } } @@ -89,14 +79,10 @@ export async function startup({ to, from, next }) { initial = true // set page title - setHeadTitle({ to, from, next }) - // console.log('Set head title') - checkLogin({ to, from, next }) - // console.log('Check login') + await setHeadTitle({ to, from, next }) + await checkLogin({ to, from, next }) await getPublicSetting({ to, from, next }) - // console.log('Get public setting') await getUserRoleAndSetRoutes({ to, from, next }) - // console.log('Get profile') return true } diff --git a/vue.config.js b/vue.config.js index ac7ec8ac0..70f489bb1 100644 --- a/vue.config.js +++ b/vue.config.js @@ -47,7 +47,7 @@ module.exports = { ['^' + process.env.VUE_APP_BASE_API]: '' } }, - '^/core/': { + '^/(core|static|media)/': { target: process.env.VUE_APP_CORE_HOST, changeOrigin: true }