qrcodeedDialog.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <div>
  3. <el-dialog
  4. title="扫码签到"
  5. :visible.sync="centerDialogVisible"
  6. :append-to-body='true'
  7. width="50%"
  8. center>
  9. <div class="codePic" >
  10. <div id="qrcode" ref="qrcodes" class="qrCode" />
  11. </div>
  12. <span slot="footer" class="dialog-footer">
  13. <el-button type="primary" @click="downloadCode()">下载二维码</el-button>
  14. <el-button @click="centerDialogVisible = false">关 闭</el-button>
  15. </span>
  16. </el-dialog>
  17. </div>
  18. </template>
  19. <script>
  20. import QRCode from 'qrcodejs2' // 引入qrcode
  21. import { BASE_URL } from '@/constant'
  22. export default {
  23. name: 'qrcode',
  24. props: {
  25. field: Object,
  26. formData: {
  27. type: Object,
  28. default () {
  29. return {}
  30. }
  31. },
  32. readonly: {
  33. type: Boolean,
  34. default: false
  35. },
  36. dialogVisible:{
  37. type: Boolean,
  38. default: false
  39. },
  40. codeId:{
  41. type: String,
  42. default: ''
  43. }
  44. },
  45. data () {
  46. return {
  47. qrCode: '',
  48. baseUrl: process.env.VUE_APP_BASE_URL,
  49. centerDialogVisible:this.dialogVisible
  50. }
  51. },
  52. watch: {
  53. 'formData.changJingId': {
  54. handler () {
  55. this.$nextTick(() => {
  56. this.qrcode()
  57. })
  58. },
  59. immediate: true
  60. }
  61. },
  62. mounted () {
  63. // setTimeout(() => {
  64. // this.qrcode()
  65. // }, 500)
  66. },
  67. destroyed () {
  68. if (window.qrTimer) {
  69. clearInterval(window.qrTimer)
  70. }
  71. },
  72. methods: {
  73. downloadCode(){
  74. let myCanvas = document.getElementById("qrcode").getElementsByTagName("canvas");
  75. let a = document.createElement("a");
  76. a.href = myCanvas[0].toDataURL("image/png");
  77. a.download = name;
  78. a.click();
  79. },
  80. qrcode () {
  81. if (this.qrCode) {
  82. this.$refs.qrcodes.innerHTML = ''
  83. }
  84. this.qrCode = new QRCode('qrcode', {
  85. width: 132,
  86. height: 132,
  87. text:`${this.baseUrl}connect/oauth2/authorize?appid=wxf0aecf99696061a3&redirect_uri=${encodeURIComponent(`${BASE_URL}#/ziliao`)}&response_type=code&scope=snsapi_base&state=${this.formData.changJingId}#wechat_redirect`,
  88. // text: `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0aecf99696061a3&redirect_uri=${encodeURIComponent(`${BASE_URL}#/ziliao`)}&response_type=code&scope=snsapi_base&state=${this.formData.changJingId}#wechat_redirect`,
  89. colorDark: '#000000', // 前景色
  90. colorLight: '#FFFFFF', // 背景色
  91. correctLevel: QRCode.CorrectLevel.L
  92. })
  93. }
  94. }
  95. }
  96. </script>
  97. <style lang="scss" scoped>
  98. .qrCode {
  99. display: flex;
  100. justify-content: center;
  101. margin: 5% 0;
  102. }
  103. .qrCode >img{
  104. width: 50%;
  105. }
  106. </style>