Skip to content

Commit 57ff2fa

Browse files
committed
add router redirect
1 parent 2fa7907 commit 57ff2fa

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

src/permission.js

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import store from './store'
55
import NProgress from 'nprogress' // progress bar
66
import 'nprogress/nprogress.css' // progress bar style
77
import notification from 'ant-design-vue/es/notification'
8-
import { ACCESS_TOKEN } from "@/store/mutation-types"
8+
import { ACCESS_TOKEN } from '@/store/mutation-types'
99

10-
NProgress.configure({ showSpinner: false })// NProgress Configuration
10+
NProgress.configure({ showSpinner: false }) // NProgress Configuration
1111

12-
const whiteList = ['/user/login', '/user/register', '/user/register-result']// no redirect whitelist
12+
const whiteList = ['/user/login', '/user/register', '/user/register-result'] // no redirect whitelist
1313

1414
router.beforeEach((to, from, next) => {
1515
NProgress.start() // start progress bar
@@ -21,35 +21,48 @@ router.beforeEach((to, from, next) => {
2121
NProgress.done()
2222
} else {
2323
if (store.getters.roles.length === 0) {
24-
store.dispatch('GetInfo').then(res => {
25-
const roles = res.result && res.result.role
26-
store.dispatch('GenerateRoutes', { roles }).then(() => { // 根据roles权限生成可访问的路由表
27-
router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
28-
next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
24+
store
25+
.dispatch('GetInfo')
26+
.then(res => {
27+
const roles = res.result && res.result.role
28+
store.dispatch('GenerateRoutes', { roles }).then(() => {
29+
// 根据roles权限生成可访问的路由表
30+
// 动态添加可访问路由表
31+
router.addRoutes(store.getters.addRouters)
32+
const redirect = decodeURIComponent(from.query.redirect || to.path)
33+
if (to.path === redirect) {
34+
// hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
35+
next({ ...to, replace: true })
36+
} else {
37+
// 跳转到目的路由
38+
next({ path: redirect })
39+
}
40+
})
2941
})
30-
}).catch(() => {
31-
notification.error({ message: '错误', description: '请求用户信息失败,请重试' })
32-
store.dispatch('Logout').then(() => {
33-
next({ path: '/user/login' })
42+
.catch(() => {
43+
notification.error({
44+
message: '错误',
45+
description: '请求用户信息失败,请重试'
46+
})
47+
store.dispatch('Logout').then(() => {
48+
next({ path: '/user/login', query: { redirect: to.fullPath } })
49+
})
3450
})
35-
})
36-
3751
} else {
3852
next()
3953
}
4054
}
4155
} else {
42-
if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
56+
if (whiteList.indexOf(to.path) !== -1) {
57+
// 在免登录白名单,直接进入
4358
next()
4459
} else {
45-
next('/user/login')
60+
next({ path: '/user/login', query: { redirect: to.fullPath } })
4661
NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
4762
}
48-
4963
}
50-
5164
})
5265

5366
router.afterEach(() => {
5467
NProgress.done() // finish progress bar
55-
})
68+
})

0 commit comments

Comments
 (0)