qrcode.vue 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <template>
  2. <div id="qrcode">
  3. <img :src="dataUrl" />
  4. </div>
  5. </template>
  6. <script>
  7. import Vue from 'vue';
  8. import QRCode from 'qrcode';
  9. export default {
  10. name: 'qrcode',
  11. props: {
  12. qrcode: null,
  13. },
  14. components: {},
  15. data: () => ({
  16. dataUrl: null,
  17. token: null,
  18. }),
  19. async mounted() {
  20. await this.initQrcode();
  21. },
  22. created() {},
  23. computed: {},
  24. methods: {
  25. async initQrcode() {
  26. // 创建二维码
  27. if (!this.qrcode) return;
  28. let uri = `${Vue.config.weixin.baseUrl}/qrcode/${this.qrcode}/scan`;
  29. if (uri.startsWith('/')) {
  30. uri = `${location.protocol}//${location.host}${uri}`;
  31. }
  32. this.dataUrl = await QRCode.toDataURL(uri);
  33. this.$stomp({
  34. [`/exchange/qrcode.login/${this.qrcode}`]: this.onMessage,
  35. });
  36. },
  37. onMessage(message) {
  38. console.log('receive a message: ', message.body);
  39. if (message.body == 'scaned') {
  40. try {
  41. this.$emit('toReturn', message);
  42. console.log('扫码登录成功');
  43. } catch (err) {
  44. console.log('扫码登录失败');
  45. console.error(err);
  46. }
  47. }
  48. },
  49. },
  50. };
  51. </script>
  52. <style lang="less" scoped></style>