<template>
  <div id="menu-custom-linktype" v-loading="loading">
    <div class="link-wrap" v-if="openParams.linkType == '06' || openParams.linkType == '07'" v-loading="loading">
      <iframe
        ref="mainFrame"
        style="border:none;"
        width="100%"
        height="100%"
        scrolling="yes"
        frameborder="0"
        allowtransparency="true" 
        noresize="noresize"
        :src="openParams.actionContent"
        allow="microphone;camera;midi;encrypted-media;"
      ></iframe>
    </div>
    <div class="link-wrap" v-else-if="openParams.linkType == '05'" v-loading="loading">
      <iframe
        ref="mainFrame"
        style="border:none"
        width="100%"
        height="100%"
        scrolling="yes"
        frameborder="0"
        allowtransparency="true" 
        noresize="noresize"
        :src="'/obpm/portal/'+openParams.actionContent"
        allow="microphone;camera;midi;encrypted-media;"
      ></iframe>
    </div>
  </div>
</template>
<script>
export default {
  inject: ["reload"],
  props: [
    "openParams",
  ],
  components: {
    
  },
  data() {
    return {
      loading: true,
    };
  },
  created() {

    console.log(this.openParams,'openParams');

    // Function to check if the URL contains the specified domain and redirect
    function checkAndRedirect(v) {
        if (v.includes('ihr')) {
          return  window.location.href = v;
        }
    }
    
    checkAndRedirect(this.openParams.actionContent);
  },
  mounted() {//loading iframe--bcx

    const { mainFrame } = this.$refs;
        // IE和非IE浏览器，监听iframe加载事件不一样，需要兼容
        const that = this;
        if (mainFrame.attachEvent) {
            // IE
            mainFrame.attachEvent('onload', () => {
                that.stateChange();
            });
        } else {
            // 非IE
            mainFrame.onload = function () {
                that.stateChange();
            };
        }
  },
  methods: {
    stateChange() {
      let that=this
      this.loading = false;
      const { mainFrame } = this.$refs;
      if(mainFrame.contentDocument){//同源有contentDocument，监听iframe页面点击，鼠标，键盘事件
        mainFrame.contentDocument.onclick=function(){
          console.log('点击同源iframe onclick页面')
          that.$store.commit('SET_LASTTIME',new Date().getTime())//全局触发点击事件--获取最后一次时间
        }
        mainFrame.contentDocument.onkeyup=function(){
          console.log('点击同源iframe onkeyup页面')
          that.$store.commit('SET_LASTTIME',new Date().getTime())//全局触发点击事件--获取最后一次时间
        }
        mainFrame.contentDocument.onmousemove=function(){
          console.log('点击同源iframe onmousemove页面')
          that.$store.commit('SET_LASTTIME',new Date().getTime())//全局触发点击事件--获取最后一次时间
        }
        window.addEventListener('message',function(obj){//接收监听子元素iframe发过来的信息
           console.log('接收监听子元素iframe发过来的信息', obj)
           if(obj.data){
              let url=obj.data.url
              if(url&&url.indexOf('http')>-1){
                 that.$store.commit('SET_LASTTIME_Flag',false)//嵌套第三方iframe地址不锁屏，返回false
              }
           }
        })
      }else{
        that.$store.commit('SET_LASTTIME_Flag',false)//嵌套第三方iframe地址不锁屏，返回false
      }
    },
  },
};
</script>
<style lang="scss" scoped>

</style>


