|
|
@@ -387,47 +387,95 @@ export default {
|
|
|
// this.showObserveDialog = true;
|
|
|
|
|
|
const url = `https://observe.cocorobo.cn/#/newClassroom?userid=${this.userid}&oid=${this.oid}&org=${this.org}&pptid=${pptid}`;
|
|
|
+
|
|
|
const _pageWindow = window.open(url, '_blank');
|
|
|
|
|
|
- // 优化:仅通过轮询方式检查窗口是否加载完成并发送消息,被打开页面无需做任何处理
|
|
|
- const sendFileData = () => {
|
|
|
- if (_pageWindow && !_pageWindow.closed) {
|
|
|
- _pageWindow.postMessage(
|
|
|
- {
|
|
|
- type: 'fileData',
|
|
|
- file,
|
|
|
- },
|
|
|
- '*'
|
|
|
- );
|
|
|
- }
|
|
|
+ // if(!_pageWindow){
|
|
|
+ // alert('浏览器弹窗被拦截,请允许弹窗后重试');
|
|
|
+ // return;
|
|
|
+ // }
|
|
|
+ const sendData = {
|
|
|
+ type: 'fileData',
|
|
|
+ file,
|
|
|
+ }
|
|
|
+ const sendMessageToVue3 = () => {
|
|
|
+ _pageWindow.postMessage(
|
|
|
+ sendData,
|
|
|
+ '*' // targetOrigin:必须是Vue3的实际域名,不要用*(安全)
|
|
|
+ );
|
|
|
};
|
|
|
|
|
|
- let checkCount = 0;
|
|
|
- const checkLoadedInterval = setInterval(() => {
|
|
|
- if (!_pageWindow || _pageWindow.closed) {
|
|
|
- clearInterval(checkLoadedInterval);
|
|
|
- return;
|
|
|
- }
|
|
|
- try {
|
|
|
- if (_pageWindow.document && _pageWindow.document.readyState === 'complete') {
|
|
|
- clearInterval(checkLoadedInterval);
|
|
|
- sendFileData();
|
|
|
- }
|
|
|
- } catch (err) {
|
|
|
- // 跨域下直接尝试发送
|
|
|
- clearInterval(checkLoadedInterval);
|
|
|
- sendFileData();
|
|
|
+ const handleVue3Ready = (e) => {
|
|
|
+ // 校验消息来源(安全:只处理Vue3域名的消息)
|
|
|
+
|
|
|
+ // 校验消息类型(确认是Vue3的就绪通知)
|
|
|
+ if (e.data.type === 'READY') {
|
|
|
+ console.log('已就绪,开始发送数据');
|
|
|
+ sendMessageToVue3();
|
|
|
}
|
|
|
- if (++checkCount > 20) {
|
|
|
- // 最多检测2秒(20次)
|
|
|
- clearInterval(checkLoadedInterval);
|
|
|
- sendFileData();
|
|
|
+
|
|
|
+ // 校验Vue3的"接收成功"确认(可选,进一步确保送达)
|
|
|
+ if (e.data.type === 'DATA_RECEIVED') {
|
|
|
+ console.log('数据已被接收');
|
|
|
+ // 收到确认后移除监听,避免内存泄漏
|
|
|
+ window.removeEventListener('message', handleVue3Ready);
|
|
|
}
|
|
|
- }, 100);
|
|
|
+ };
|
|
|
+ window.addEventListener('message', handleVue3Ready);
|
|
|
+ const timeoutTimer = setTimeout(() => {
|
|
|
+ console.log('未收到就绪通知,主动发送数据');
|
|
|
+ sendMessageToVue3();
|
|
|
+ // 移除监听(兜底后清理)
|
|
|
+ window.removeEventListener('message', handleVue3Ready);
|
|
|
+ }, 5000);
|
|
|
+
|
|
|
+ const checkWinClosed = setInterval(() => {
|
|
|
+ if (_pageWindow.closed) {
|
|
|
+ clearTimeout(timeoutTimer);
|
|
|
+ clearInterval(checkWinClosed);
|
|
|
+ window.removeEventListener('message', handleVue3Ready);
|
|
|
+ }
|
|
|
+ }, 1000);
|
|
|
+ // 优化:仅通过轮询方式检查窗口是否加载完成并发送消息,被打开页面无需做任何处理
|
|
|
+ // const sendFileData = () => {
|
|
|
+ // if (_pageWindow && !_pageWindow.closed) {
|
|
|
+ // _pageWindow.postMessage(
|
|
|
+ // {
|
|
|
+ // type: 'fileData',
|
|
|
+ // file,
|
|
|
+ // },
|
|
|
+ // '*'
|
|
|
+ // );
|
|
|
+ // }
|
|
|
+ // };
|
|
|
+
|
|
|
+ // let checkCount = 0;
|
|
|
+ // const checkLoadedInterval = setInterval(() => {
|
|
|
+ // if (!_pageWindow || _pageWindow.closed) {
|
|
|
+ // clearInterval(checkLoadedInterval);
|
|
|
+ // return;
|
|
|
+ // }
|
|
|
+ // try {
|
|
|
+ // if (_pageWindow.document && _pageWindow.document.readyState === 'complete') {
|
|
|
+ // clearInterval(checkLoadedInterval);
|
|
|
+ // sendFileData();
|
|
|
+ // }
|
|
|
+ // } catch (err) {
|
|
|
+ // // 跨域下直接尝试发送
|
|
|
+ // clearInterval(checkLoadedInterval);
|
|
|
+ // sendFileData();
|
|
|
+ // }
|
|
|
+ // if (++checkCount > 20) {
|
|
|
+ // // 最多检测2秒(20次)
|
|
|
+ // clearInterval(checkLoadedInterval);
|
|
|
+ // sendFileData();
|
|
|
+ // }
|
|
|
+ // }, 100);
|
|
|
|
|
|
setTimeout(()=>{
|
|
|
window.focus()
|
|
|
},100)
|
|
|
+
|
|
|
function openPageWindow(){
|
|
|
_pageWindow.focus()
|
|
|
}
|