qRcodeDialog.vue
10.2 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<template>
<d2-dialog title="收款二维码"
:is-visible="qRcodeVisible"
class="qRcodeDailog"
width="750px"
btn-ok-label="全部下载"
@closeDialog="handleClose"
@confirmDialog="handleAddConfirm">
<div class="qrcon">
<div class="wxcon">
<div class="codeDiv"
id="wxview">
<img style="width:100%;height:100%"
src="../../images/code3.png">
<!-- v-if="qrCodeURL" <div class="imgDiv"> -->
<img v-if="qrCodeURL"
class="qrImg"
crossorigin="anonymous"
:src="qrCodeURL">
<!-- crossorigin="anonymous" -->
<div v-if="!qrCodeURL"
class="el-icon-loading loadingIcon" />
<!-- </div> -->
<div class="merchantNameDiv">{{ info.merchantName }}</div>
</div>
<el-button style="marginTop:20px"
type="primary "
@click="takeScreenshot('wxview', `${info.merchantName}微信支付收款码`)">下载</el-button>
</div>
<div class="alicon">
<div class="codeDiv"
id="aliview">
<img style="width:100%;height:100%"
src="../../images/alipay.png">
<!-- <img v-if="aliqrCodeURL"
class="qrImg imgs"
:src="aliqrCodeURL"
crossorigin="anonymous"> -->
<div id="qrcodeDiv"
class="qrImg" />
<!-- -->
<!-- :src="aliqrCodeURL"> -->
<div v-if="!aliqrCodeURL"
class="el-icon-loading loadingIcon" />
<!-- </div> -->
<div class="merchantNameDiv">{{ info.merchantName }}</div>
</div>
<el-button style="marginTop:20px"
type="primary "
@click="takeScreenshot('aliview', `${info.merchantName}支付宝支付收款码`)">下载
</el-button>
</div>
</div>
</d2-dialog>
</template>
<script>
// alipayQrcode
import { createWeappQrcode } from '@api/serviceManage'
import { baseConfig } from '@/main'
import html2canvas from 'html2canvas'
import QRCode from "qrcodejs2"
export default {
name: 'QRcodeDialog',
data() {
return {
infoForm: {
merchantType: '',
merchant: [],
payStartDate: '',
payEndDate: '',
reportType: '1'
},
// 基本数据
baseData: {
merchantList: []
},
qrCodeURL: '',
aliqrCodeURL: '',
defaultImg: require('../../images/defaultImg.png')
}
},
props: {
qRcodeVisible: {
type: Boolean,
default: false
},
info: {
type: Object,
default: null
}
},
watch: {
},
created() {
},
components: {},
mounted() {
console.log(this.info)
this.createQrcodeImage()
this.alipayQrcode()
},
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.scene = `merchantId=${this.info.yqMerchantId}&merchantName=${this.info.yqMerchantName}`
oParams.scene = `shopMerchantId=${this.info.merchantId}&shopMerchantName=${this.info.merchantName}`
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(eleId, name) {
// const opts = {
// useCORS: true
// }
const that = this
html2canvas(document.getElementById(eleId), {
backgroundColor: null,
allowTaint: true, // 允许污染
taintTest: true, // 在渲染前测试图片(没整明白有啥用)
useCORS: true // 使用跨域(当allowTaint为true时这段代码没什么用,下面解释)
}).then(canvas => {
that.convertCanvasToImg(canvas, name)
// setTimeout(() => {
// })
})
},
// 转换图片格式
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)
},
// 生成支付宝中间二维码
alipayQrcode() {
// this
// const that = this
// let shareUrl = `https://www.baidu.com/`;
const shareUrl = `${baseConfig.alipayPath}?shopMerchantId=${this.info.merchantId}&shopMerchantName=${this.info.merchantName}`
console.log(baseConfig.alipayPath)
console.log(shareUrl)
setTimeout(() => {
console.log(document.getElementById('qrcodeDiv'))
const qrcode = new QRCode("qrcodeDiv", {
text: shareUrl,
width: 150, // 图像宽度
height: 150 // 图像高度
})
qrcode.clear() // 清除二维码
qrcode.makeCode(shareUrl) // 生成另一个新的二维码
this.aliqrCodeURL = qrcode
}, 500)
// this.$nextTick(() => {
// const qrcode = new QRCode('qrcodeDiv', {
// text,
// width: 150,
// height: 150,
// colorDark: '#000000',
// colorLight: '#ffffff',
// correctLevel: QRCode.CorrectLevel.H
// })
// console.log(qrcode)
// }, 2000)
// const params = {
// path: baseConfig.alipayPath,
// yqMerchantId: this.info.yqMerchantId,
// yqMerchantName: this.info.yqMerchantName
// }
// alipayQrcode(params).then((result) => {
// console.log(result)
// this.aliqrCodeURL = result
// this.$nextTick(() => {
// that.combinedPoster(result)
// // that.getBase64Image(result, document.querySelector('.imgs'))
// })
// }).catch((err) => {
// console.log(err)
// })
},
handleAddConfirm() {
this.takeScreenshot('wxview', `${this.info.merchantName}微信支付收款码`)
setTimeout(() => {
this.takeScreenshot('aliview', `${this.info.merchantName}支付宝支付收款码`)
}, 2000)
// this.takeScreenshot()
},
handleClose() {
this.$emit('dialogClose', true)
}
}
}
</script>
<style scoped>
.qrcon {
display: flex;
}
.wxcon,
.alicon {
text-align: center;
}
.wxcon {
margin-right: 20px;
}
.codeDiv {
width: 345px;
height: 468px;
margin: 0px auto;
position: relative;
/* background-image: url('./images/code3.png'); */
}
.qrImg {
position: absolute;
width: 150px;
height: 150px;
left: 50%;
top: 42%;
/* transform: translateX(-50%); */
transform: translate(-50%, -50%);
/* background-color: #d8d8d8; */
z-index: 2;
object-fit: contain;
/* transform: scale(0.95); */
}
.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;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-box-orient: vertical;
/* 自定义行数 */
-webkit-line-clamp: 2;
}
</style>