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