qrcode.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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" :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. }
  78. }
  79. </script>
  80. <style lang="scss" scoped>
  81. .border {
  82. display: flex;
  83. justify-content: space-between;
  84. padding: 12px;
  85. border-radius: 8px;
  86. border: 1px solid #ECECEC;
  87. .ercode {
  88. overflow: hidden;
  89. width: 110px;
  90. height: 110px;
  91. canvas {
  92. width: 100%;
  93. height: 100%;
  94. transform: scale(1.24);
  95. }
  96. }
  97. .info {
  98. display: flex;
  99. flex-direction: column;
  100. justify-content: center;
  101. flex: 1;
  102. margin-left: 15px;
  103. word-break: break-all;
  104. .font {
  105. font-size: 14px;
  106. line-height: 22px;
  107. font-weight: 400;
  108. }
  109. .copy {
  110. display: flex;
  111. align-items: center;
  112. justify-content: center;
  113. color: #1D9BF0;
  114. font-size: 16px;
  115. font-weight: 600;
  116. width: 100%;
  117. height: 35px;
  118. margin-top: 13px;
  119. border-radius: 35px;
  120. border: 1px solid #1D9BF0;
  121. }
  122. }
  123. }
  124. </style>