test.vue
4.38 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
<template>
<div class="index">
<div id="captureId" class="capture" v-show="firstFlag">
<div class="back"></div>
<h2>{{this.textInfo}}</h2>
</div>
<img :src="dataURL" alt="" v-show="!firstFlag">
<div v-show="this.isCNLive.value" class="save_btn" @click="savecanvas">
点击按钮保存图片
</div>
<div v-show="!this.isCNLive.value" class="tip_text" >
长按页面保存证书到手机相册
</div>
</div>
</template>
<script>
import html2canvas from 'html2canvas';
export default {
data () {
return {
dataURL:'',
firstFlag:true,
imgUrl:"https://wjjtest.ys2.cnliveimg.com/shop/img/2019/12/31/5e0b06db22be5.jpg",
textInfo:'杜致阳签名'
};
},
mounted(){
html2canvas(document.querySelector('#captureId')).then(canvas => {
let imgUrl = canvas.toDataURL('image/png');
this.dataURL = imgUrl;
this.firstFlag = false;
})
},
methods: {
// 直接获取图片url传给原生
getimgUrl(imgData){
// 调用原生方法
// 调用分享(新方法)
if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.CNDSCheckAllowVisitPhotoAlbumHandler) {
// 获取URL图片
window.webkit.messageHandlers.CNDSCheckAllowVisitPhotoAlbumHandler.postMessage({body: {imgData:imgData}});
} else if (window.obj && window.obj.CNDSCheckAllowVisitPhotoAlbumHandler) {
// 获取URL图片
window.obj.CNDSCheckAllowVisitPhotoAlbumHandler(imgData);
}
},
savecanvas(){
let canvas = document.querySelector('.canvas');
let that = this;
html2canvas(canvas,{scale:2,logging:false,useCORS:true}).then(function(canvas) {
let type = 'png';
let imgData = canvas.toDataURL(type);
// 照片格式处理
let _fixType = function(type) {
type = type.toLowerCase().replace(/jpg/i, 'jpeg');
let r = type.match(/png|jpeg|bmp|gif/)[0];
return 'image/' + r;
};
// imgData = imgData.replace(_fixType(type),'');
let filename = "证书" + '.' + type;
if(that.isCNLive.value){
that.getimgUrl(imgData)
}else {
that.saveFile(imgData,filename);
}
});
},
saveFile(data, filename){
let save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
save_link.href = data;
save_link.download = filename;
console.log(save_link.download)
console.log(save_link.href)
let event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
save_link.dispatchEvent(event);
}
}
}
</script>
<style lang='less' scoped>
.index{
height: 100%;
width: 100%;
position: relative;
.canvas{
height: 100%;
background: #d5574a;
padding: 0.5067rem 0.6rem 0;
box-sizing: border-box;
img{
width: 100%;
}
}
.save_btn{
height: 100px;
line-height: 100px;
background: #333333;
text-align: center;
color: #ffffff;
font-size: 36px;
}
img{
width: 100%;
}
h2{
position: absolute;
top:20px;
right: 50px;
}
.back{
height: 860px;
background: url("../../static/img/index/1.jpg") no-repeat center top;
background-size: 100%;
}
.tip_text{
height: 100px;
line-height: 100px;
text-align: center;
color: #000;
font-size: 36px;
}
}
</style>