lrf402788946 5 years ago
parent
commit
2d1fee4162
2 changed files with 67 additions and 6 deletions
  1. 55 0
      components/qrcode.vue
  2. 12 6
      store/login.js

+ 55 - 0
components/qrcode.vue

@@ -0,0 +1,55 @@
+<template>
+  <div id="qrcode">
+    <img :src="dataUrl" />
+  </div>
+</template>
+
+<script>
+import Vue from 'vue';
+import QRCode from 'qrcode';
+export default {
+  name: 'qrcode',
+  props: {
+    qrcode: null,
+  },
+  components: {},
+  data: () => ({
+    dataUrl: null,
+    token: null,
+  }),
+  async mounted() {
+    await this.initQrcode();
+  },
+  created() {},
+  computed: {},
+  methods: {
+    async initQrcode() {
+      // 创建二维码
+      if (!this.qrcode) return;
+      //二维码地址应该是变量,不过这里需要处理
+      let uri = `${Vue.config.weixin.baseUrl}/qrcode/${this.qrcode}/scan`;
+      if (uri.startsWith('/')) {
+        uri = `${location.protocol}//${location.host}${uri}`;
+      }
+      this.dataUrl = await QRCode.toDataURL(uri);
+      this.$stomp({
+        [`/exchange/qrcode.login/${this.qrcode}`]: this.onMessage,
+      });
+    },
+    onMessage(message) {
+      console.log('receive a message: ', message.body);
+      if (message.body == 'scaned') {
+        try {
+          this.$emit('toReturn', message);
+          console.log('扫码登录成功');
+        } catch (err) {
+          console.log('扫码登录失败');
+          console.error(err);
+        }
+      }
+    },
+  },
+};
+</script>
+
+<style lang="less" scoped></style>

+ 12 - 6
store/login.js

@@ -13,13 +13,21 @@ const state = () => ({});
 const mutations = {};
 
 const actions = {
-  async login({ commit }, { user, router, path = '/', needReturn = false }) {
+  async login({ commit }, { user, router, path = '/', needReturn = false, typeCheck = true }) {
     const res = await this.$axios.$post(`${api.interface}`, user);
+    const setUser = (user, commit) => {
+      localStorage.setItem('user', JSON.stringify(user));
+      commit('setUser', user, { root: true });
+    };
     if (res.errcode === 0) {
       let user = jwt.decode(res.data);
+      if (!typeCheck) {
+        setUser(user, commit);
+        if (needReturn) return res;
+        else router.push(path);
+      }
       if (user.type == process.env.VUE_APP_USER_TYPE) {
-        localStorage.setItem('user', JSON.stringify(user));
-        commit('setUser', user, { root: true });
+        setUser(user, commit);
         Notification({
           title: '登录成功',
           message: `欢迎,${user.name}`,
@@ -27,9 +35,7 @@ const actions = {
           duration: 2000,
         });
         if (needReturn) return res;
-        else {
-          router.push(path);
-        }
+        else router.push(path);
       } else {
         Notification({
           title: '请重新登陆',