Przeglądaj źródła

task-61 添加单点登录

cc12458 1 rok temu
rodzic
commit
79c6e9fc54
2 zmienionych plików z 55 dodań i 5 usunięć
  1. 11 1
      src/api/user.js
  2. 44 4
      src/views/Home.vue

+ 11 - 1
src/api/user.js

@@ -31,4 +31,14 @@ export function getCodeimg() {
     url: '/captchaImage',
     method: 'get'
   })
-};
+};
+
+export function loginBySSO(value) {
+  return request({
+    url: `/pre_interface/V6.0/getToken?${value}`,
+    method: 'post',
+  }).then(res => {
+    if (res.ResultCode === 0) return res.Data.token;
+    else throw res.ResultInfo;
+  })
+}

+ 44 - 4
src/views/Home.vue

@@ -3,7 +3,7 @@
     <div class="top"></div>
     <div class="flex-center flex-column login-body">
       <h2>中医智能辅助诊疗系统</h2>
-      <div class="login-form">
+      <div class="login-form" v-if="!useToken">
         <!-- <div class="flex-center">
           <img src="../assets/logo.png" alt="" class="logo" />
         </div>-->
@@ -28,6 +28,9 @@
           <el-checkbox v-model="checked">记住我</el-checkbox>
         </div>
       </div>
+      <div v-else-if="useTokenError" style="max-width: 50vw;background-color: rgba(255,255,255,0.4);">
+        <el-result icon="error" title="登录错误" :subTitle="useTokenError"></el-result>
+      </div>
     </div>
 
     <div class="bottom">
@@ -38,8 +41,13 @@
 </template>
 
 <script>
-import { login, getCode, getCodeimg } from "@/api/user.js";
-import { setTimeout } from "timers";
+import {login, getCode, getCodeimg, loginBySSO} from '@/api/user.js';
+
+function getURLSearchParams(value) {
+  value ??= `${ location.search }&${ location.hash.split('?')[ 1 ] || '' }`;
+  return new URLSearchParams(value);
+}
+
 export default {
   name: "Home",
   components: {},
@@ -48,12 +56,21 @@ export default {
       name: "",
       pas: "",
       code: "",
-
+      useToken: true,
+      useTokenError: '',
       checked: false,
       src: process.env.VUE_APP_BASE_API + "captchaImage"
     };
   },
   created() {
+    const sso = getURLSearchParams()
+    this.useToken = sso.has("token")
+    if (this.useToken) {
+      localStorage.setItem("sso-token", sso.get("token"));
+      this.loginBySSO(sso.toString())
+      return;
+    }
+    localStorage.removeItem("sso-token");
     // this.getCodeimg()
     this.changeImg();
     setTimeout(() => {
@@ -112,6 +129,29 @@ export default {
         }, 1500);
       }
     },
+    async loginBySSO(value) {
+      this.useTokenError = '';
+      let loading;
+      const timer = setTimeout(() => {
+        loading = this.$loading({
+          lock: true,
+          text: '正在登录',
+          spinner: 'el-icon-loading',
+          background: 'rgba(0, 0, 0, 0.7)',
+        });
+      }, 2000);
+      try {
+        const token = await loginBySSO(value);
+        localStorage.setItem('token', token);
+        this.$nextTick(() => {
+          this.$router.push({path: '/index/todaypatients'});
+        });
+      } catch (e) {
+        this.useTokenError = e.message;
+      }
+      clearTimeout(timer);
+      if (loading) loading.close();
+    },
 
     // 修改验证码
     changeImg() {