Selaa lähdekoodia

增加外部链接自动登录

mork 1 kuukausi sitten
vanhempi
sitoutus
b2b0c77d43
2 muutettua tiedostoa jossa 92 lisäystä ja 0 poistoa
  1. 5 0
      src/router/index.js
  2. 87 0
      src/views/AutoLogin.vue

+ 5 - 0
src/router/index.js

@@ -11,6 +11,11 @@ const routes = [
     name: 'index',
     component: () => import('@/views/IndexView.vue'),
   },
+  {
+    path: '/autoLogin',
+    name: 'autoLogin',
+    component: () => import('@/views/AutoLogin.vue')
+  },
 ]
 
 const router = createRouter({

+ 87 - 0
src/views/AutoLogin.vue

@@ -0,0 +1,87 @@
+<template>
+  <div class="main">
+    
+  </div>
+</template>
+
+<script>
+import encrypt‌ from "@/utils/encrypt‌"; // 加密登录
+import { ElMessage, ElLoading } from "element-plus";
+
+export default {
+  name: "AutoLogin",
+  data() {
+    return {
+      loginLoad: null,
+      ruleForm: {
+        username: "",
+        password: "",
+        clientId: webConfig.serviceId,
+        type: 1
+      },
+    };
+  },
+  created() {},
+  mounted() {
+    let that = this;
+    // 监听父页面发来的消息
+    window.addEventListener('message', async (e) => {
+      // 校验发送来源,防止恶意页面跨域发送消息
+      console.log("校验发送来源:", e.origin);
+      if (e.origin !== 'http://localhost:5173' && e.origin !== 'http://121.43.55.7:2124') return
+        const data = e.data
+        if (data.type === 'AUTO_LOGIN') {
+          const { username, password } = data
+          that.ruleForm.username = username;
+          that.ruleForm.password = password;
+          // 执行登录
+          that.onAutoSubmit();
+        }
+      })
+  },
+  methods: {
+    showLoading(){
+        this.loginLoad = ElLoading.service({
+          lock: true,
+          text: 'Loading',
+          background: 'rgba(0, 0, 0, 0.7)',
+        })
+    },
+    onAutoSubmit() {
+      let that = this;
+      console.log("进来了");
+      this.showLoading();
+      encrypt‌(that.ruleForm).then((res) => {
+            this.loginLoad.close();
+            that.$store.state.login = true;
+            that.$store.state.user = res.content;
+            that.$router.push("/index");
+            ElMessage({
+              message: "加载成功",
+              type: "success",
+              duration: 1500
+            });
+        }).catch((err) => {
+          this.loginLoad.close();
+          ElMessage({
+              message: err,
+              type: "error",
+              duration: 1500
+          });
+      });
+    }
+  }
+};
+</script>
+<style>
+
+.main {
+  height: 100%;
+  width: 100%;
+  position: absolute;
+  background-color: #fffbf0;
+}
+
+</style>
+
+