index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 configService from '@/common/service/config.service.js'
  6. import api from "@/api/api";
  7. import { SKIP_SSO_URL_QUERY, locationUrl, openSso, getUrlParam } from './sso.js'
  8. Vue.use(Router)
  9. //初始化
  10. const router = new Router({
  11. encodeURI: true,
  12. routes: [...modules]//路由表
  13. });
  14. const whiteList = ['/pages/login/login']
  15. //全局路由前置守卫
  16. router.beforeEach((to, from, next) => {
  17. let token = uni.getStorageSync(ACCESS_TOKEN);
  18. console.log("-----to---------------------"+JSON.stringify(token))
  19. if (token) {
  20. next()
  21. } else {
  22. // query如果没有明确要求跳过sso则执行sso登录
  23. // const token = uni.getStorageSync(ACCESS_TOKEN);
  24. const ticket = getUrlParam('ticket');
  25. console.log("-----ticket---------------------"+ticket)
  26. if (ticket != null && ticket != '') {
  27. /**
  28. * 单点登录,返回是否需要跳转到单点登录页
  29. */
  30. let redirect = false;
  31. // if (openSso == 'true') {
  32. if (!token) {
  33. if (ticket) {
  34. try {
  35. new Promise((resolve, reject) => {
  36. api
  37. .validateCasLogin({
  38. ticket: ""+ticket,
  39. service: "http://localhost:8080/",
  40. })
  41. .then((response) => {
  42. if (response.data.code == 200) {
  43. uni.setStorageSync(ACCESS_TOKEN, response.data.result.token);
  44. redirect = true;
  45. } else {
  46. reject(response);
  47. }
  48. })
  49. .catch((error) => {
  50. console.log("catch===>response", response);
  51. reject(error);
  52. });
  53. });
  54. } catch (e) {
  55. redirect = true;
  56. // if (process.env.VUE_APP_PLATFORM != 'h5') {
  57. // uni.navigateTo({
  58. // url: 'https://id.shendong.com.cn/default' + '/login?service=' + encodeURIComponent(locationUrl())
  59. // });
  60. // } else {
  61. // window.location.href = 'https://id.shendong.com.cn/default' + '/login?service=' + encodeURIComponent(locationUrl());
  62. // }
  63. }
  64. } else {
  65. if (window.location.search == '?type=noCas') {
  66. } else {
  67. redirect = true;
  68. uni.navigateTo({
  69. url: 'https://id.shendong.com.cn/default' + '/login?service=' + encodeURIComponent(locationUrl())
  70. });
  71. // if (process.env.VUE_APP_PLATFORM != 'h5') {
  72. // uni.navigateTo({
  73. // url: 'https://id.shendong.com.cn/default' + '/login?service=' + encodeURIComponent(locationUrl())
  74. // });
  75. // } else {
  76. // window.location.href = 'https://id.shendong.com.cn/default' + '/login?service=' + encodeURIComponent(locationUrl());
  77. // }
  78. }
  79. }
  80. }
  81. // }
  82. let redirectSso = redirect;
  83. // 如果需要重定向到sso页面则取消路由导航
  84. if (redirectSso) return;
  85. } else if (whiteList.indexOf(to.path) !== -1) {
  86. next()
  87. } else {
  88. next({ path: '/pages/login/login' })
  89. }
  90. }
  91. })
  92. // 全局路由后置守卫
  93. router.afterEach((to, from) => {
  94. console.log("afterEach")
  95. })
  96. export default router;