| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- import modules from "./modules";
- import Vue from "vue";
- import Router from "@/plugin/uni-simple-router/index.js";
- import { ACCESS_TOKEN } from "@/common/util/constants.js";
- import configService from "@/common/service/config.service.js";
- import api from "@/api/api";
- import {
- SKIP_SSO_URL_QUERY,
- locationUrl,
- openSso,
- getUrlParam,
- } from "./sso.js";
- Vue.use(Router);
- //初始化
- const router = new Router({
- encodeURI: true,
- routes: [...modules], //路由表
- });
- const whiteList = ["/pages/login/login"];
- // 防止短时间内重复跳转登录页
- let lastRedirectAt = 0;
- //全局路由前置守卫
- router.beforeEach((to, from, next) => {
- let token = uni.getStorageSync(ACCESS_TOKEN);
- console.log("-----to---------------------" + JSON.stringify(token));
- if (token) {
- next();
- return;
- }
- // 目标就是登录页,直接放行
- if (whiteList.indexOf(to.path) !== -1) {
- next();
- return;
- }
- // 1 秒内只触发一次登录跳转,避免与退出登录 / 401 的 reLaunch 叠加
- const now = Date.now();
- if (now - lastRedirectAt < 1000) return;
- lastRedirectAt = now;
- {
- // query如果没有明确要求跳过sso则执行sso登录
- // const token = uni.getStorageSync(ACCESS_TOKEN);
- const ticket = getUrlParam("ticket");
- console.log("-----ticket---------------------" + ticket);
- if (ticket != null && ticket != "") {
- /**
- * 单点登录,返回是否需要跳转到单点登录页
- */
- let redirect = false;
- // if (openSso == 'true') {
- if (!token) {
- if (ticket) {
- try {
- new Promise((resolve, reject) => {
- api
- .validateCasLogin({
- ticket: "" + ticket,
- service: "http://localhost:8080/",
- })
- .then((response) => {
- if (response.data.code == 200) {
- uni.setStorageSync(
- ACCESS_TOKEN,
- response.data.result.token,
- );
- redirect = true;
- } else {
- reject(response);
- }
- })
- .catch((error) => {
- console.log("catch===>response", response);
- reject(error);
- });
- });
- } catch (e) {
- redirect = true;
- // if (process.env.VUE_APP_PLATFORM != 'h5') {
- // uni.navigateTo({
- // url: 'https://id.shendong.com.cn/default' + '/login?service=' + encodeURIComponent(locationUrl())
- // });
- // } else {
- // window.location.href = 'https://id.shendong.com.cn/default' + '/login?service=' + encodeURIComponent(locationUrl());
- // }
- }
- } else {
- if (window.location.search == "?type=noCas") {
- } else {
- redirect = true;
- uni.navigateTo({
- url:
- "https://id.shendong.com.cn/default" +
- "/login?service=" +
- encodeURIComponent(locationUrl()),
- });
- // if (process.env.VUE_APP_PLATFORM != 'h5') {
- // uni.navigateTo({
- // url: 'https://id.shendong.com.cn/default' + '/login?service=' + encodeURIComponent(locationUrl())
- // });
- // } else {
- // window.location.href = 'https://id.shendong.com.cn/default' + '/login?service=' + encodeURIComponent(locationUrl());
- // }
- }
- }
- }
- // }
- let redirectSso = redirect;
- // 如果需要重定向到sso页面则取消路由导航
- if (redirectSso) return;
- } else if (whiteList.indexOf(to.path) !== -1) {
- next();
- } else {
- next({ path: "/pages/login/login" });
- }
- }
- });
- // 全局路由后置守卫
- router.afterEach((to, from) => {
- console.log("afterEach");
- });
- export default router;
|