qrcode.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <div class="border">
  3. <div class="ercode">
  4. <canvas id="canvas"></canvas>
  5. </div>
  6. <div class="info">
  7. <div class="font">{{ tokenRechargeAddress }}</div>
  8. <div class="copy copy-btn" @click="copy" :data-clipboard-text="tokenRechargeAddress">Copy</div>
  9. </div>
  10. </div>
  11. </template>
  12. <script>
  13. let QRCode = require('qrcode');
  14. let ClipboardJS = require('clipboard');
  15. import Api from '../http/api';
  16. import { postRequest } from '../http/index';
  17. import { Toast } from 'vant';
  18. export default {
  19. name: 'qrCode',
  20. props: ['tokenChain', 'rechargeAddress'],
  21. data() {
  22. return {
  23. tokenRechargeAddress: '',
  24. };
  25. },
  26. mounted() {
  27. this.getTokenAddress();
  28. },
  29. methods: {
  30. getTokenAddress() {
  31. if (this.rechargeAddress !== '') {
  32. // 有过查询
  33. this.tokenRechargeAddress = this.rechargeAddress;
  34. this.createQRCode(this.rechargeAddress);
  35. this.copyToken();
  36. } else {
  37. // 没查询过
  38. postRequest(Api.getTokenRechargeAddress, {
  39. params: {
  40. tokenChain: this.tokenChain,
  41. },
  42. }).then((res) => {
  43. let { code, data } = res;
  44. if (code === 0) {
  45. this.tokenRechargeAddress = data.rechargeAddress;
  46. this.createQRCode(data.rechargeAddress);
  47. this.copyToken();
  48. }
  49. });
  50. }
  51. },
  52. createQRCode(str) {
  53. var canvas = document.getElementById('canvas');
  54. QRCode.toCanvas(
  55. canvas,
  56. str,
  57. {
  58. width: 110,
  59. height: 110,
  60. scale: 1,
  61. color: {
  62. dark: '#000', // 二维码前景颜色
  63. light: '#fff', // 二维码背景颜色
  64. },
  65. },
  66. function (error) {
  67. if (error) console.error(error);
  68. console.log('success!');
  69. }
  70. );
  71. },
  72. copyToken() {
  73. var clipboard = new ClipboardJS('.copy-btn');
  74. clipboard.on('success', function (e) {
  75. Toast(`copy success`);
  76. e.clearSelection();
  77. });
  78. clipboard.on('error', function () {
  79. Toast(`copy error`);
  80. });
  81. },
  82. copy() {
  83. this.$emit('copy');
  84. },
  85. },
  86. };
  87. </script>
  88. <style lang="scss" scoped>
  89. .border {
  90. display: flex;
  91. justify-content: space-between;
  92. padding: 12px;
  93. border-radius: 8px;
  94. border: 1px solid #ececec;
  95. .ercode {
  96. overflow: hidden;
  97. width: 110px;
  98. height: 110px;
  99. canvas {
  100. width: 100%;
  101. height: 100%;
  102. transform: scale(1.24);
  103. }
  104. }
  105. .info {
  106. display: flex;
  107. flex-direction: column;
  108. justify-content: center;
  109. flex: 1;
  110. margin-left: 15px;
  111. word-break: break-all;
  112. .font {
  113. font-size: 14px;
  114. line-height: 22px;
  115. font-weight: 400;
  116. }
  117. .copy {
  118. display: flex;
  119. align-items: center;
  120. justify-content: center;
  121. color: #1d9bf0;
  122. font-size: 16px;
  123. font-weight: 600;
  124. width: 100%;
  125. height: 35px;
  126. margin-top: 13px;
  127. border-radius: 35px;
  128. border: 1px solid #1d9bf0;
  129. }
  130. }
  131. }
  132. </style>