account.vue 2.19 KB
<template>
  <div>
    <iframe
      id="myIframe"
      class="box"
      allowfullscreen
      :src="iframeSrc + iframeUrlWithParams"
    ></iframe>
  </div>
</template>

<script>
import { baseConfig } from "@/main"
import { mapState } from 'vuex'
export default {
  data() {
    return {
      // iframeSrc: 'https://oss-account-padtest.glsx.com.cn/customer/account',
      iframeSrc: baseConfig.accountUrl + '/customer/account',
      iframeUrl: '/index.html'
    };
  },
  computed: {
    iframeUrlWithParams() {
      // 检查是否已经有 t 参数
      if (this.iframeUrl.includes('?t=')) {
        // 如果已经有 t 参数,替换它
        return this.iframeUrl.replace(/(\?|&)t=[^&]+/, '$1t=' + this.SSOInfo.t);
      } else {
        // 如果没有 t 参数,添加它
        return this.iframeUrl.includes('?') 
          ? this.iframeUrl + '&t=' + this.SSOInfo.t 
          : this.iframeUrl + '?t=' + this.SSOInfo.t;
      }
    },
    ...mapState({
      SSOInfo: (state) => state.d2admin.user.SSOInfo,
    })
  },
  mounted() {
    // 监听来自 iframe 的消息
    window.addEventListener('message', (event) => {
      console.log('收到 iframe 消息:', event.data);
      if (event.data.type === 'location-change') {
        console.log('Iframe 地址已改变:', event.data.url);
        // 在这里可以执行其他操作,比如更新 iframe 的 src 属性
        this.handleIframeLocationChange(event.data.url);
      }
    });
  },
  methods: {
    handleIframeLocationChange(url) {
      const iframe = document.getElementById('myIframe');
      const internalUrl = url;
      let newInternalUrl = internalUrl;
        if (internalUrl.includes('?t=')) {
          newInternalUrl = internalUrl.replace(/(\?|&)t=[^&]+/, '$1t=' + this.SSOInfo.t);
        } else {
          newInternalUrl = internalUrl.includes('?') 
            ? internalUrl + '&t=' + this.SSOInfo.t 
            : internalUrl + '?t=' + this.SSOInfo.t;
        }
        console.log('新的内部地址:', newInternalUrl);
        this.iframeUrl = newInternalUrl
    }
  }
};
</script>

<style lang="scss" scoped>
.box {
  position: absolute;
  width: 97%;
  height: 96%;
  border: 0;
  margin: 20px;
  background-color: #fff;
}
</style>