qrcodeedDialog.vue 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <div>
  3. <el-dialog
  4. title="扫码签到"
  5. :visible.sync="dialogVisible"
  6. :close-on-click-modal="false"
  7. :close-on-press-escape="false"
  8. :append-to-body="true"
  9. class="qrcode-dialog"
  10. width="50%"
  11. top="6vh"
  12. center
  13. >
  14. <div class="codePic">
  15. <div id="qrcode" ref="qrcodes" class="qrCode" />
  16. </div>
  17. <span slot="footer" class="dialog-footer">
  18. <el-button type="primary" @click="downloadCode()">下载二维码</el-button>
  19. <el-button @click="dialogVisible = false">关 闭</el-button>
  20. </span>
  21. </el-dialog>
  22. </div>
  23. </template>
  24. <script>
  25. import QRCode from 'qrcodejs2' // 引入qrcode
  26. import { BASE_URL } from '@/constant'
  27. export default {
  28. name: 'qrcode',
  29. props: {
  30. visible: {
  31. type: Boolean,
  32. default: false
  33. },
  34. codeId: {
  35. type: String,
  36. default: ''
  37. }
  38. },
  39. data () {
  40. return {
  41. qrCode: '',
  42. dialogVisible: this.visible
  43. }
  44. },
  45. mounted () {
  46. this.$nextTick(() => {
  47. this.qrcodeRender()
  48. })
  49. },
  50. methods: {
  51. downloadCode () {
  52. const myCanvas = document.getElementById('qrcode').getElementsByTagName('canvas')
  53. const a = document.createElement('a')
  54. a.href = myCanvas[0].toDataURL('image/png')
  55. a.download = name
  56. a.click()
  57. },
  58. qrcodeRender () {
  59. if (this.qrCode) {
  60. this.$refs.qrcodes.innerHTML = ''
  61. }
  62. this.qrCode = new QRCode('qrcode', {
  63. width: 200,
  64. height: 200,
  65. text: `${BASE_URL}h5/#/pages/login/login?qrcodeId=${this.codeId}`,
  66. colorDark: '#000000', // 前景色
  67. colorLight: '#FFFFFF', // 背景色
  68. correctLevel: QRCode.CorrectLevel.L
  69. })
  70. }
  71. }
  72. }
  73. </script>
  74. <style lang="scss" scoped>
  75. .qrcode-dialog {
  76. .qrCode {
  77. display: flex;
  78. justify-content: center;
  79. margin: 5% 0;
  80. }
  81. .qrCode >img{
  82. width: 50%;
  83. }
  84. }
  85. </style>