作者 RuoYi

白名单支持对通配符路径匹配

@@ -4,11 +4,16 @@ import { Message } from 'element-ui' @@ -4,11 +4,16 @@ import { Message } from 'element-ui'
4 import NProgress from 'nprogress' 4 import NProgress from 'nprogress'
5 import 'nprogress/nprogress.css' 5 import 'nprogress/nprogress.css'
6 import { getToken } from '@/utils/auth' 6 import { getToken } from '@/utils/auth'
  7 +import { isPathMatch } from '@/utils/validate'
7 import { isRelogin } from '@/utils/request' 8 import { isRelogin } from '@/utils/request'
8 9
9 NProgress.configure({ showSpinner: false }) 10 NProgress.configure({ showSpinner: false })
10 11
11 -const whiteList = ['/login', '/register'] 12 +const whiteList = ['/login', '/register', '/register*', '/register/*']
  13 +
  14 +const isWhiteList = (path) => {
  15 + return whiteList.some(pattern => isPathMatch(pattern, path))
  16 +}
12 17
13 router.beforeEach((to, from, next) => { 18 router.beforeEach((to, from, next) => {
14 NProgress.start() 19 NProgress.start()
@@ -18,7 +23,7 @@ router.beforeEach((to, from, next) => { @@ -18,7 +23,7 @@ router.beforeEach((to, from, next) => {
18 if (to.path === '/login') { 23 if (to.path === '/login') {
19 next({ path: '/' }) 24 next({ path: '/' })
20 NProgress.done() 25 NProgress.done()
21 - } else if (whiteList.indexOf(to.path) !== -1) { 26 + } else if (isWhiteList(to.path)) {
22 next() 27 next()
23 } else { 28 } else {
24 if (store.getters.roles.length === 0) { 29 if (store.getters.roles.length === 0) {
@@ -43,7 +48,7 @@ router.beforeEach((to, from, next) => { @@ -43,7 +48,7 @@ router.beforeEach((to, from, next) => {
43 } 48 }
44 } else { 49 } else {
45 // 没有token 50 // 没有token
46 - if (whiteList.indexOf(to.path) !== -1) { 51 + if (isWhiteList(to.path)) {
47 // 在免登录白名单,直接进入 52 // 在免登录白名单,直接进入
48 next() 53 next()
49 } else { 54 } else {
1 /** 1 /**
  2 + * 路径匹配器
  3 + * @param {string} pattern
  4 + * @param {string} path
  5 + * @returns {Boolean}
  6 + */
  7 +export function isPathMatch(pattern, path) {
  8 + const regexPattern = pattern.replace(/\//g, '\\/').replace(/\*\*/g, '.*').replace(/\*/g, '[^\\/]*')
  9 + const regex = new RegExp(`^${regexPattern}$`)
  10 + return regex.test(path)
  11 +}
  12 +
  13 +/**
2 * 判断value字符串是否为空 14 * 判断value字符串是否为空
3 * @param {string} value 15 * @param {string} value
4 * @returns {Boolean} 16 * @returns {Boolean}
5 */ 17 */
6 export function isEmpty(value) { 18 export function isEmpty(value) {
7 if (value == null || value == "" || value == undefined || value == "undefined") { 19 if (value == null || value == "" || value == undefined || value == "undefined") {
8 - return true; 20 + return true
9 } 21 }
10 - return false; 22 + return false
11 } 23 }
12 24
13 /** 25 /**
@@ -87,7 +99,7 @@ export function validEmail(email) { @@ -87,7 +99,7 @@ export function validEmail(email) {
87 * @returns {Boolean} 99 * @returns {Boolean}
88 */ 100 */
89 export function isString(str) { 101 export function isString(str) {
90 - return typeof str === 'string' || str instanceof String; 102 + return typeof str === 'string' || str instanceof String
91 } 103 }
92 104
93 /** 105 /**