qRcodeDialog.vue
6.88 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<!-- 下载对账单 -->
<template>
<d2-dialog title="收款二维码"
:isVisible="qRcodeVisible"
class="qRcodeDailog"
width="40%"
btnOkLabel="下载二维码"
@closeDialog="handleClose"
@confirmDialog="handleAddConfirm">
<div class="codeDiv"
id="view">
<img style="width:100%;height:100%"
src="./images/code3.png">
<!-- v-if="qrCodeURL" <div class="imgDiv"> -->
<img v-if="qrCodeURL"
class="qrImg"
id="img1"
crossorigin="anonymous"
:src="qrCodeURL">
<div v-if="!qrCodeURL"
class="el-icon-loading loadingIcon"></div>
<!-- </div> -->
<div class="merchantNameDiv">{{info.yqMerchantName}}</div>
</div>
</d2-dialog>
</template>
<script>
import { createWeappQrcode } from '@api/serviceManage'
import html2canvas from 'html2canvas'
export default {
name: 'qRcodeDialog',
data() {
return {
infoForm: {
merchantType: '',
merchant: [],
payStartDate: '',
payEndDate: '',
reportType: '1'
},
// 基本数据
baseData: {
merchantList: []
},
qrCodeURL: '',
defaultImg: require('./images/defaultImg.png')
}
},
props: {
qRcodeVisible: {
type: Boolean,
default: false
},
info: {
type: Object,
default: null
}
},
watch: {
},
created() {
},
components: {},
mounted() {
this.createQrcodeImage()
},
methods: {
/**
* @description: 图片转base64
* @param {String} url 需要转换的图片原链接(http://....jpg)
* @param {String} outputFormat 转换出来的图片的类型(canvas支持jpg/png格式)
* @return: 返回图片对应的base64编码
*/
getBas64(url, outputFormat = 'image/png') {
return new Promise(function (resolve, reject) {
let canvas = document.createElement('CANVAS')
const ctx = canvas.getContext('2d')
const img = new Image()
img.crossOrigin = 'Anonymous' // 重点!设置image对象可跨域请求
img.onload = function () {
canvas.height = img.height
canvas.width = img.width
ctx.drawImage(img, 0, 0)
const dataURL = canvas.toDataURL(outputFormat)
canvas = null
resolve(dataURL)
}
// img.src = url; // 旧的方式
img.src = url + '?t=' + new Date().valueOf() // 防止oss的缓存问题
})
},
createQrcodeImage() {
const oParams = {}
oParams.channelType = 1
oParams.appId = 'wx06d1198e751435d1'
// oParams.appId = 'wx902f0b9c4b2c669d'
oParams.qrcodeType = 'QR_WXA_UNLIMIT'
oParams.path = 'packagesPayment/pages/QRPayment/QRPayment'
// oParams.path = 'packagesPayment/pages/payment/index'
oParams.scene = `merchantId=${this.info.yqMerchantId}&merchantName=${this.info.yqMerchantName}`
createWeappQrcode(oParams).then(res => {
this.qrCodeURL = res
// function getBase64(imgUrl) {
// window.URL = window.URL || window.webkitURL
// var xhr = new XMLHttpRequest()
// xhr.open('get', imgUrl, true)
// xhr.responseType = 'blob'
// xhr.onload = function () {
// if (this.status == 200) {
// // 得到一个blob对象
// var blob = this.response
// console.log('blob', blob)
// // 至关重要
// const oFileReader = new FileReader()
// oFileReader.onloadend = function (e) {
// // 此处拿到的已经是base64的图片了,可以赋值做相应的处理
// console.log(e.target.result)
// }
// oFileReader.readAsDataURL(blob)
// }
// }
// xhr.send()
// }
// console.log(getBase64(res))
// getBase64('需要转成base64的图片url')
})
},
// 下载保存图片
takeScreenshot() {
// const opts = {
// useCORS: true
// }
html2canvas(document.getElementById('view'), {
backgroundColor: null,
allowTaint: true, // 允许污染
taintTest: true, // 在渲染前测试图片(没整明白有啥用)
useCORS: true // 使用跨域(当allowTaint为true时这段代码没什么用,下面解释)
}).then(canvas => {
this.convertCanvasToImg(canvas)
})
},
// 转换图片格式
convertCanvasToImg(canvas) {
// canvas base64 转 blob
var myBlob = this.dataURLtoBlob(canvas.toDataURL('img/png', 0.92))
// blob转URL对象
const myUrl = URL.createObjectURL(myBlob)
// 创建a标签,下载图片
this.downImg(myUrl)
},
// 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) {
// 创建a标签 并设置其相关属性,最后触发其点击事件
const a = document.createElement('a')
const clickEvent = document.createEvent('MouseEvents')
a.setAttribute('href', url)
a.setAttribute('download', 'filename')
a.setAttribute('target', '_blank')
clickEvent.initEvent('click', true, true)
a.dispatchEvent(clickEvent)
},
handleAddConfirm() {
this.takeScreenshot()
},
handleClose() {
this.$emit('dialogClose', true)
}
}
}
</script>
<style>
.dateItem {
display: flex;
}
.dateItem .el-form-item__content {
display: flex;
margin-left: 0 !important;
}
.codeDiv {
width: 345px;
height: 468px;
margin: 0px auto;
position: relative;
/* background-image: url('./images/code3.png'); */
}
.qrImg {
width: 150px;
height: 150px;
position: absolute;
left: 50%;
top: 42%;
transform: translate(-50%, -50%);
/* background-color: #d8d8d8; */
z-index: 2;
}
.loadingIcon {
position: absolute;
/* width: 44px;
height: 44px; */
font-size: 30px;
/* top: 60px;
left: 60px; */
/* transform: translate(-50%, -50%); */
/* width: 150px;
height: 150px; */
position: absolute;
left: 46%;
top: 40%;
transform: translate(-50%, -50%);
}
.merchantNameDiv {
width: 150px;
height: 20px;
position: absolute;
left: 50%;
top: 63%;
transform: translate(-50%, -50%);
text-align: center;
font-size: 14px;
font-family: PingFangSC-Medium, PingFang SC;
font-weight: 500;
color: #333333;
line-height: 17px;
}
</style>