<template>
  <iframe ref="iframe" width="100%" height="100%" :src="src" />
</template>
<script>
// import router from '../../router'
import { oauth } from '@/api/user'
export default {
  data() {
    return {
      loading: true,
      src: ''
    }
  },
  mounted() {
    const a = this.$route.path.split('/')
    let path = ''
    path = '/' + a[1] + '/#'
    a.forEach((item, index) => {
      if (index > 1) {
        path = path + '/' + item
      }
      return path
    })
    if (path.startsWith('/aiceps-service-web')) {
      oauth().then((res) => {
        const { data } = res
        this.src = `${process.env.VUE_APP_AICEPS_SERVICE_URL || ''}/aiceps-service-web/#/oauthLogin?redirectUri=${encodeURIComponent(path.split('/#')[1])}&isIframe=1&encryptedData=${data}`
      })
    } else {
      this.src = path
    }

    // 3.添加监听子iframe事件
    window.addEventListener('message', this.iframeMessage, false)
  },
  destroyed() {
    window.removeEventListener('message', this.iframeMessage, false)
  },
  // watch: {
  //   '$route.path'() {
  //     this.goto()
  //   }
  // },
  // methods: {
  //   goto(a) {
  //     this.refs.iframe.contentWindow.postMessage({
  //       path: a
  //     }, '*')
  //   }
  // }
  methods: {
    iframeMessage(event) {
      const { data } = event
      if (typeof data === 'string' && !data.startsWith('webpack')) {
        // 这里获取data参数，就是子系统给过来的参数，你就可以进行判断跳转做操作了
        this.src = ''
        setTimeout(() => {
          this.src = data
        })
        console.log('子系统传来的' + data)
      }
    }
  }
}
</script>

