qrcode.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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'],
  21. data() {
  22. return {
  23. tokenRechargeAddress: ''
  24. }
  25. },
  26. mounted() {
  27. this.getTokenAddress()
  28. },
  29. methods: {
  30. getTokenAddress() {
  31. postRequest(Api.getTokenRechargeAddress, {
  32. params: {
  33. tokenChain: this.tokenChain
  34. }
  35. }).then(res => {
  36. let { code, data } = res;
  37. if (code === 0) {
  38. this.tokenRechargeAddress = data.rechargeAddress;
  39. this.createQRCode(data.rechargeAddress)
  40. this.copyToken()
  41. }
  42. })
  43. },
  44. createQRCode(str) {
  45. var canvas = document.getElementById('canvas')
  46. QRCode.toCanvas(canvas, str, {
  47. width: 110,
  48. height: 110,
  49. scale: 1,
  50. color: {
  51. dark: '#000', // 二维码前景颜色
  52. light: '#fff' // 二维码背景颜色
  53. }
  54. }, function (error) {
  55. if (error) console.error(error)
  56. console.log('success!');
  57. })
  58. },
  59. copyToken() {
  60. var clipboard = new ClipboardJS('.copy-btn');
  61. clipboard.on('success', function (e) {
  62. Toast(`copy success`)
  63. e.clearSelection();
  64. });
  65. clipboard.on('error', function (e) {
  66. Toast(`copy error`)
  67. });
  68. }
  69. }
  70. }
  71. </script>
  72. <style lang="scss" scoped>
  73. .border {
  74. display: flex;
  75. justify-content: space-between;
  76. padding: 12px;
  77. border-radius: 8px;
  78. border: 1px solid #ECECEC;
  79. .ercode {
  80. overflow: hidden;
  81. width: 110px;
  82. height: 110px;
  83. canvas {
  84. width: 100%;
  85. height: 100%;
  86. transform: scale(1.24);
  87. }
  88. }
  89. .info {
  90. display: flex;
  91. flex-direction: column;
  92. justify-content: center;
  93. flex: 1;
  94. margin-left: 15px;
  95. word-break: break-all;
  96. .font {
  97. font-size: 14px;
  98. line-height: 22px;
  99. font-weight: 400;
  100. }
  101. .copy {
  102. display: flex;
  103. align-items: center;
  104. justify-content: center;
  105. color: #1D9BF0;
  106. font-size: 16px;
  107. font-weight: 600;
  108. width: 100%;
  109. height: 35px;
  110. margin-top: 13px;
  111. border-radius: 35px;
  112. border: 1px solid #1D9BF0;
  113. }
  114. }
  115. }
  116. </style>