Procházet zdrojové kódy

单点登录-提交

lxh před 2 měsíci
rodič
revize
2741796f61
3 změnil soubory, kde provedl 111 přidání a 13 odebrání
  1. 7 0
      api/api.js
  2. 75 13
      common/router/index.js
  3. 29 0
      common/router/sso.js

+ 7 - 0
api/api.js

@@ -9,6 +9,13 @@ const apiService = {
     return http.post("/sys/mLogin", params);
   },
   /**
+   * 单点登录
+   */
+  validateCasLogin(params) {
+    return http.get("/sys/cas/client/validateLogin", params);
+  },
+
+  /**
    * 手机号码登录
    */
   phoneNoLogin(params) {

+ 75 - 13
common/router/index.js

@@ -1,28 +1,90 @@
 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 { ACCESS_TOKEN } from '@/common/util/constants.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]//路由表
+	encodeURI: true,
+	routes: [...modules]//路由表
 });
 
-const whiteList = ['/pages/login/login'] 
+const whiteList = ['/pages/login/login']
 //全局路由前置守卫
 router.beforeEach((to, from, next) => {
-	let token=uni.getStorageSync(ACCESS_TOKEN);
-	if(token){
-		 next()
-	}else{
-		if (whiteList.indexOf(to.path) !== -1) {
-		  next()
-		}else{
-		  next({ path: '/pages/login/login'})
+	let token = uni.getStorageSync(ACCESS_TOKEN);
+	if (token) {
+		next()
+	} else {
+		// query如果没有明确要求跳过sso则执行sso登录
+		if (to.query[SKIP_SSO_URL_QUERY.key] !== SKIP_SSO_URL_QUERY.val) {
+			/**
+			   * 单点登录,返回是否需要跳转到单点登录页
+			*/
+			let redirect = false;
+			if (openSso == 'true') {
+				const token = uni.getStorageSync(ACCESS_TOKEN);
+				const ticket = getUrlParam('ticket');
+				if (!token) {
+					if (ticket) {
+						try {
+							new Promise((resolve, reject) => {
+								api
+									.validateCasLogin({
+										ticket: ticket,
+										service: locationUrl,
+									})
+									.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;
+							let isApp = typeof cordova !== 'undefined' ? 'true' : 'false'
+							if (isApp == 'true') {
+								plus.runtime.openWeb('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;
+							let isApp = typeof cordova !== 'undefined' ? 'true' : 'false'
+							if (isApp == 'true') {
+								plus.runtime.openWeb('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) => {

+ 29 - 0
common/router/sso.js

@@ -0,0 +1,29 @@
+export let SKIP_SSO_URL_QUERY = { key: 'skipsso', val: '1' };
+export	let locationUrl = 'http://' + window.location.host + '/';
+export	let  openSso='true';
+
+
+/**
+ * 获取url地址参数
+ * @param paraName
+ */
+export function getUrlParam(paraName) {
+  const url = document.location.toString();
+  const arrObj = url.split('?');
+
+  if (arrObj.length > 1) {
+    const arrPara = arrObj[1].split('&');
+    let arr;
+
+    for (let i = 0; i < arrPara.length; i++) {
+      arr = arrPara[i].split('=');
+
+      if (arr != null && arr[0] == paraName) {
+        return arr[1];
+      }
+    }
+    return '';
+  } else {
+    return '';
+  }
+}