test.vue 4.38 KB
<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>