index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 api from "@/api/api";
  6. import { SKIP_SSO_URL_QUERY, locationUrl, openSso, getUrlParam } from './sso.js'
  7. Vue.use(Router)
  8. //初始化
  9. const router = new Router({
  10. encodeURI: true,
  11. routes: [...modules]//路由表
  12. });
  13. const whiteList = ['/pages/login/login']
  14. //全局路由前置守卫
  15. router.beforeEach((to, from, next) => {
  16. let token = uni.getStorageSync(ACCESS_TOKEN);
  17. if (token) {
  18. next()
  19. } else {
  20. // query如果没有明确要求跳过sso则执行sso登录
  21. if (to.query[SKIP_SSO_URL_QUERY.key] !== SKIP_SSO_URL_QUERY.val) {
  22. /**
  23. * 单点登录,返回是否需要跳转到单点登录页
  24. */
  25. let redirect = false;
  26. if (openSso == 'true') {
  27. const token = uni.getStorageSync(ACCESS_TOKEN);
  28. const ticket = getUrlParam('ticket');
  29. if (!token) {
  30. if (ticket) {
  31. try {
  32. new Promise((resolve, reject) => {
  33. api
  34. .validateCasLogin({
  35. ticket: ticket,
  36. service: locationUrl,
  37. })
  38. .then((response) => {
  39. if (response.data.code == 200) {
  40. uni.setStorageSync(ACCESS_TOKEN, response.data.result.token);
  41. redirect = true;
  42. } else {
  43. reject(response);
  44. }
  45. })
  46. .catch((error) => {
  47. console.log("catch===>response", response);
  48. reject(error);
  49. });
  50. });
  51. } catch (e) {
  52. redirect = true;
  53. uni.navigateTo({
  54. url: 'https://id.shendong.com.cn/default' + '/login?service=' + encodeURIComponent(locationUrl)
  55. });
  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;