<template>
  <div v-loading="loading" class="app-container">
    <iframe ref="companyIframe" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" width="100%" :height="iframeHight" :src="src" />
  </div>
</template>

<script>
export default {
  name: 'EnterpriseData',
  components: {

  },
  data() {
    return {
      loading: false, // 是否加载中
      companyId: null, // 企业ID
      iframeHight: window.innerHeight, // iframe高度
      scrollTop: 0, // 滚动条距离内部页面顶部距离
      token: this.$store.getters.token // 需要携带的token
    }
  },
  created() {
    if (this.$route.query.companyId) { // 获取companyId
      this.loading = true
      this.companyId = this.encodeStr(window.atob(this.$route.query.companyId))
      this.src = `https://pre-plug.jiansheku.com/enterprise/${this.companyId}?token=${this.token}`
    }
    // 示例传参：?companyId=window.btoa('12306')
  },
  mounted() {
    this.getInframeHight() // 实时控制iframe高度
    window.addEventListener('scroll', this.scrolling) // 监听页面滚动事件
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.scrolling) // 销毁页面滚动事件
  },
  methods: {
    getInframeHight() {
      const _this = this
      window.addEventListener('message', function(e) {
        const data = e.data
        const sc = document && document.documentElement.scrollTop || document && document.body.scrollTop
        if (data && typeof data === 'object') {
          // 动态设置iFrame高度
          if (data.height) {
            _this.iframeHight = data.height
            _this.loading = false
          }
          // 点击栏目名及子标签动态设置滚动高度
          if (data.scrollHeight) {
            window.scrollTo(sc, parseInt(data.scrollHeight) + 83)
          }
          // 点击下拉子标签动态设置滚动高度
          if (data.clientHeight) {
            window.scrollTo(sc, sc - 30)
          }
        }
      })
    },
    scrolling() {
      // 滚动条距文档顶部的距离
      const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      this.scrollTop = scrollTop
      // 准备传值
      this.$nextTick(() => {
        const ifa = this.$refs.companyIframe
        ifa.contentWindow.postMessage({ 'scrollTop': this.scrollTop }, '*')
      })
    },
    // companyId加密
    encodeStr(str, secondary) {
      const table = 'VyB2Kz79QWYjpiD5lRCIMwJEhqFSx0GN1cveZfU4gs6rk8dPbLtAomOnT3'
      const ss = [2, 7, 5, 1, 4, 8, 3, 0, 6]
      const xor = 177451812
      const add = 8728348608
      const mp2 = new Map()
      let content = secondary ? str : '         '
      let result = '' // 最终加密id
      if (!secondary) {
        let s = parseInt(str)
        s = (s ^ xor) + add
        for (let i = 0; i < table.length; i++) {
          const s1 = table.substring(i, i + 1)
          mp2.set(i, s1)
        }
        for (let i = 0; i < 9; i++) {
          const r = mp2.get(parseInt(s / this.power(58, i) % 58))
          content = this.changeStr(content, ss[i], r)
        }
      }
      // 二次加密
      const idArr = content.split('')
      for (var i = 0; i < idArr.length; i++) {
        const hex = idArr[i].charCodeAt().toString(16)
        result = result + hex
      }
      return result
    },
    power(a, b) {
      let power = 1
      for (let c = 0; c < b; c++) {
        power *= a
      }
      return power
    },
    changeStr(str, index, changeStr) {
      return str.substr(0, index) + changeStr + str.substr(index + changeStr.length)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
