index.js 1.5 KB

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