1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- import store from '@/store'
- export default (router) => {
- // 路由白名单,可以考虑不同环境配置不同白名单
- const whiteList = ['/pages/index/index', '/pages/home/home', '/pages/login/login'] // no redirect whitelist
- // 全局路由前置守卫
- router.beforeEach(async (to, from, next) => {
- const token = store.state.vuex_access_token
- const userId = store.state.vuex_user_info
- if (token) {
- if (to.path === '/pages/login/login') {
- next()
- } else if (!userId) {
- try {
- // await store.dispatch('user/getUserData')
- next()
- } catch (error) {
- // await store.dispatch('user/logout')
- next({
- path: '/pages/login/index',
- query: {
- redirect: JSON.stringify({
- path: to.path,
- query: to.query
- })
- }
- })
- }
- } else {
- next()
- }
- } else if (whiteList.indexOf(to.path) !== -1) {
- // 在免登录白名单,直接进入
- next()
- } else {
- next({
- path: '/pages/account/login/index',
- query: {
- redirectView: JSON.stringify({
- path: to.path,
- query: to.query
- })
- }
- })
- }
- })
- // 全局路由后置守卫
- // eslint-disable-next-line
- router.afterEach((to, from) => {
- // console.log('router.afterEach.to', to)
- // console.log('router.afterEach.from', from)
- })
- }
|