|
@@ -5,11 +5,11 @@ import store from './store'
|
|
|
import NProgress from 'nprogress'
|
|
|
import 'nprogress/nprogress.css'
|
|
|
import notification from 'ant-design-vue/es/notification'
|
|
|
-import { ACCESS_TOKEN } from "@/store/mutation-types"
|
|
|
+import { ACCESS_TOKEN } from '@/store/mutation-types'
|
|
|
|
|
|
-NProgress.configure({ showSpinner: false })
|
|
|
+NProgress.configure({ showSpinner: false })
|
|
|
|
|
|
-const whiteList = ['/user/login', '/user/register', '/user/register-result']
|
|
|
+const whiteList = ['/user/login', '/user/register', '/user/register-result']
|
|
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
|
NProgress.start()
|
|
@@ -21,35 +21,48 @@ router.beforeEach((to, from, next) => {
|
|
|
NProgress.done()
|
|
|
} else {
|
|
|
if (store.getters.roles.length === 0) {
|
|
|
- store.dispatch('GetInfo').then(res => {
|
|
|
- const roles = res.result && res.result.role
|
|
|
- store.dispatch('GenerateRoutes', { roles }).then(() => {
|
|
|
- router.addRoutes(store.getters.addRouters)
|
|
|
- next({ ...to, replace: true })
|
|
|
+ store
|
|
|
+ .dispatch('GetInfo')
|
|
|
+ .then(res => {
|
|
|
+ const roles = res.result && res.result.role
|
|
|
+ store.dispatch('GenerateRoutes', { roles }).then(() => {
|
|
|
+
|
|
|
+
|
|
|
+ router.addRoutes(store.getters.addRouters)
|
|
|
+ const redirect = decodeURIComponent(from.query.redirect || to.path)
|
|
|
+ if (to.path === redirect) {
|
|
|
+
|
|
|
+ next({ ...to, replace: true })
|
|
|
+ } else {
|
|
|
+
|
|
|
+ next({ path: redirect })
|
|
|
+ }
|
|
|
+ })
|
|
|
})
|
|
|
- }).catch(() => {
|
|
|
- notification.error({ message: '错误', description: '请求用户信息失败,请重试' })
|
|
|
- store.dispatch('Logout').then(() => {
|
|
|
- next({ path: '/user/login' })
|
|
|
+ .catch(() => {
|
|
|
+ notification.error({
|
|
|
+ message: '错误',
|
|
|
+ description: '请求用户信息失败,请重试'
|
|
|
+ })
|
|
|
+ store.dispatch('Logout').then(() => {
|
|
|
+ next({ path: '/user/login', query: { redirect: to.fullPath } })
|
|
|
+ })
|
|
|
})
|
|
|
- })
|
|
|
-
|
|
|
} else {
|
|
|
next()
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
- if (whiteList.indexOf(to.path) !== -1) {
|
|
|
+ if (whiteList.indexOf(to.path) !== -1) {
|
|
|
+
|
|
|
next()
|
|
|
} else {
|
|
|
- next('/user/login')
|
|
|
+ next({ path: '/user/login', query: { redirect: to.fullPath } })
|
|
|
NProgress.done()
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
})
|
|
|
|
|
|
router.afterEach(() => {
|
|
|
NProgress.done()
|
|
|
-})
|
|
|
+})
|