|
|
|
|
@ -3,8 +3,9 @@ import store from './store'
|
|
|
|
|
import { Message } from 'element-ui'
|
|
|
|
|
import NProgress from 'nprogress' // progress bar
|
|
|
|
|
import 'nprogress/nprogress.css' // progress bar style
|
|
|
|
|
import { getToken } from '@/utils/auth' // get token from cookie
|
|
|
|
|
import { getToken, setToken } from '@/utils/auth' // get token from cookie
|
|
|
|
|
import getPageTitle from '@/utils/get-page-title'
|
|
|
|
|
import { deepCopy } from "@/utils"
|
|
|
|
|
|
|
|
|
|
NProgress.configure({ showSpinner: false }) // NProgress Configuration
|
|
|
|
|
|
|
|
|
|
@ -13,23 +14,24 @@ const whiteList = ['/login'] // no redirect whitelist
|
|
|
|
|
router.beforeEach(async(to, from, next) => {
|
|
|
|
|
// start progress bar
|
|
|
|
|
NProgress.start()
|
|
|
|
|
|
|
|
|
|
// set page title
|
|
|
|
|
document.title = getPageTitle(to.meta.title)
|
|
|
|
|
|
|
|
|
|
// determine whether the user has logged in
|
|
|
|
|
const hasToken = getToken()
|
|
|
|
|
|
|
|
|
|
if (to.query.tourl) {
|
|
|
|
|
await store.dispatch('app/toggleDevice',"mobile")
|
|
|
|
|
await store.dispatch('app/closeSideBar',false)
|
|
|
|
|
}
|
|
|
|
|
if (hasToken) {
|
|
|
|
|
if (to.path === '/login') {
|
|
|
|
|
// if is logged in, redirect to the home page
|
|
|
|
|
next({ path: '/' })
|
|
|
|
|
NProgress.done()
|
|
|
|
|
} else {
|
|
|
|
|
// determine whether the user has obtained his permission roles through getInfo
|
|
|
|
|
const hasRoles = store.getters.roles && store.getters.roles.length > 0
|
|
|
|
|
if (hasRoles) {
|
|
|
|
|
next()
|
|
|
|
|
to.query.tourl ? next(to.query.tourl) : next()
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
// get user info
|
|
|
|
|
@ -45,7 +47,11 @@ router.beforeEach(async(to, from, next) => {
|
|
|
|
|
|
|
|
|
|
// hack method to ensure that addRoutes is complete
|
|
|
|
|
// set the replace: true, so the navigation will not leave a history record
|
|
|
|
|
next({ ...to, replace: true })
|
|
|
|
|
let resetTo = deepCopy(to)
|
|
|
|
|
resetTo.path = to.query.tourl || to.path
|
|
|
|
|
resetTo.replace = true
|
|
|
|
|
console.log(resetTo)
|
|
|
|
|
next(resetTo)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error)
|
|
|
|
|
// remove token and go to login page to re-login
|
|
|
|
|
@ -58,31 +64,33 @@ router.beforeEach(async(to, from, next) => {
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* has no token*/
|
|
|
|
|
if (to.query.token && to.query.tp) {
|
|
|
|
|
try {
|
|
|
|
|
await store.dispatch('user/loginskip', {
|
|
|
|
|
token: to.query.token,
|
|
|
|
|
tp: to.query.tp
|
|
|
|
|
})
|
|
|
|
|
next(to.query.tourl ? to.query.tourl : '/')
|
|
|
|
|
} catch (e) {
|
|
|
|
|
next('/login')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (whiteList.indexOf(to.path) !== -1) {
|
|
|
|
|
// in the free login whitelist, go directly
|
|
|
|
|
next()
|
|
|
|
|
} else {
|
|
|
|
|
// other pages that do not have permission to access are redirected to the login page.
|
|
|
|
|
next(`/login?redirect=${to.path}`)
|
|
|
|
|
NProgress.done()
|
|
|
|
|
if (to.query.token && to.query.tp) {
|
|
|
|
|
try {
|
|
|
|
|
await store.dispatch('user/loginskip', {
|
|
|
|
|
token: to.query.token,
|
|
|
|
|
tp: to.query.tp
|
|
|
|
|
})
|
|
|
|
|
next(to.query.tourl || '/')
|
|
|
|
|
} catch (e) {
|
|
|
|
|
next('/login')
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// other pages that do not have permission to access are redirected to the login page.
|
|
|
|
|
next(`/login?redirect=${to.path}`)
|
|
|
|
|
NProgress.done()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
NProgress.done()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
router.afterEach(() => {
|
|
|
|
|
// finish progress bar
|
|
|
|
|
store.dispatch('app/toggleDevice',"mobile")
|
|
|
|
|
store.dispatch('app/closeSideBar',false)
|
|
|
|
|
NProgress.done()
|
|
|
|
|
})
|
|
|
|
|
|