From 5c9354368442e1fecb0a9aea341b11c5a47afc55 Mon Sep 17 00:00:00 2001 From: IITII Date: Wed, 15 Mar 2023 20:00:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=87=8D=E6=96=B0=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E5=90=8E=E8=B7=B3=E8=BD=AC=E5=9B=9E=E5=8E=9F=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/boot/auth.js | 74 +++++++++++++++++++++++----------------------- src/utils/utils.js | 2 +- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/boot/auth.js b/src/boot/auth.js index bb85892..3fab3d8 100644 --- a/src/boot/auth.js +++ b/src/boot/auth.js @@ -3,48 +3,48 @@ * @date 2023/02/02 */ 'use strict' -import { boot } from 'quasar/wrappers' +import {boot} from 'quasar/wrappers' // 路由白名单 const witheList = ['public'] let logger = console -export default boot(({ app, router, store }) => { +export default boot(({app, router, store}) => { logger = app.$log || console router.beforeEach((to, from, next) => { - // 获取或者加载 Vue Store对象 - // const store = Vue.$store || VueStore - // logger.debug(to) - // 如果路由深度不够,无需判断 - if (to.matched.length < 2) return next() - // 如果目标路由没有 meta 或者 roles 信息,无需判断 - if (!(to.matched[1].meta && to.matched[1].meta.roles)) { - return next() - } - const roles = to.matched[1].meta.roles - // 将用户类型转换为用户角色 - const userRole = store.getters['user/user_type'] - const token = store.state.user.token - if (!token) { - logger.debug('Forbidden with empty token', token) - return next({name: 'home'}) - } - // 访问白名单路由 - if (witheList.some(white => roles.indexOf(white) >= 0)) { - logger.debug('Get public routes,', witheList) - return next() - // 访问用户角色是否包含在目标路由角色组 - } else if (roles.some(r => userRole === r)) { - // 包含 - logger.debug('Get user routes,', userRole, roles) - return next() - } else { - // 不包含 - logger.debug('Forbidden', userRole, roles) - // forbidden - return next({name: 'login'}) - // return next({name: 'home'}) - // return next({path: '/404'}) - } - }) + // 获取或者加载 Vue Store对象 + // const store = Vue.$store || VueStore + // logger.debug(to) + // 如果路由深度不够,无需判断 + if (to.matched.length < 2) return next() + // 如果目标路由没有 meta 或者 roles 信息,无需判断 + if (!(to.matched[1].meta && to.matched[1].meta.roles)) { + return next() + } + const roles = to.matched[1].meta.roles + // 将用户类型转换为用户角色 + const userRole = store.getters['user/user_type'] + const token = store.state.user.token + if (!token) { + logger.debug('Forbidden with empty token', token) + return next({name: 'home'}) + } + // 访问白名单路由 + if (witheList.some(white => roles.indexOf(white) >= 0)) { + logger.debug('Get public routes,', witheList) + return next() + // 访问用户角色是否包含在目标路由角色组 + } else if (roles.some(r => userRole === r)) { + // 包含 + logger.debug('Get user routes,', userRole, roles) + return next() + } else { + // 不包含 + logger.debug('Forbidden', userRole, roles) + // forbidden + return next({name: 'login', query: {redirect: to.fullPath}}) + // return next({name: 'home'}) + // return next({path: '/404'}) + } + }) }) diff --git a/src/utils/utils.js b/src/utils/utils.js index 1093e92..b719137 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -46,7 +46,7 @@ export function afterLogin(user, store, router, axios, redirect = true) { axios.defaults.headers['authorization'] = ['Token', user.token].join(' ') if (redirect) { // router.push('/' + user.user_type) - router.push(`/${user.user_type}/image`) + router.push({path: router.query.redirect || `/${user.user_type}/image`}) } }