qrcodeedDialog.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <div>
  3. <el-dialog
  4. :title="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 { PUBLIC_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. type: {
  39. type: String,
  40. default: ''
  41. },
  42. title: {
  43. type: String,
  44. default: '扫码签到'
  45. },
  46. prefixUrl: {
  47. type: String,
  48. default: ''
  49. }
  50. },
  51. data() {
  52. return {
  53. qrCode: '',
  54. dialogVisible: this.visible,
  55. url: 'http://192.168.2.16:8080/'
  56. }
  57. },
  58. // mounted () {
  59. // this.$nextTick(() => {
  60. // this.qrcodeRender()
  61. // })
  62. // },
  63. watch: {
  64. dialogVisible: {
  65. handler: function (val, oldVal) {
  66. if (val === true) {
  67. this.$nextTick(() => {
  68. this.qrcodeRender()
  69. })
  70. }
  71. },
  72. deep: true,
  73. immediate: true
  74. }
  75. },
  76. methods: {
  77. downloadCode() {
  78. const myCanvas = document
  79. .getElementById('qrcode')
  80. .getElementsByTagName('canvas')
  81. const a = document.createElement('a')
  82. a.href = myCanvas[0].toDataURL('image/png')
  83. a.download = name
  84. a.click()
  85. },
  86. qrcodeRender() {
  87. if (this.qrCode) {
  88. this.$refs.qrcodes.innerHTML = ''
  89. }
  90. this.qrCode = new QRCode('qrcode', {
  91. width: 200,
  92. height: 200,
  93. // text: `${this.url}h5/#/pages/signin/signin?codeId=${this.codeId}`,
  94. text: `${PUBLIC_URL}#/bpmn${this.prefixUrl || '/siginin/index'}?codeId=${this.codeId}&type=${this.type}`,
  95. // text: `http://demo2.local/#/bpmn/siginin/index?codeId=${this.codeId}&type=${this.type}`,
  96. colorDark: '#000000', // 前景色
  97. colorLight: '#FFFFFF', // 背景色
  98. correctLevel: QRCode.CorrectLevel.L
  99. })
  100. }
  101. }
  102. }
  103. </script>
  104. <style lang="scss" scoped>
  105. .qrcode-dialog {
  106. .qrCode {
  107. display: flex;
  108. justify-content: center;
  109. margin: 5% 0;
  110. }
  111. .qrCode > img {
  112. width: 50%;
  113. }
  114. }
  115. </style>