jiangping
2025-06-17 64fa2c33cd645e86d4e2a8c34c7881ea4aa678cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<template>
  <TableLayout >
    <!-- 表格和分页 -->
    <template v-slot:table-wrap>
      <ul class="toolbar" v-permissions="['business:category:create', 'business:category:delete']">
        <li><el-button type="primary" @click="handlePageChange">刷新</el-button></li>
      </ul>
        <el-table
          v-loading="isWorking.search"
          :data="tableData.list"
          stripe
          @selection-change="handleSelectionChange"
        >
        <el-table-column prop="name" label="表单入口名称" min-width="100px"></el-table-column>
        <el-table-column prop="url" label="二维码" min-width="100px">
          <template slot-scope="{ row }">
            <div :id="`qrcode${row.id}`" :ref="`qrcode${row.id}`"></div>
          </template>
        </el-table-column>
          <el-table-column
              label="操作"
              min-width="120"  >
            <template slot-scope="{row}">
              <el-button type="text" @click="download(row)"  >下载</el-button>
            </template>
          </el-table-column>
      </el-table>
      <canvas id="QRCode_header" ref="canvas" title="扫描二维码"></canvas>
    </template>
    <!-- 新建/修改 -->
  </TableLayout>
</template>
 
<script>
import BaseTable from '@/components/base/BaseTable'
import TableLayout from '@/layouts/TableLayout'
import QRCode from 'qrcodejs2'
export default {
  name: 'Category',
  extends: BaseTable,
  components: { TableLayout },
  data () {
    return {
      // 搜索
      searchForm: {
        id: '',
        creator: '',
        createDate: '',
        editor: '',
        editDate: '',
        isdeleted: '',
        name: '',
        remark: '',
        status: '',
        sortnum: '',
        imgurl: '',
        type: '',
        parentId: null,
        namePath: '',
        idPath: ''
      }
    }
  },
  created () {
    this.config({
      module: '二维码管理',
      api: '/system/dict'
    })
    this.handlePageChange()
  },
  methods: {
    handlePageChange () {
      this.isWorking.search = true
      var that = this
      this.api.qrcodeList({})
        .then(data => {
          that.tableData.list = data
          setTimeout(function () {
            that.tableData.list.forEach(row => {
              that.$refs['qrcode' + row.id].innerHTML = ''
              row.qrcodeImg = that.crateQrcodeShow('qrcode' + row.id, row.url)
            })
          }, 1000)
        })
        .catch(e => {
          this.$tip.apiFailed(e)
        })
        .finally(() => {
          this.isWorking.search = false
        })
    },
    download (row) {
      const nodeList = Array.prototype.slice.call(row.qrcodeImg._el.children)
      const img = nodeList.find((item) => item.nodeName.toUpperCase() === 'IMG')// 选出图片类型
      // 构建画布
      const canvas = document.createElement('canvas')
      canvas.width = 220
      canvas.height = 220
      const ctx = canvas.getContext('2d')
      ctx.fillStyle = 'white'
      ctx.fillRect(0, 0, canvas.width, canvas.height) // 填充整个画布区域,确保背景色覆盖整个画布
      ctx.drawImage(img, 10,10,200,200)
      // 构造url
      const url = canvas.toDataURL('image/png')
 
      const a = document.createElement('a')
      a.href = url
      a.download = `${row.name}-二维码.png`
      a.click()
      a.remove()
    },
    crateQrcodeShow (div, qrcode1) {
      if (qrcode1 == null || qrcode1 == '') {
        return
      }
      return new QRCode(div, {
        width: 120,
        height: 120,
        text: qrcode1
      })
    }
  }
}
</script>