1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import router from '@/router'
- import store from '@/store'
- import { getToken, getUserInfo, initGoPage } from '@/utils/auth'
- // import getPageTitle from './utils/get-page-title'
- // 获取用户信息
- const userInfo = getUserInfo()
- if (userInfo) {
- store.commit('user/SAVE_USER_INOF', userInfo)
- }
- // 路由白名单
- const whiteList = ['/login']
- const shareList = ['Share', 'SharePayVip', 'SharePaySuccess', 'SharePayFaild']
- // 路由拦截器
- router.beforeEach(async(to, from, next) => {
- const hasToken = getToken()
- // 设置页面标题
- // document.title = getPageTitle(to.meta.title)
- if (shareList.indexOf(to.name) > -1) {
- next()
- return
- }
- if (hasToken) {
- if (to.path === '/login') {
- next({ path: initGoPage() })
- } else {
- store.dispatch('app/setCountry')
- const hasRoles = store.getters.roles && store.getters.roles.length > 0
- if (hasRoles) {
- next()
- } else {
- try {
- // 代理状态更换时清空全部页面标签
- console.log('代理状态切换 ', store.getters.isChangeProxy)
- if (store.getters.isChangeProxy) {
- store.commit('tagsView/CLEAR_ALL_VIEW')
- store.commit('proxy/CHANGE_PROXY_STATE', false)
- }
- await store.dispatch('user/fetchUserInfo')
- await store.dispatch('user/fetchUserVipInfo')
- // 根据用户角色获取路由配置
- const accessRoutes = await store.dispatch('permission/generateRoutes')
- console.log(accessRoutes)
- // 添加路由配置
- router.addRoutes(accessRoutes)
- // websocket
- store.dispatch('notice/start')
- // 放行
- next({ ...to, path: '/', replace: true })
- } catch (err) {
- store.dispatch('user/logout')
- }
- }
- }
- } else {
- if (whiteList.indexOf(to.path) !== -1) {
- next()
- } else {
- next('/login')
- }
- }
- })
|