employeeDialog.vue
4.84 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<template>
<d2-dialog
title="员工注册码"
:is-visible="employeeVisible"
class="qRcodeDailog"
width="750px"
btn-ok-label="下载"
@closeDialog="handleClose"
@confirmDialog="handleAddConfirm"
>
<div class="employee-wrap" v-if="qrCodeURL" ref="printContainer">
<div class="title">员工注册码</div>
<div class="code">
<img :src="qrCodeURL" alt="" />
</div>
<div class="merchantName">{{ info.merchantName }}</div>
<div class="merchantId">{{ info.merchantId }}</div>
</div>
</d2-dialog>
</template>
<script>
// alipayQrcode
import { employeeRegister } from "@api/shopConfig"
import html2canvas from "html2canvas"
export default {
name: "employeeDialog",
data() {
return {
// 基本数据
baseData: {
merchantList: []
},
loading: true,
qrCodeURL: ""
}
},
props: {
employeeVisible: {
type: Boolean,
default: false
},
info: {
type: Object,
default: null
}
},
watch: {},
created() {},
components: {},
mounted() {
this.getEmployeeRegister()
console.log(this.info)
},
methods: {
getEmployeeRegister() {
employeeRegister({
merchantId: this.info.merchantId
}).then((res) => {
if (res) {
this.qrCodeURL = res.qrCode
this.loading = false
}
})
},
downloadIamge(imgsrc, name = new Date().getTime()) {
//下载图片地址和图片名
var image = new Image()
// 解决跨域 Canvas 污染问题
image.setAttribute("crossOrigin", "anonymous")
image.onload = function () {
var canvas = document.createElement("canvas")
canvas.width = image.width
canvas.height = image.height
var context = canvas.getContext("2d")
context.drawImage(image, 0, 0, image.width, image.height)
var url = canvas.toDataURL("image/png") //得到图片的base64编码数据
var a = document.createElement("a") // 生成一个a元素
var event = new MouseEvent("click") // 创建一个单击事件
a.download = name || "photo" // 设置图片名称
a.href = url // 将生成的URL设置为a.href属性
a.dispatchEvent(event) // 触发a的单击事件
}
image.src = imgsrc
},
handleAddConfirm() {
const content = this.$refs.printContainer;
html2canvas(content, {
width: content.clientWidth, // 设置图片宽度
height: content.clientHeight,
allowTaint: true, // 允许污染
taintTest: true, // 在渲染前测试图片(没整明白有啥用)
useCORS: true // 使用跨域(当allowTaint为true时这段代码没什么用,下面解释)
}).then((canvas) => {
const url = canvas.toDataURL("image/png")
const filename = "员工注册码.png"
const link = document.createElement("a")
link.href = url
link.download = filename
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
// that.convertCanvasToImg(canvas, "员工注册码.png")
})
},
// 转换图片格式
convertCanvasToImg(canvas, name) {
// canvas base64 转 blob
var myBlob = this.dataURLtoBlob(canvas.toDataURL("img/png", 0.92))
// blob转URL对象
const myUrl = URL.createObjectURL(myBlob)
// 创建a标签,下载图片
this.downImg(myUrl, name)
},
// base64 转 blob
dataURLtoBlob(dataurl) {
var arr = dataurl.split(",")
var mime = arr[0].match(/:(.*?);/)[1]
var bstr = atob(arr[1])
var n = bstr.length
var u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new Blob([u8arr], { type: mime })
},
// 下载图片 filename是图片名称
downImg(url, name) {
// 创建a标签 并设置其相关属性,最后触发其点击事件
const a = document.createElement("a")
const clickEvent = document.createEvent("MouseEvents")
a.setAttribute("href", url)
a.setAttribute("download", name)
a.setAttribute("target", "_blank")
clickEvent.initEvent("click", true, true)
a.dispatchEvent(clickEvent)
},
handleClose() {
this.$emit("dialogClose", true)
}
}
}
</script>
<style scoped lang="scss">
::v-deep .el-dialog__body{
display: flex;
align-items: center;
flex-direction: column;
justify-content: center;
}
.employee-wrap {
display: flex;
align-items: center;
flex-direction: column;
justify-content: center;
width:196px;
.title {
font-weight: 500;
font-size: 14px;
color: #333333;
margin-bottom: 8px;
}
.code {
width: 120px;
height: 120px;
img {
width: 100%;
}
}
.merchantName,
.merchantId {
color: #333333;
font-size: 14px;
margin-top: 8px;
}
}
</style>