index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. let isApp = typeof cordova !== 'undefined' ? 'true' : 'false'
  54. if (isApp == 'true') {
  55. plus.runtime.openWeb('https://id.shendong.com.cn/default' + '/login?service=' + encodeURIComponent(locationUrl))
  56. } else {
  57. window.location.href = 'https://id.shendong.com.cn/default' + '/login?service=' + encodeURIComponent(locationUrl);
  58. }
  59. }
  60. } else {
  61. if (window.location.search == '?type=noCas') {
  62. } else {
  63. redirect = true;
  64. let isApp = typeof cordova !== 'undefined' ? 'true' : 'false'
  65. if (isApp == 'true') {
  66. plus.runtime.openWeb('https://id.shendong.com.cn/default' + '/login?service=' + encodeURIComponent(locationUrl))
  67. } else {
  68. window.location.href = 'https://id.shendong.com.cn/default' + '/login?service=' + encodeURIComponent(locationUrl);
  69. }
  70. }
  71. }
  72. }
  73. }
  74. let redirectSso = redirect;
  75. // 如果需要重定向到sso页面则取消路由导航
  76. if (redirectSso) return;
  77. } else if (whiteList.indexOf(to.path) !== -1) {
  78. next()
  79. } else {
  80. next({ path: '/pages/login/login' })
  81. }
  82. }
  83. })
  84. // 全局路由后置守卫
  85. router.afterEach((to, from) => {
  86. console.log("afterEach")
  87. })
  88. export default router;