qrcodeedDialog.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.getElementById('qrcode').getElementsByTagName('canvas')
  79. const a = document.createElement('a')
  80. a.href = myCanvas[0].toDataURL('image/png')
  81. a.download = name
  82. a.click()
  83. },
  84. qrcodeRender () {
  85. if (this.qrCode) {
  86. this.$refs.qrcodes.innerHTML = ''
  87. }
  88. this.qrCode = new QRCode('qrcode', {
  89. width: 200,
  90. height: 200,
  91. // text: `${this.url}h5/#/pages/signin/signin?codeId=${this.codeId}`,
  92. text: `${PUBLIC_URL}#/bpmn${this.prefixUrl || '/siginin/index'}?codeId=${this.codeId}&type=${this.type}`,
  93. // text: `http://demo2.local/#/bpmn/siginin/index?codeId=${this.codeId}&type=${this.type}`,
  94. colorDark: '#000000', // 前景色
  95. colorLight: '#FFFFFF', // 背景色
  96. correctLevel: QRCode.CorrectLevel.L
  97. })
  98. }
  99. }
  100. }
  101. </script>
  102. <style lang="scss" scoped>
  103. .qrcode-dialog {
  104. .qrCode {
  105. display: flex;
  106. justify-content: center;
  107. margin: 5% 0;
  108. }
  109. .qrCode >img{
  110. width: 50%;
  111. }
  112. }
  113. </style>