qrcode.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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(canvas, str, {
  55. width: 110,
  56. height: 110,
  57. scale: 1,
  58. color: {
  59. dark: '#000', // 二维码前景颜色
  60. light: '#fff' // 二维码背景颜色
  61. }
  62. }, function (error) {
  63. if (error) console.error(error)
  64. console.log('success!');
  65. })
  66. },
  67. copyToken() {
  68. var clipboard = new ClipboardJS('.copy-btn');
  69. clipboard.on('success', function (e) {
  70. Toast(`copy success`)
  71. e.clearSelection();
  72. });
  73. clipboard.on('error', function (e) {
  74. Toast(`copy error`)
  75. });
  76. },
  77. copy() {
  78. this.$emit('copy');
  79. },
  80. }
  81. }
  82. </script>
  83. <style lang="scss" scoped>
  84. .border {
  85. display: flex;
  86. justify-content: space-between;
  87. padding: 12px;
  88. border-radius: 8px;
  89. border: 1px solid #ECECEC;
  90. .ercode {
  91. overflow: hidden;
  92. width: 110px;
  93. height: 110px;
  94. canvas {
  95. width: 100%;
  96. height: 100%;
  97. transform: scale(1.24);
  98. }
  99. }
  100. .info {
  101. display: flex;
  102. flex-direction: column;
  103. justify-content: center;
  104. flex: 1;
  105. margin-left: 15px;
  106. word-break: break-all;
  107. .font {
  108. font-size: 14px;
  109. line-height: 22px;
  110. font-weight: 400;
  111. }
  112. .copy {
  113. display: flex;
  114. align-items: center;
  115. justify-content: center;
  116. color: #1D9BF0;
  117. font-size: 16px;
  118. font-weight: 600;
  119. width: 100%;
  120. height: 35px;
  121. margin-top: 13px;
  122. border-radius: 35px;
  123. border: 1px solid #1D9BF0;
  124. }
  125. }
  126. }
  127. </style>