chore: 渲染器定时查询 wujieProps 确认是否修改了入参

This commit is contained in:
wangxuefeng
2025-03-31 10:43:18 +08:00
parent cf1fab8c58
commit 99b207ec59
3 changed files with 129 additions and 12 deletions

View File

@@ -1,7 +1,13 @@
<script setup lang="ts"> <script setup lang="ts">
import type { AxiosInterceptorManager, AxiosResponse } from 'axios'; 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 { useQuery } from '@tanstack/vue-query';
import { jsonp, request } from '@vtj/utils'; 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 () => { onMounted(async () => {
// 通知父应用已准备就绪如果是wujie子应用 // 通知父应用已准备就绪如果是wujie子应用
@@ -244,7 +341,7 @@ onMounted(async () => {
notifyParent('ready', 'y-code-renderer is ready'); notifyParent('ready', 'y-code-renderer is ready');
} }
// 获取初始化参数 - 在挂载时执行一次 // 获取初始化参数
initParams.value = getInitParams(); initParams.value = getInitParams();
const paramsValid = isValidParams(initParams.value); const paramsValid = isValidParams(initParams.value);
@@ -264,6 +361,9 @@ onMounted(async () => {
renderer.value = component; renderer.value = component;
if (isWujieSubApp) { if (isWujieSubApp) {
notifyParent('render-success'); notifyParent('render-success');
// 设置 wujie props 监听器
setupWujiePropsWatcher();
} }
} else { } else {
if (isWujieSubApp) { if (isWujieSubApp) {

View File

@@ -1,6 +1,6 @@
{ {
"name": "@sy/wujie-vue3-renderer-adapter", "name": "@sy/wujie-vue3-renderer-adapter",
"version": "1.0.0", "version": "1.0.1",
"description": "wujie-vue3 ycode renderer adapter", "description": "wujie-vue3 ycode renderer adapter",
"type": "module", "type": "module",
"scripts": { "scripts": {

View File

@@ -3,7 +3,7 @@ import type { AxiosInterceptorManager } from 'axios';
import type { Router, RouteRecordRaw } from 'vue-router'; import type { Router, RouteRecordRaw } from 'vue-router';
import { computed, onBeforeUnmount, onMounted } from 'vue'; import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue';
import WujieVue from 'wujie-vue3'; import WujieVue from 'wujie-vue3';
@@ -26,6 +26,7 @@ const props = withDefaults(
projectId: number; projectId: number;
route?: RouteRecordRaw; route?: RouteRecordRaw;
router?: Router; router?: Router;
searchParams?: Record<string, any>;
sync?: boolean; sync?: boolean;
url?: string; url?: string;
}>(), }>(),
@@ -41,6 +42,7 @@ const props = withDefaults(
route: undefined, route: undefined,
// 默认不使用 router // 默认不使用 router
router: undefined, router: undefined,
searchParams: () => ({}),
sync: true, sync: true,
url: 'https://y-code-renderer.shiyue.com', url: 'https://y-code-renderer.shiyue.com',
}, },
@@ -62,6 +64,29 @@ const { bus } = WujieVue;
// // InstanceofPlugin(), // // InstanceofPlugin(),
// ]; // ];
// 使用 ref 创建响应式的 subAppProps
const subAppProps = ref({
...props,
adapterInfo: {
version,
},
});
// 监听 props 的所有变化
watch(
() => ({ ...props }),
(newProps) => {
console.log('props 发生变化', newProps);
subAppProps.value = {
...newProps,
adapterInfo: {
version,
},
};
},
{ deep: true },
);
// 生命周期钩子 // 生命周期钩子
const beforeLoad = (appWindow: Window) => { const beforeLoad = (appWindow: Window) => {
console.log(`${props.name} 开始加载`, appWindow); console.log(`${props.name} 开始加载`, appWindow);
@@ -76,14 +101,6 @@ const handleMessage = (data: any) => {
console.log(`${props.name} 收到子应用消息:`, data); console.log(`${props.name} 收到子应用消息:`, data);
}; };
// 准备传递给子应用的数据
const subAppProps = {
...props,
adapterInfo: {
version,
},
};
const finalUrl = computed(() => { const finalUrl = computed(() => {
return `${props.url}?fileId=${props.fileId}&projectId=${props.projectId}&applicationId=${props.applicationId}`; return `${props.url}?fileId=${props.fileId}&projectId=${props.projectId}&applicationId=${props.applicationId}`;
}); });