index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 {
  8. SKIP_SSO_URL_QUERY,
  9. locationUrl,
  10. openSso,
  11. getUrlParam,
  12. } from "./sso.js";
  13. Vue.use(Router);
  14. //初始化
  15. const router = new Router({
  16. encodeURI: true,
  17. routes: [...modules], //路由表
  18. });
  19. const whiteList = ["/pages/login/login"];
  20. // 防止短时间内重复跳转登录页
  21. let lastRedirectAt = 0;
  22. //全局路由前置守卫
  23. router.beforeEach((to, from, next) => {
  24. let token = uni.getStorageSync(ACCESS_TOKEN);
  25. console.log("-----to---------------------" + JSON.stringify(token));
  26. if (token) {
  27. next();
  28. return;
  29. }
  30. // 目标就是登录页,直接放行
  31. if (whiteList.indexOf(to.path) !== -1) {
  32. next();
  33. return;
  34. }
  35. // 1 秒内只触发一次登录跳转,避免与退出登录 / 401 的 reLaunch 叠加
  36. const now = Date.now();
  37. if (now - lastRedirectAt < 1000) return;
  38. lastRedirectAt = now;
  39. {
  40. // query如果没有明确要求跳过sso则执行sso登录
  41. // const token = uni.getStorageSync(ACCESS_TOKEN);
  42. const ticket = getUrlParam("ticket");
  43. console.log("-----ticket---------------------" + ticket);
  44. if (ticket != null && ticket != "") {
  45. /**
  46. * 单点登录,返回是否需要跳转到单点登录页
  47. */
  48. let redirect = false;
  49. // if (openSso == 'true') {
  50. if (!token) {
  51. if (ticket) {
  52. try {
  53. new Promise((resolve, reject) => {
  54. api
  55. .validateCasLogin({
  56. ticket: "" + ticket,
  57. service: "http://localhost:8080/",
  58. })
  59. .then((response) => {
  60. if (response.data.code == 200) {
  61. uni.setStorageSync(
  62. ACCESS_TOKEN,
  63. response.data.result.token,
  64. );
  65. redirect = true;
  66. } else {
  67. reject(response);
  68. }
  69. })
  70. .catch((error) => {
  71. console.log("catch===>response", response);
  72. reject(error);
  73. });
  74. });
  75. } catch (e) {
  76. redirect = true;
  77. // if (process.env.VUE_APP_PLATFORM != 'h5') {
  78. // uni.navigateTo({
  79. // url: 'https://id.shendong.com.cn/default' + '/login?service=' + encodeURIComponent(locationUrl())
  80. // });
  81. // } else {
  82. // window.location.href = 'https://id.shendong.com.cn/default' + '/login?service=' + encodeURIComponent(locationUrl());
  83. // }
  84. }
  85. } else {
  86. if (window.location.search == "?type=noCas") {
  87. } else {
  88. redirect = true;
  89. uni.navigateTo({
  90. url:
  91. "https://id.shendong.com.cn/default" +
  92. "/login?service=" +
  93. encodeURIComponent(locationUrl()),
  94. });
  95. // if (process.env.VUE_APP_PLATFORM != 'h5') {
  96. // uni.navigateTo({
  97. // url: 'https://id.shendong.com.cn/default' + '/login?service=' + encodeURIComponent(locationUrl())
  98. // });
  99. // } else {
  100. // window.location.href = 'https://id.shendong.com.cn/default' + '/login?service=' + encodeURIComponent(locationUrl());
  101. // }
  102. }
  103. }
  104. }
  105. // }
  106. let redirectSso = redirect;
  107. // 如果需要重定向到sso页面则取消路由导航
  108. if (redirectSso) return;
  109. } else if (whiteList.indexOf(to.path) !== -1) {
  110. next();
  111. } else {
  112. next({ path: "/pages/login/login" });
  113. }
  114. }
  115. });
  116. // 全局路由后置守卫
  117. router.afterEach((to, from) => {
  118. console.log("afterEach");
  119. });
  120. export default router;