diff --git a/packages/form/src/fields/Select.vue b/packages/form/src/fields/Select.vue index 292c91a1..74df232a 100644 --- a/packages/form/src/fields/Select.vue +++ b/packages/form/src/fields/Select.vue @@ -99,11 +99,16 @@ const getOptions = async () => { const { config } = props; const { option } = config; - let { body } = option; + const { root = '', totalKey = 'total' } = option; + let { body = {}, url } = option; + + if (typeof url === 'function') { + url = await url(mForm, { model: props.model, formValue: mForm?.values }); + } let postOptions: Record = { method: option.method || 'POST', - url: option.url, + url, cache: option.cache, timeout: option.timeout, mode: option.mode, @@ -111,22 +116,21 @@ const getOptions = async () => { json: option.json || false, }; - if (body) { - if (typeof body === 'function') { - body = body(mForm, { - model: props.model, - formValue: mForm?.values, - formValues: mForm?.values, - config: props.config, - }) as Record; - } - - body.query = query.value; - body.pgSize = pgSize.value; - body.pgIndex = pgIndex.value; - postOptions.data = body; + if (typeof body === 'function') { + body = body(mForm, { + model: props.model, + formValue: mForm?.values, + formValues: mForm?.values, + config: props.config, + }) as Record; } + body.query = query.value; + body.pgSize = pgSize.value; + body.pgIndex = pgIndex.value; + + postOptions.data = body; + const requestFuc = getConfig('request') as Function; if (typeof option.beforeRequest === 'function') { @@ -151,10 +155,14 @@ const getOptions = async () => { }); } - const optionsData = res[option.root]; + const optionsData = root.split('.').reduce((accumulator, currentValue: any) => accumulator[currentValue], res); - if (res.total > 0) { - total.value = res.total; + const resTotal = globalThis.parseInt( + totalKey.split('.').reduce((accumulator, currentValue: any) => accumulator[currentValue], res), + 10, + ); + if (resTotal > 0) { + total.value = resTotal; } remoteData.value = remoteData.value.concat(optionsData); @@ -214,6 +222,8 @@ const getInitOption = async () => { const { config } = props; const { option } = config; + const { root = '', initRoot = '' } = option; + let { initBody = {} } = option; let options: SelectOption[] | SelectGroupOption[] = []; @@ -226,24 +236,53 @@ const getInitOption = async () => { url = await url(mForm, { model: props.model, formValue: mForm?.values }); } - const postOptions: Record = { + if (typeof initBody === 'function') { + initBody = initBody(mForm, { + model: props.model, + formValue: mForm?.values, + formValues: mForm?.values, + config: props.config, + }) as Record; + } + + let postOptions: Record = { method: option.method || 'POST', url, data: { id: props.model[props.name], + ...initBody, }, mode: option.mode, headers: option.headers || {}, json: option.json || false, }; + + if (typeof option.beforeInitRequest === 'function') { + postOptions = option.beforeInitRequest(mForm, postOptions, { + model: props.model, + formValue: mForm?.values, + }); + } + if (option.method?.toLocaleLowerCase() === 'jsonp') { postOptions.jsonpCallback = option.jsonpCallback || 'callback'; } const requestFuc = getConfig('request') as Function; - const res = await requestFuc(postOptions); + let res = await requestFuc(postOptions); - let initData = res[option.root]; + if (typeof option.afterRequest === 'function') { + res = option.afterRequest(mForm, res, { + model: props.model, + formValue: mForm?.values, + formValues: mForm?.values, + config: props.config, + }); + } + + let initData = (initRoot || root) + .split('.') + .reduce((accumulator, currentValue: any) => accumulator[currentValue], res); if (initData) { if (!Array.isArray(initData)) { initData = [initData]; diff --git a/packages/form/src/schema.ts b/packages/form/src/schema.ts index 4085731b..2ee28259 100644 --- a/packages/form/src/schema.ts +++ b/packages/form/src/schema.ts @@ -433,7 +433,7 @@ export interface SelectConfig extends FormItem, Input { options: SelectConfigOption[] | SelectConfigGroupOption[] | SelectOptionFunction; remote: true; option: { - url: string; + url: string | ((mForm: FormState | undefined, data: { model: any; formValue: any }) => string); initUrl?: string | ((mForm: FormState | undefined, data: { model: any; formValue: any }) => string); method?: 'jsonp' | string; cache?: boolean; @@ -444,13 +444,22 @@ export interface SelectConfig extends FormItem, Input { }; json?: false | boolean; body?: Record | RemoteSelectOptionBodyFunction; + initBody?: Record | RemoteSelectOptionBodyFunction; jsonpCallback?: 'callback' | string; afterRequest?: RemoteSelectOptionRequestFunction; + afterInitRequest?: RemoteSelectOptionRequestFunction; beforeRequest?: (mForm: FormState | undefined, postOptions: Record, data: any) => Record; - root: string; + beforeInitRequest?: ( + mForm: FormState | undefined, + postOptions: Record, + data: any, + ) => Record; + root?: string; + totalKey?: string; + initRoot?: string; item?: RemoteSelectOptionItemFunction; - value: string | SelectOptionValueFunction; - text: string | SelectOptionTextFunction; + value?: string | SelectOptionValueFunction; + text?: string | SelectOptionTextFunction; }; }