bonus-ui/src/views/iframe/MallIframe.vue

38 lines
971 B
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<iframe ref="mallFrame" :src="iframeSrc" style="width: 100%; height: 100vh; border: none"></iframe>
</template>
<script>
export default {
name: 'MallIframe',
data() {
return {
iframeSrc: '',
}
},
mounted() {
// 构造 iframe 地址使用相对路径自动携带Cookie
this.iframeSrc =
process.env.NODE_ENV === 'production'
? '/iws/mall-view/home?redirect=equipList'
: 'http://localhost:8102/iws/mall-view/home?redirect=equipList'
// 等 iframe 加载完成再传 token
this.$refs.mallFrame.onload = () => {
const token = localStorage.getItem('token') // A 项目的 token
console.log('A项目token:', token)
if (token) {
// 使用 postMessage 发送给 B
this.$refs.mallFrame.contentWindow.postMessage(
{
type: 'SET_TOKEN',
token: token,
},
window.location.origin
)
}
}
},
}
</script>