<template>
    <div class="ocr-all" v-loading.fullscreen.lock="fullscreenLoading" element-loading-text="识别中">
        <div style="color: #606266;font-size: 15px;font-weight: 600;">PS:选择图片后，点击要需要识别类型！</div>
        <div class="ocr-box">
            <el-upload
            action="#"
            list-type="picture-card"
            :on-change="handleChangePicture"
            :auto-upload="false">
                <i slot="default" class="el-icon-plus"></i>
                <div slot="file" slot-scope="{file}">
                <img
                    class="el-upload-list__item-thumbnail"
                    :src="file.url" alt=""
                >
                <span class="el-upload-list__item-actions">
                    <span
                    class="el-upload-list__item-preview"
                    @click="handlePictureCardPreview(file)"
                    >
                        <i class="el-icon-zoom-in"></i>
                    </span>
                    <!-- <span
                    v-if="!disabled"
                    class="el-upload-list__item-delete"
                    @click="handleDownload(file)"
                    >
                    <i class="el-icon-download"></i>
                    </span>
                    <span
                    v-if="!disabled"
                    class="el-upload-list__item-delete"
                    @click="handleRemove(file)"
                    >
                    <i class="el-icon-delete"></i>
                    </span> -->
                </span>
                </div>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="">
            </el-dialog>
        </div>
        <div style="color: #606266;font-size: 15px;font-weight: 600;text-align: center;">上传图片只能是 JPG,PNG,BMP 格式!大小不能超过 7MB!</div>
        <el-row>
            <el-button type="primary" @click="license(1)">营业执照</el-button>
            <el-button type="primary" @click="creditCode(1)">统一信用代码</el-button>
            <el-button type="primary" @click="IDcardR(1)">身份证-人像面</el-button>
            <el-button type="primary" @click="IDcardG(1)">身份证-国徽面</el-button>
            <el-button type="primary" @click="driving(1)">行驶证</el-button>
            <el-button type="primary" @click="household(1)">户口本</el-button>
            <el-button type="primary" @click="deed(1)">房产证</el-button>
            <el-button type="primary" @click="Marriage(1)">结婚证</el-button>
            <el-button type="primary" @click="mainlandPassport(1)">大陆护照</el-button>
            <el-button type="primary" @click="mainlandPassports(1)">护照</el-button>
            <el-button type="primary" @click="universal(1)">通用印刷体</el-button>
            <el-button type="primary" @click="license(2)">营业执照(英文)</el-button>
            <el-button type="primary" @click="creditCode(2)">统一信用代码(英文)</el-button>
            <el-button type="primary" @click="IDcardR(2)">身份证-人像面(英文)</el-button>
            <el-button type="primary" @click="IDcardG(2)">身份证-国徽面(英文)</el-button>
            <el-button type="primary" @click="driving(2)">行驶证(英文)</el-button>
            <el-button type="primary" @click="household(2)">户口本(英文)</el-button>
            <el-button type="primary" @click="deed(2)">房产证(英文)</el-button>
            <el-button type="primary" @click="Marriage(2)">结婚证(英文)</el-button>
            <el-button type="primary" @click="mainlandPassport(2)">大陆护照(英文)</el-button>
            <el-button type="primary" @click="mainlandPassports(2)">护照(英文)</el-button>
            <el-button type="primary" @click="universal(2)">通用印刷体(英文)</el-button>
            <div style="width:160px"></div>
            <div style="width:160px"></div>
        </el-row>
    </div>
</template>

<script>
import { el } from '@fullcalendar/core/internal-common';
export default {
    data() {
    return {
        token:'',
        userid:'',
        dialogImageUrl: '',
        dialogVisible: false,
        disabled: false,
        panduan:"",
        picBase64:'',
        fullscreenLoading:false,
  	}
	},
    methods: {
        // handleRemove(file,) {
        //     console.log(file);
        //     file.status='cancel'
        // },
        handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url;
            this.dialogVisible = true;
        },
        // handleDownload(file) {
        //     console.log(file);
        // },
        handleChangePicture(file, fileList) {
            this.beforeAvatarUpload(file)
            if (fileList.length > 1) {
                    fileList.splice(0, 1);
                }
            if(this.panduan){
                this.getBase64(file.raw).then(res => {
                    this.picBase64=res
                    console.log(res)
                });
            }
        },
        beforeAvatarUpload(file) {
            this.panduan=true;
            const isJPG = file.raw.type === 'image/jpeg';
            const isPNG = file.raw.type === 'image/png';
            const isWEBP = file.raw.type === 'image/webp';
            const isLt2M = file.size / 1024 / 1024 < 7;
            if (!isJPG&&!isPNG&&!isWEBP) {
                this.$message.error('上传图片只能是 JPG,PNG,BMP 格式!');
                this.panduan=false;
            }
            if (!isLt2M) {
                this.$message.error('上传图片大小不能超过 7MB!');
                this.panduan=false;
            }
            return isJPG && isLt2M;
        },
        getBase64(file) {
            return new Promise(function (resolve, reject) {
                const reader = new FileReader()
                let imgResult = ''
                reader.readAsDataURL(file)
                reader.onload = function () {
                    imgResult = reader.result
                }
                reader.onerror = function (error) {
                    reject(error)
                }
                reader.onloadend = function () {
                    resolve(imgResult)
                }
            })
        },
        //身份证人像面
        IDcardR(val){
            this.fullscreenLoading = true;
            var url="/api/TencentOCR/GetIDCard"
            var that=this
            if(that.panduan){
                this.$axios({
                    method: 'post',
                    url:url,
                    headers:{
                        Authorization:'Bearer '+this.token
                    },
                    data:{
                        portType: 1,
                        userId:that.userid,
                        language:val,
                        picBase64:that.picBase64,
                        cardSide:0
                    }
                }).then(function(res){
                    if(res.data.code==200){       
                        console.log(res)
                        that.$message({
                            message: res.data.msg,
                            type: 'success'
                        });           
                        that.fullscreenLoading = false;
                        window.open(res.data.data.downUrl)
                    }else{
                        that.fullscreenLoading = false;
                        that.$message({
                            message: res.data.msg,
                            type: 'warning'
                        });       
                    }
                })
            }else{
                that.fullscreenLoading = false;
                this.$message.error('请检查图片是否为此类型!');
            }
        },
        //身份证国徽面
        IDcardG(val){
            this.fullscreenLoading = true;
            var url="/api/TencentOCR/GetIDCard"
            var that=this
            if(that.panduan){
                that.fullscreenLoading = true;
                this.$axios({
                    method: 'post',
                    url:url,
                    headers:{
                        Authorization:'Bearer '+this.token
                    },
                    data:{
                        portType: 1,
                        userId:that.userid,
                        language:val,
                        picBase64:that.picBase64,
                        cardSide:1
                    }
                }).then(function(res){
                    if(res.data.code==200){       
                        console.log(res)
                        that.$message({
                            message: res.data.msg,
                            type: 'success'
                        });   
                        that.fullscreenLoading = false;        
                        window.open(res.data.data.downUrl)
                    }else{
                        that.fullscreenLoading = false;
                        that.$message({
                            message: res.data.msg,
                            type: 'warning'
                        });       
                    }
                })
            }else{
                that.fullscreenLoading = false;
                this.$message.error('请检查图片是否为此类型!');
            }
        },
        //营业执照
        license(val){
            this.fullscreenLoading=true;
            var url="/api/TencentOCR/GetBizLicenseOCR"
            var that=this
            if(that.panduan){
                that.fullscreenLoading = true;
                this.$axios({
                    method: 'post',
                    url:url,
                    headers:{
                        Authorization:'Bearer '+this.token
                    },
                    data:{
                        language:val,
                        picBase64:that.picBase64,
                    }
                }).then(function(res){
                    if(res.data.code==200){       
                        that.$message({
                            message: res.data.msg,
                            type: 'success'
                        });           
                        that.fullscreenLoading=false;
                        window.open(res.data.data.url)
                    }else{
                        that.fullscreenLoading=false;
                        that.$message({
                            message: res.data.msg,
                            type: 'warning'
                        });       
                    }
                })
            }else{
                that.fullscreenLoading = false;
                this.$message.error('请检查图片是否为此类型!');
            }
        },
        //统一信用代码
        creditCode(val){
            this.fullscreenLoading = true;
            var url="/api/TencentOCR/GetOrgCodeCertOCR"
            var that=this
            if(that.panduan){
                that.fullscreenLoading = true;
                this.$axios({
                    method: 'post',
                    url:url,
                    headers:{
                        Authorization:'Bearer '+this.token
                    },
                    data:{
                        language:val,
                        picBase64:that.picBase64,
                    }
                }).then(function(res){
                    if(res.data.code==200){       
                        console.log(res)
                        that.$message({
                            message: res.data.msg,
                            type: 'success'
                        });         
                        that.fullscreenLoading = false;  
                        window.open(res.data.data.url)
                    }else{
                        that.fullscreenLoading = false;
                        that.$message({
                            message: res.data.msg,
                            type: 'warning'
                        });       
                    }
                })
            }else{
                that.fullscreenLoading = false;
                this.$message.error('请检查图片是否为此类型!');
            }
        },
        //行驶证
        driving(val){
            this.fullscreenLoading = true;
            var url="/api/TencentOCR/GetVehicleLicense"
            var that=this
            if(that.panduan){
                that.fullscreenLoading = true;
                this.$axios({
                    method: 'post',
                    url:url,
                    headers:{
                        Authorization:'Bearer '+this.token
                    },
                    data:{
                        language:val,
                        picBase64:that.picBase64,
                        cardSide:0
                    }
                }).then(function(res){
                    if(res.data.code==200){       
                        console.log(res)
                        that.$message({
                            message: res.data.msg,
                            type: 'success'
                        }); 
                        that.fullscreenLoading = false;          
                        window.open(res.data.data.url)
                    }else{
                        that.fullscreenLoading = false;
                        that.$message({
                            message: res.data.msg,
                            type: 'warning'
                        });       
                    }
                })
            }else{
                that.fullscreenLoading = false;
                this.$message.error('请检查图片是否为此类型!');
            }
        },
        //户口本
        household(val){
            this.fullscreenLoading = true;
            var url="/api/TencentOCR/GetResidenceBookletOCR"
            var that=this
            if(that.panduan){
                that.fullscreenLoading = true;
                this.$axios({
                    method: 'post',
                    url:url,
                    headers:{
                        Authorization:'Bearer '+this.token
                    },
                    data:{
                        language:val,
                        picBase64:that.picBase64,
                    }
                }).then(function(res){
                    if(res.data.code==200){       
                        console.log(res)
                        that.$message({
                            message: res.data.msg,
                            type: 'success'
                        });   
                        that.fullscreenLoading = false;        
                        window.open(res.data.data.url)
                    }else{
                        that.fullscreenLoading = false;
                        that.$message({
                            message: res.data.msg,
                            type: 'warning'
                        });       
                    }
                })
            }else{
                that.fullscreenLoading = false;
                this.$message.error('请检查图片是否为此类型!');
            }
        },
        //房产证
        deed(val){
            this.fullscreenLoading = true;
            var url="/api/TencentOCR/GetPropOwnerCert"
            var that=this
            if(that.panduan){
                that.fullscreenLoading = true;
                this.$axios({
                    method: 'post',
                    url:url,
                    headers:{
                        Authorization:'Bearer '+this.token
                    },
                    data:{
                        language:val,
                        picBase64:that.picBase64,
                    }
                }).then(function(res){
                    if(res.data.code==200){       
                        console.log(res)
                        that.$message({
                            message: res.data.msg,
                            type: 'success'
                        });  
                        that.fullscreenLoading = false;         
                        window.open(res.data.data.url)
                    }else{
                        that.fullscreenLoading = false;
                        that.$message({
                            message: res.data.msg,
                            type: 'warning'
                        });       
                    }
                })
            }else{
                that.fullscreenLoading = false;
                this.$message.error('请检查图片是否为此类型!');
            }
        },
        //结婚证
        Marriage(val){
            this.fullscreenLoading = true;
            var url="/api/TencentOCR/GetMarriageLicense"
            var that=this
            if(that.panduan){
                that.fullscreenLoading = true;
                this.$axios({
                    method: 'post',
                    url:url,
                    headers:{
                        Authorization:'Bearer '+this.token
                    },
                    data:{
                        language:val,
                        picBase64:that.picBase64,
                    }
                }).then(function(res){
                    if(res.data.code==200){       
                        console.log(res)
                        that.$message({
                            message: res.data.msg,
                            type: 'success'
                        });   
                        that.fullscreenLoading = false;        
                        window.open(res.data.data.url)
                    }else{
                        that.fullscreenLoading = false;
                        that.$message({
                            message: res.data.msg,
                            type: 'warning'
                        });       
                    }
                })
            }else{
                that.fullscreenLoading = false;
                that.$message.error('请检查图片是否为此类型!');
            }
        },
        //大陆护照
        mainlandPassport(val){
            this.fullscreenLoading = true;
            var url="/api/TencentOCR/GetPassport"
            var that=this
            if(that.panduan){
                that.fullscreenLoading = true;
                this.$axios({
                    method: 'post',
                    url:url,
                    headers:{
                        Authorization:'Bearer '+this.token
                    },
                    data:{
                        language:val,
                        picBase64:that.picBase64,
                    }
                }).then(function(res){
                    if(res.data.code==200){       
                        console.log(res)
                        that.$message({
                            message: res.data.msg,
                            type: 'success'
                        });     
                        that.fullscreenLoading = false;      
                        window.open(res.data.data.url)
                    }else{
                        that.fullscreenLoading = false;
                        that.$message({
                            message: res.data.msg,
                            type: 'warning'
                        });       
                    }
                })
            }else{
                that.fullscreenLoading = false;
                that.$message.error('请检查图片是否为此类型!');
            }
        },
        //护照
        mainlandPassports(val){
            this.fullscreenLoading = true;
            var url="/api/TencentOCR/GetMLIDPassport"
            var that=this
            if(that.panduan){
                that.fullscreenLoading = true;
                this.$axios({
                    method: 'post',
                    url:url,
                    headers:{
                        Authorization:'Bearer '+this.token
                    },
                    data:{
                        language:val,
                        picBase64:that.picBase64,
                    }
                }).then(function(res){
                    if(res.data.code==200){       
                        console.log(res)
                        that.$message({
                            message: res.data.msg,
                            type: 'success'
                        });     
                        that.fullscreenLoading = false;      
                        window.open(res.data.data.url)
                    }else{
                        that.fullscreenLoading = false;
                        that.$message({
                            message: res.data.msg,
                            type: 'warning'
                        });       
                    }
                })
            }else{
                that.fullscreenLoading = false;
                that.$message.error('请检查图片是否为此类型!');
            }
        },
        //通用印刷体
        universal(val){
            this.fullscreenLoading = true;
            var url="/api/TencentOCR/GetGeneralBasic"
            var that=this
            if(that.panduan){
                that.fullscreenLoading = true;
                this.$axios({
                    method: 'post',
                    url:url,
                    headers:{
                        Authorization:'Bearer '+this.token
                    },
                    data:{
                        language:val,
                        picBase64:that.picBase64,
                    }
                }).then(function(res){
                    if(res.data.code==200){       
                        console.log(res)
                        that.$message({
                            message: res.data.msg,
                            type: 'success'
                        });           
                        that.fullscreenLoading = false;
                        window.open(res.data.data.url)
                    }else{
                        that.fullscreenLoading = false;
                        that.$message({
                            message: res.data.msg,
                            type: 'warning'
                        });       
                    }
                })
            }else{
                that.fullscreenLoading = false;
                that.$message.error('请检查图片是否为此类型!');
            }
        },
    },
    mounted(){
        this.token=JSON.parse(localStorage.getItem('userinif')).token;
        this.userid=JSON.parse(localStorage.getItem('userinif')).userInfo.userId;
    }
}
</script>
<style>
    .ocr-all {
        background-color: #fff;
        padding: 10px;
        box-shadow: 0 0 5px #0005;
        border-radius: 10px;
    }
    .ocr-box .el-upload-list--picture-card .el-upload-list__item{
        display: inline-flex;
    }
    .ocr-box{
        display: flex;
        justify-content: center;
        margin-top: 180px;
    }
    .ocr-all .el-button--primary{
        width: 160px;
        margin: 10px 0px;
    }
    .ocr-all .el-button+.el-button{
        margin-left: 0px;
    }
    .ocr-all .el-row{
        width: 44%;
        margin: 0 auto;
        margin-top: 30px;
        margin-bottom: 180px;
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
    }
    .ocr-all .el-row::after, .ocr-all .el-row::before{
        content: none;
    }
</style>