qrcode.vue 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. if (uri.startsWith('/')) {
  34. uri = `${location.protocol}//${location.host}${uri}`;
  35. }
  36. this.dataUrl = await QRCode.toDataURL(uri);
  37. this.$stomp({
  38. [`/exchange/${this.exchange}/${this.qrcode}`]: this.onMessage,
  39. });
  40. console.log(`/exchange/${this.exchange}/${this.qrcode}`);
  41. console.log(uri);
  42. },
  43. onMessage(message) {
  44. console.log('receive a message: ', message.body);
  45. this.$emit('toReturn', message);
  46. },
  47. },
  48. };
  49. </script>
  50. <style lang="less" scoped></style>