qrcode.vue 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. uri: { type: String },
  14. },
  15. components: {},
  16. data: () => ({
  17. dataUrl: null,
  18. token: null,
  19. }),
  20. async mounted() {
  21. await this.initQrcode();
  22. },
  23. created() {},
  24. computed: {},
  25. methods: {
  26. async initQrcode() {
  27. // 创建二维码
  28. if (!this.qrcode) return;
  29. //二维码地址应该是变量,不过这里需要处理
  30. let uri = `${Vue.config.weixin.baseUrl}${this.uri}`;
  31. if (uri.startsWith('/')) {
  32. uri = `${location.protocol}//${location.host}${uri}`;
  33. }
  34. this.dataUrl = await QRCode.toDataURL(uri);
  35. this.$stomp({
  36. [`/exchange/qrcode.login/${this.qrcode}`]: this.onMessage,
  37. });
  38. },
  39. onMessage(message) {
  40. console.log('receive a message: ', message.body);
  41. if (message.body == 'scaned') {
  42. try {
  43. this.$emit('toReturn', message);
  44. console.log('扫码登录成功');
  45. } catch (err) {
  46. console.log('扫码登录失败');
  47. console.error(err);
  48. }
  49. }
  50. },
  51. },
  52. };
  53. </script>
  54. <style lang="less" scoped></style>