import Vue from 'vue'
import Router from 'uni-simple-router'
import routes from './routers.js'
import store from '../store/index.js'
import { setToken, getToken } from '../util/'
Vue.use(Router)
const router = new Router({
routes:routes
})
const LOGIN_PAGE_NAME = 'login'
const HOME_PAGE_NAME = 'index'
router.beforeEach((to, from, next) => {
const token = getToken();
if (!token && to.name !== LOGIN_PAGE_NAME) {
if(to.name == HOME_PAGE_NAME || to.name == 'gywm'){
// 未登录且要跳转的页面是首页或关于我们
next() // 跳转
}
// 未登录且要跳转的页面不是登录页
next({
name: LOGIN_PAGE_NAME ,// 跳转到登录页
NAVTYPE: 'replaceAll'
})
} else if (!token && to.name === LOGIN_PAGE_NAME) {
// 未登陆且要跳转的页面是登录页
next() // 跳转
} else if (token && to.name === LOGIN_PAGE_NAME) {
// 已登录且要跳转的页面是登录页
next({
name: HOME_PAGE_NAME ,// 跳转到首页
NAVTYPE: 'replaceAll'
})
} else {
if (store.state.user.hasGetInfo) {
next()
} else {
store.dispatch('getLoginUserInfo').then(res => {
if(res.code == 10001){
setToken('')
next({
name: 'login',
NAVTYPE: 'replaceAll'
})
}else{
next()
}
}).catch(() => {
setToken('')
next({
name: 'login',
NAVTYPE: 'replaceAll'
})
})
}
}
})
router.afterEach(to => {
})
export default router
代码哪里有问题吗
0 个回复