From 47a853330d843f800dd67225fc1494c2bd5a103d Mon Sep 17 00:00:00 2001 From: xueyitt <1455668754@qq.com> Date: Tue, 24 Mar 2026 10:22:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20ApiSelect=E5=A2=9E=E5=8A=A0shouldFetch?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=EF=BC=8C=E5=9C=A8api=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E4=B9=8B=E5=89=8D=E7=9A=84=E5=88=A4=E6=96=AD=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E5=85=81=E8=AE=B8=E8=AF=B7=E6=B1=82=E7=9A=84=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=E5=87=BD=E6=95=B0=20(#7713)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/api-component/api-component.vue | 13 ++++++++++++- playground/src/views/examples/form/basic.vue | 5 +++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/effects/common-ui/src/components/api-component/api-component.vue b/packages/effects/common-ui/src/components/api-component/api-component.vue index 19f51f526..682cd16b8 100644 --- a/packages/effects/common-ui/src/components/api-component/api-component.vue +++ b/packages/effects/common-ui/src/components/api-component/api-component.vue @@ -46,6 +46,8 @@ interface Props { alwaysLoad?: boolean; /** 在api请求之前的回调函数 */ beforeFetch?: AnyPromiseFunction; + /** 在api请求之前的判断是否允许请求的回调函数 */ + shouldFetch?: AnyPromiseFunction; /** 在api请求之后的回调函数 */ afterFetch?: AnyPromiseFunction; /** 直接传入选项数据,也作为api返回空数据时的后备数据 */ @@ -88,6 +90,7 @@ const props = withDefaults(defineProps(), { alwaysLoad: false, loadingSlot: '', beforeFetch: undefined, + shouldFetch: undefined, afterFetch: undefined, modelPropName: 'modelValue', api: undefined, @@ -159,7 +162,7 @@ const bindProps = computed(() => { }); async function fetchApi() { - const { api, beforeFetch, afterFetch, resultField } = props; + const { api, beforeFetch, shouldFetch, afterFetch, resultField } = props; if (!api || !isFunction(api)) { return; @@ -178,6 +181,14 @@ async function fetchApi() { if (beforeFetch && isFunction(beforeFetch)) { finalParams = (await beforeFetch(cloneDeep(finalParams))) || finalParams; } + // 判断是否需要控制执行中断 + if ( + shouldFetch && + isFunction(shouldFetch) && + !(await shouldFetch(finalParams)) + ) { + return; + } let res = await api(finalParams); if (afterFetch && isFunction(afterFetch)) { res = (await afterFetch(res)) || res; diff --git a/playground/src/views/examples/form/basic.vue b/playground/src/views/examples/form/basic.vue index fd96ff4d0..4474df943 100644 --- a/playground/src/views/examples/form/basic.vue +++ b/playground/src/views/examples/form/basic.vue @@ -113,6 +113,10 @@ const [BaseForm, baseFormApi] = useVbenForm({ params: { keyword: keyword.value || undefined, }, + // 远程搜索判断。当为true时,才允许调用api + shouldFetch: (params: any) => { + return !!params?.keyword; + }, showSearch: true, }; }, @@ -120,6 +124,7 @@ const [BaseForm, baseFormApi] = useVbenForm({ fieldName: 'remoteSearch', // 界面显示的label label: '远程搜索', + help: '远程查询,仅有输入时方进行查询', renderComponentContent: () => { return { notFoundContent: fetching.value ? h(Spin) : undefined,