# 路由权限
import router from './router'
import store from './store'
import { Message } from 'element-ui'
// 进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import pageTitle from '@/utils/pageTitle'
import { getToken } from '@/utils/auth'
NProgress.configure({ showSpinner: false }) // 进度配置
router.beforeEach(async (to, from, next) => {
const hasToken = getToken()
const unAuth = process.env.VUE_APP_AUTH_REQUESTS_ANT_MATCHERS.split(',')
const isLoginVuex = store.getters.login_status
NProgress.start()
// 放权路径
if (unAuth.includes(to.path)) {
next()
} else if (hasToken) {
// vuex 数据放在缓存
// 页面是否刷新
if (!isLoginVuex) {
try {
store.commit('user/SET_LOGIN_STATUS', true)
await store.dispatch('user/getInfo')
next({ ...to, replace: true })
} catch (err) {
next('/net-error')
NProgress.done()
}
} else {
if (to.path === '/login') {
Message.warning('你已经登录了,点击退出可以更换账号')
next({ path: '/' })
} else {
next()
}
}
} else {
Message.warning('账号登录过期或未登录!')
next('/login')
}
document.title = pageTitle(to.name)
NProgress.done()
})
router.afterEach(() => {
NProgress.done()
})