<template>
    <div style="height:95vh" class="iframess"  @contextmenu.prevent.capture>
        <div style="position: fixed;width: 10vw;height:5vh;background:#ffffff;right: 1vw;">
            <img src="./../../assets/pdfLogo.png" style="width:100%;height: 100%;">
        </div>
        <iframe id="mainFrame" src="/baiwen.pdf" style="z-index: -6;" width="100%" height="100%" frameborder="0" allowfullscreen></iframe>
    </div>
</template>
<script>
import pdf from 'vue-pdf'
export default {  
    name: 'Pdf',
    components: {
        pdf
    },
    data() {
        return {
            url:'/baiwen.pdf',
            pageNum:1,
            pageTotalNum: 101, // pdf 总页数

        }
    },
    created () {
        this.$nextTick(() => {
            document.addEventListener('contextmenu', event => event.preventDefault());
            // 禁用右键
            document.oncontextmenu = new Function('event.returnValue=false')
            // 禁用选择
            document.onselectstart = new Function('event.returnValue=false')
            //禁止f12
            document.οnkeydοwn = new Function('event.returnValue=false')
        })
        // 如禁止f12 代码没生效，加下面这段
        document.onkeydown = function (e) {
            if (e && e.keyCode === 123) {
                e.returnValue = false
                // e.keyCode = 0   //去掉也可以，如果要写，则需要setter + getter配合使用，不然则会报错
                return false
            }
        }
    },
    mounted() {
    //   let that = this;
    //   this.getNumPages()
    //   setTimeout(function(){
    //     document.addEventListener('contextmenu', event => event.preventDefault()); 
    //     that.prohibit();
    //   },2000)
  },
    methods:{
        prohibit () {
            alert('00000000000000000')
            document.getElementById('mainFrame').οnlοad=function(){
                document.addEventListener('contextmenu', event => event.preventDefault());
                iframe.contentWindow.document.οncοntextmenu=new Function('event.returnValue=false');
                iframe.contentWindow.document.onselectstart=new Function('event.returnValue=false')
            };
            document.getElementById('mainFrame').onload = function(){
                document.getElementById('mainFrame').oncontextmenu.addEventListener('')
                document.getElementById('mainFrame').oncontextmenu = function () {
                return false
            },
                window.document.getElementById('mainFrame').contentWindow.document.oncontextmenu  = function(){ 
                    alert('请不要点击鼠标右键！');
                    return false;
                    }  
            }
            document.getElementById('mainFrame').oncontextmenu = function () {
                return false
            },
            // console.log(document)
            document.oncontextmenu = function () {
                return false
            }
        },
        prePage() {
            let page = this.pageNum
            page = page > 1 ? page - 1 : this.pageTotalNum
            this.pageNum = page
        },
    
        // 下一页
        nextPage() {
            let page = this.pageNum
            page = page < this.pageTotalNum ? page + 1 : 1
            this.pageNum = page
        },
        getNumPages() {
            let loadingTask = pdf.createLoadingTask(this.url)
            loadingTask.promise.then(pdf => {
                this.pageTotalNum = pdf.numPages
            }).catch(err => {
                console.error('pdf 加载失败', err);
            })
        },
    }
}

    
</script>
<style>
.iframess{
    opacity: 0.99;
}
</style>