12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- import Vue from 'vue'
- import router from './router'
- import store from './store'
- import NProgress from 'nprogress'
- import '@/components/NProgress/nprogress.less'
- import notification from 'ant-design-vue/es/notification'
- import { setDocumentTitle, domTitle } from '@/utils/domUtil'
- import { ACCESS_TOKEN } from '@/store/mutation-types'
- NProgress.configure({ showSpinner: false })
- const whiteList = ['login', 'register', 'registerResult']
- const defaultRoutePath = '/home'
- router.beforeEach((to, from, next) => {
- NProgress.start()
- to.meta && (typeof to.meta.title !== 'undefined' && setDocumentTitle(`${to.meta.title} - ${domTitle}`))
- let token = store.state.user.token || sessionStorage.getItem(ACCESS_TOKEN)
- token = token != 'undefined' ? token : ''
-
-
- if (token) {
-
- if (to.path === '/user/login') {
- next({ path: defaultRoutePath })
- NProgress.done()
- } else {
- const userInfo = Vue.ls.get('userName')
- console.log(userInfo)
- if (userInfo) {
- store.dispatch('SetInfo', userInfo)
- } else {
- store.dispatch('Logout').then(() => {
- next({ path: '/user/login', query: { redirect: to.fullPath } })
- })
- }
-
- if (store.getters.roles.permissionList.length === 0) {
- store
- .dispatch('GetInfo', token)
- .then(res => {
- const roles = res
- console.log(roles, 'roles')
- if (roles.permissionList.length == 0 || !roles) {
- store.dispatch('Logout').then(() => {
- store.dispatch('ResetRoutes').then(() => {
- next({ path: '/user/login', query: { redirect: to.fullPath } })
- })
- })
- } else {
- store.dispatch('GenerateRoutes', { roles }).then(() => {
-
-
-
- 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', query: { redirect: to.fullPath } })
- })
- })
- } else {
- next()
- }
- }
- } else {
-
- if (whiteList.includes(to.name)) {
-
- next()
- } else {
- next({ path: '/user/login', query: { redirect: to.fullPath } })
- NProgress.done()
- }
- }
- })
- router.afterEach(() => {
- NProgress.done()
- })
|