chore: 渲染器定时查询 wujieProps 确认是否修改了入参
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
<script setup lang="ts">
|
||||
import type { AxiosInterceptorManager, AxiosResponse } from 'axios';
|
||||
|
||||
import { getCurrentInstance, onMounted, ref, watch } from 'vue';
|
||||
import {
|
||||
getCurrentInstance,
|
||||
onBeforeUnmount,
|
||||
onMounted,
|
||||
ref,
|
||||
watch,
|
||||
} from 'vue';
|
||||
|
||||
import { useQuery } from '@tanstack/vue-query';
|
||||
import { jsonp, request } from '@vtj/utils';
|
||||
@@ -237,6 +243,97 @@ const notifyParent = (event: string, data?: any) => {
|
||||
}
|
||||
};
|
||||
|
||||
// 添加一个函数来检测 wujie props 的变化并更新 initParams
|
||||
const setupWujiePropsWatcher = () => {
|
||||
if (!isWujieSubApp) return;
|
||||
|
||||
// 获取当前 props 的快照
|
||||
let prevPropsSnapshot = window.$wujie?.props
|
||||
? JSON.stringify(window.$wujie.props)
|
||||
: null;
|
||||
|
||||
// 设置定时器定期检查 props 变化
|
||||
const intervalId = setInterval(() => {
|
||||
if (!window.$wujie?.props) return;
|
||||
|
||||
// 获取最新的 props 并序列化
|
||||
const currentPropsSnapshot = JSON.stringify(window.$wujie.props);
|
||||
|
||||
// 比较是否发生变化
|
||||
if (currentPropsSnapshot !== prevPropsSnapshot) {
|
||||
console.log('检测到 wujie props 变化:', window.$wujie.props);
|
||||
|
||||
// 更新快照
|
||||
prevPropsSnapshot = currentPropsSnapshot;
|
||||
|
||||
// 更新 initParams
|
||||
const newParams = window.$wujie.props as WujieProps;
|
||||
const oldParams = initParams.value;
|
||||
|
||||
// 更新参数
|
||||
initParams.value = newParams;
|
||||
|
||||
// 如果核心参数发生变化,重新加载组件
|
||||
if (
|
||||
isValidParams(newParams) &&
|
||||
(oldParams?.fileId !== newParams.fileId ||
|
||||
oldParams?.projectId !== newParams.projectId)
|
||||
) {
|
||||
console.log('核心参数发生变化,重新加载组件');
|
||||
reloadComponent();
|
||||
}
|
||||
}
|
||||
}, 300); // 每300ms检查一次,避免过于频繁
|
||||
|
||||
// 组件卸载时清除定时器
|
||||
onBeforeUnmount(() => {
|
||||
clearInterval(intervalId);
|
||||
});
|
||||
};
|
||||
|
||||
// 重新加载组件的函数
|
||||
const reloadComponent = async () => {
|
||||
if (!isValidParams(initParams.value)) {
|
||||
errorMessage.value = '缺少必要参数:fileId 和 projectId';
|
||||
if (isWujieSubApp) {
|
||||
notifyParent('render-fail', errorMessage.value);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
isLoading.value = true;
|
||||
showLoading('参数已更新,重新加载组件...');
|
||||
|
||||
try {
|
||||
// 如果需要,重置 provider 以强制重新初始化
|
||||
provider.value = null;
|
||||
|
||||
// 重新初始化低代码引擎
|
||||
await initLowCodeEngine();
|
||||
|
||||
// 获取新的渲染组件
|
||||
const component = await getRenderComponent();
|
||||
if (component) {
|
||||
renderer.value = component;
|
||||
if (isWujieSubApp) {
|
||||
notifyParent('render-success');
|
||||
}
|
||||
} else {
|
||||
if (isWujieSubApp) {
|
||||
notifyParent('render-fail', 'Failed to get component');
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('重新加载过程出错:', error);
|
||||
if (isWujieSubApp) {
|
||||
notifyParent('render-fail', error);
|
||||
}
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
hideLoading();
|
||||
}
|
||||
};
|
||||
|
||||
// 组件挂载后执行初始化
|
||||
onMounted(async () => {
|
||||
// 通知父应用已准备就绪(如果是wujie子应用)
|
||||
@@ -244,7 +341,7 @@ onMounted(async () => {
|
||||
notifyParent('ready', 'y-code-renderer is ready');
|
||||
}
|
||||
|
||||
// 获取初始化参数 - 在挂载时执行一次
|
||||
// 获取初始化参数
|
||||
initParams.value = getInitParams();
|
||||
const paramsValid = isValidParams(initParams.value);
|
||||
|
||||
@@ -264,6 +361,9 @@ onMounted(async () => {
|
||||
renderer.value = component;
|
||||
if (isWujieSubApp) {
|
||||
notifyParent('render-success');
|
||||
|
||||
// 设置 wujie props 监听器
|
||||
setupWujiePropsWatcher();
|
||||
}
|
||||
} else {
|
||||
if (isWujieSubApp) {
|
||||
|
||||
Reference in New Issue
Block a user