Houjie
2025-04-11 1bf977929dd324f3ac64b70debd8a79443c54392
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import modules from './modules'
import Vue from 'vue'
import Router from '@/plugin/uni-simple-router/index.js'
import {ACCESS_TOKEN} from '@/common/util/constants.js'
import {isOAuth2AppEnv} from '@/common/util/util.js'
 
Vue.use(Router)
//初始化
const router = new Router({
    encodeURI:true,  
    routes: [...modules]//路由表
});
 
const whiteList = ['/pages/login/login','/pages/login/loginOauth2']
//全局路由前置守卫
router.beforeEach((to, from, next) => {
    if(to.path == '/oauth2-app/login'){
        let temp = location.href;
        location.href = temp.replace('/oauth2-app/login','/pages/login/loginOauth2')
        return;
    }
    let token=uni.getStorageSync(ACCESS_TOKEN);
    if(token){
         if (to.path === '/pages/login/login' || to.path === '/pages/login/loginOauth2') {
             if(from.path ==='/pages/index/index'){
                 return;
             }else{
                 next()
             }
        }
        next()
    }else{
        if (whiteList.indexOf(to.path) !== -1) {
          // 在免登录白名单,如果进入的页面是login页面并且当前是OAuth2app环境,就进入OAuth2登录页面
          if (to.path === '/pages/login/login' && isOAuth2AppEnv()) {
            next({path: '/pages/login/loginOauth2'})
          } else {
            // 在免登录白名单,直接进入
            next()
          }
        }else{
            // 如果当前是在OAuth2APP环境,就跳转到OAuth2登录页面
            let path = isOAuth2AppEnv() ? '/pages/login/loginOauth2' : '/pages/login/login';
            next({ path: path })
        }
    } 
})
// 全局路由后置守卫
router.afterEach((to, from) => {
    console.log("afterEach")
});
export default router;