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">
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) {

View File

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

View File

@ -3,7 +3,7 @@ import type { AxiosInterceptorManager } from 'axios';
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';
@ -26,6 +26,7 @@ const props = withDefaults(
projectId: number;
route?: RouteRecordRaw;
router?: Router;
searchParams?: Record<string, any>;
sync?: boolean;
url?: string;
}>(),
@ -41,6 +42,7 @@ const props = withDefaults(
route: undefined,
// 使 router
router: undefined,
searchParams: () => ({}),
sync: true,
url: 'https://y-code-renderer.shiyue.com',
},
@ -62,6 +64,29 @@ const { bus } = WujieVue;
// // 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) => {
console.log(`${props.name} 开始加载`, appWindow);
@ -76,14 +101,6 @@ const handleMessage = (data: any) => {
console.log(`${props.name} 收到子应用消息:`, data);
};
//
const subAppProps = {
...props,
adapterInfo: {
version,
},
};
const finalUrl = computed(() => {
return `${props.url}?fileId=${props.fileId}&projectId=${props.projectId}&applicationId=${props.applicationId}`;
});