feat: ApiSelect增加shouldFetch控制,在api请求之前的判断是否允许请求的回调函数 (#7713)
parent
2aced2f659
commit
47a853330d
|
|
@ -46,6 +46,8 @@ interface Props {
|
||||||
alwaysLoad?: boolean;
|
alwaysLoad?: boolean;
|
||||||
/** 在api请求之前的回调函数 */
|
/** 在api请求之前的回调函数 */
|
||||||
beforeFetch?: AnyPromiseFunction<any, any>;
|
beforeFetch?: AnyPromiseFunction<any, any>;
|
||||||
|
/** 在api请求之前的判断是否允许请求的回调函数 */
|
||||||
|
shouldFetch?: AnyPromiseFunction<any, boolean>;
|
||||||
/** 在api请求之后的回调函数 */
|
/** 在api请求之后的回调函数 */
|
||||||
afterFetch?: AnyPromiseFunction<any, any>;
|
afterFetch?: AnyPromiseFunction<any, any>;
|
||||||
/** 直接传入选项数据,也作为api返回空数据时的后备数据 */
|
/** 直接传入选项数据,也作为api返回空数据时的后备数据 */
|
||||||
|
|
@ -88,6 +90,7 @@ const props = withDefaults(defineProps<Props>(), {
|
||||||
alwaysLoad: false,
|
alwaysLoad: false,
|
||||||
loadingSlot: '',
|
loadingSlot: '',
|
||||||
beforeFetch: undefined,
|
beforeFetch: undefined,
|
||||||
|
shouldFetch: undefined,
|
||||||
afterFetch: undefined,
|
afterFetch: undefined,
|
||||||
modelPropName: 'modelValue',
|
modelPropName: 'modelValue',
|
||||||
api: undefined,
|
api: undefined,
|
||||||
|
|
@ -159,7 +162,7 @@ const bindProps = computed(() => {
|
||||||
});
|
});
|
||||||
|
|
||||||
async function fetchApi() {
|
async function fetchApi() {
|
||||||
const { api, beforeFetch, afterFetch, resultField } = props;
|
const { api, beforeFetch, shouldFetch, afterFetch, resultField } = props;
|
||||||
|
|
||||||
if (!api || !isFunction(api)) {
|
if (!api || !isFunction(api)) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -178,6 +181,14 @@ async function fetchApi() {
|
||||||
if (beforeFetch && isFunction(beforeFetch)) {
|
if (beforeFetch && isFunction(beforeFetch)) {
|
||||||
finalParams = (await beforeFetch(cloneDeep(finalParams))) || finalParams;
|
finalParams = (await beforeFetch(cloneDeep(finalParams))) || finalParams;
|
||||||
}
|
}
|
||||||
|
// 判断是否需要控制执行中断
|
||||||
|
if (
|
||||||
|
shouldFetch &&
|
||||||
|
isFunction(shouldFetch) &&
|
||||||
|
!(await shouldFetch(finalParams))
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
let res = await api(finalParams);
|
let res = await api(finalParams);
|
||||||
if (afterFetch && isFunction(afterFetch)) {
|
if (afterFetch && isFunction(afterFetch)) {
|
||||||
res = (await afterFetch(res)) || res;
|
res = (await afterFetch(res)) || res;
|
||||||
|
|
|
||||||
|
|
@ -113,6 +113,10 @@ const [BaseForm, baseFormApi] = useVbenForm({
|
||||||
params: {
|
params: {
|
||||||
keyword: keyword.value || undefined,
|
keyword: keyword.value || undefined,
|
||||||
},
|
},
|
||||||
|
// 远程搜索判断。当为true时,才允许调用api
|
||||||
|
shouldFetch: (params: any) => {
|
||||||
|
return !!params?.keyword;
|
||||||
|
},
|
||||||
showSearch: true,
|
showSearch: true,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
@ -120,6 +124,7 @@ const [BaseForm, baseFormApi] = useVbenForm({
|
||||||
fieldName: 'remoteSearch',
|
fieldName: 'remoteSearch',
|
||||||
// 界面显示的label
|
// 界面显示的label
|
||||||
label: '远程搜索',
|
label: '远程搜索',
|
||||||
|
help: '远程查询,仅有输入时方进行查询',
|
||||||
renderComponentContent: () => {
|
renderComponentContent: () => {
|
||||||
return {
|
return {
|
||||||
notFoundContent: fetching.value ? h(Spin) : undefined,
|
notFoundContent: fetching.value ? h(Spin) : undefined,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue