Browse Source

添加单点登录过渡页,处理单点登录逻辑

lph 1 năm trước cách đây
mục cha
commit
02e031bed2

+ 23 - 0
zkqy-ui/src/api/login.js

@@ -87,3 +87,26 @@ export function getCodeImg() {
     timeout: 20000
   })
 }
+
+// 单点登录
+export function loginBySso(data) {
+  return request({
+    url: `/Oauth2Login/${data}`,
+    headers: {
+      isToken: false
+    },
+    method: 'get',
+    // params: data
+  })
+}
+
+// 调用后端权限校验接口
+export function authorize(data) {
+  return request({
+    url: `/authorize`,
+    headers: {
+      isToken: false
+    },
+    method: 'get',
+  })
+}

+ 1 - 1
zkqy-ui/src/permission.js

@@ -8,7 +8,7 @@ import { isRelogin } from '@/utils/request'
 
 NProgress.configure({ showSpinner: false })
 
-const whiteList = ['/login', '/register', '/404', '/adminLogin', '/401', '/loading']
+const whiteList = ['/login', '/register', '/404', '/adminLogin', '/401', '/loading', '/redirectAuth']
 
 router.beforeEach((to, from, next) => {
   NProgress.start()

+ 1 - 1
zkqy-ui/src/router/index.js

@@ -191,7 +191,7 @@ export const constantRoutes = [
     hidden: true
   },
   {
-    path: '/loading',
+    path: '/redirectAuth',
     component: () => import('@/views/loading'),
     hidden: true
   },

+ 19 - 8
zkqy-ui/src/store/modules/user.js

@@ -1,6 +1,6 @@
-import {login, logout, getInfo, adminLoginApi} from '@/api/login'
-import {changeDatasource} from '@/api/dataEngine/index'
-import {getToken, setToken, removeToken} from '@/utils/auth'
+import { login, logout, getInfo, adminLoginApi } from '@/api/login'
+import { changeDatasource } from '@/api/dataEngine/index'
+import { getToken, setToken, removeToken } from '@/utils/auth'
 
 const user = {
   state: {
@@ -46,8 +46,19 @@ const user = {
   },
 
   actions: {
+    // 单点登录
+    LoginBySso({ commit }, data) {
+      let { username, token } = data;
+
+      return new Promise((resolve, reject) => {
+        commit('SET_NAME', username)
+        setToken(token)
+        commit('SET_TOKEN', token)
+        resolve();
+      })
+    },
     // 登录
-    Login({commit}, userInfo) {
+    Login({ commit }, userInfo) {
       const username = userInfo.username.trim()
       const password = userInfo.password
       const tenantID = userInfo.tenantID
@@ -65,7 +76,7 @@ const user = {
       })
     },
     // admin登录
-    adminLogin({commit}, userInfo) {
+    adminLogin({ commit }, userInfo) {
       const username = userInfo.username.trim()
       const password = userInfo.password
 
@@ -85,7 +96,7 @@ const user = {
     },
 
     // 获取用户信息
-    GetInfo({commit, state}) {
+    GetInfo({ commit, state }) {
       return new Promise((resolve, reject) => {
         getInfo().then(res => {
           const user = res.user
@@ -115,7 +126,7 @@ const user = {
     },
 
     // 退出系统
-    LogOut({commit, state}) {
+    LogOut({ commit, state }) {
       return new Promise((resolve, reject) => {
         logout(state.token).then(() => {
           if (state.userId == 1) {  // 如果当前用户是admin的话
@@ -139,7 +150,7 @@ const user = {
     },
 
     // 前端 登出
-    FedLogOut({commit}) {
+    FedLogOut({ commit }) {
       return new Promise(resolve => {
         commit('SET_TOKEN', '')
         removeToken()

+ 83 - 3
zkqy-ui/src/views/loading.vue

@@ -10,16 +10,96 @@
 </template>
 
 <script>
+import { loginBySso, authorize } from "@/api/login";
+import { Base64 } from "js-base64";
 export default {
   name: "loading",
   props: [],
   components: {},
   data() {
-    return {};
+    return {
+      token: "",
+    };
   },
   computed: {},
-  methods: {},
-  onMounted() {},
+  methods: {
+    // 校验时间是否过时
+    validateTime(timeStamp) {
+      if (!timeStamp) return false;
+      let nowTime = parseInt(new Date().getTime() / 1000);
+      let limit = 120;
+      timeStamp = parseInt(timeStamp);
+      console.log(nowTime, timeStamp);
+      return nowTime - timeStamp < limit;
+    },
+    async initUserInfo() {
+      console.log(window.location.href);
+      console.log(this.$route.query);
+
+      let { bWVz } = this.$route.query;
+      if (bWVz) {
+        console.log("tenantCode", Base64.decode(bWVz));
+        let loginData = Base64.decode(bWVz).split("^_^");
+        console.log(loginData);
+        let code = loginData[0];
+        let username = loginData[1];
+        let timeStamp = loginData[2];
+        let isOutTime = this.validateTime(timeStamp);
+        isOutTime = true;
+        console.log(isOutTime);
+        if (isOutTime) {
+          try {
+            let res = await loginBySso(bWVz);
+            if (res.code == 200) {
+              // this.token = res.token;
+              this.$store
+                .dispatch("LoginBySso", {
+                  username: username,
+                  token: res.token,
+                })
+                .then(() => {
+                  this.$router
+                    .push({ path: this.redirect || "/" })
+                    .catch(() => {});
+                });
+            } else {
+              this.$message.error("网络异常,请重新跳转");
+              this.$router.push("/401");
+            }
+          } catch (error) {
+            this.$message.error("网络异常,请重新跳转");
+            this.$router.push("/401");
+          }
+        } else {
+          this.$message.error("链接已过期,请重新跳转");
+          this.$router.push("/401");
+        }
+      } else {
+        this.$message.error("参数异常,请重新跳转");
+        this.$router.push("/401");
+      }
+    },
+    async authorizeHandler() {
+      try {
+        let res = await authorize();
+        console.log(res);
+        if (res.code == 200) {
+          window.location.href = res.data;
+        } else {
+          this.$router.push("/401");
+        }
+      } catch (error) {}
+    },
+  },
+
+  mounted() {
+    if (this.$route.query.bWVz) {
+      this.initUserInfo();
+    }
+    if (this.$route.query.type == "oauth") {
+      this.authorizeHandler();
+    }
+  },
 };
 </script>
 

+ 1 - 1
zkqy-ui/src/views/login.vue

@@ -431,7 +431,7 @@ export default {
   align-items: center;
   height: 100%;
   background-image: url("../assets/images/background3.jpg");
-  background-size:100% 100%;
+  background-size: 100% 100%;
   /* padding-bottom: 55.19%;  */
   background-position: center;
   background-repeat: no-repeat;