@@ -41,10 +41,10 @@ interface Action extends ComponentDesc {
4141 reqConfig ?: any ;
4242 /** 隐藏默认按钮 */
4343 hidden ?: boolean ;
44- /** 请求前执行,可修改参数 */
45- before ?: ( params : Record < string , any > , ...args : any [ ] ) => Record < string , any > ;
46- /** 请求后执行,可修改结果 */
47- after ?: ( params : Record < string , any > ) => Record < string , any > ;
44+ /** 请求前执行,可修改请求参数,返回false可阻止请求发送 */
45+ before ?: ( params : Record < string , any > , ...args : any [ ] ) => Record < string , any > | boolean ;
46+ /** 请求后执行,可修改结果,返回false可阻止默认流程的执行 */
47+ after ?: ( params : Record < string , any > ) => Record < string , any > | boolean ;
4848 /** 打开modal前执行,可修改modal里表单的值 */
4949 open ?: ( params : Record < string , any > ) => Record < string , any > ;
5050}
@@ -125,13 +125,17 @@ export const useCurd = (config: CurdConfig) => {
125125 throw new Error ( `action.query.api required!` ) ;
126126 }
127127 const { api, after, before, reqConfig } = actions . query ;
128- let params = doBeforeQuery ( query , pager ) ;
129- if ( before ) {
130- params = before ( params ) ;
128+ let params : any = doBeforeQuery ( query , pager ) ;
129+ params = before ?.( params ) ;
130+ if ( ! params ) {
131+ getGlobalConfig ( ) . debug && console . warn ( 'actions.query.before返回false阻止了请求执行,如果是自定义请求流程,可忽略' ) ;
132+ return ;
131133 }
132134 let data = await doRequest ( api , params , reqConfig ) ;
133- if ( after ) {
134- data = after ( data ) ;
135+ data = after ?.( data ) ;
136+ if ( ! data ) {
137+ getGlobalConfig ( ) . debug && console . warn ( 'actions.query.after返回false阻止了默认逻辑数据绑定,请自行绑定数据!' ) ;
138+ return ;
135139 }
136140 table . modelRef . value = data . list ;
137141 if ( pagerCfg ) {
@@ -208,13 +212,14 @@ export const useCurd = (config: CurdConfig) => {
208212 }
209213 await doValidate ( edit ) ;
210214 let params = doGetFormData ( edit ) ;
211- if ( before ) {
212- params = before ( params ) ;
215+ params = before ?.( params ) ;
216+ if ( ! params ) {
217+ getGlobalConfig ( ) . debug && console . warn ( 'actions.[create/update].before返回false阻止了请求执行,如果是自定义请求流程,可忽略' ) ;
218+ return ;
213219 }
214- const data = await doRequest ( api , params , reqConfig ) ;
215- if ( after ) {
216- after ( data ) ;
217- } else {
220+ const data = ( await doRequest ( api , params , reqConfig ) ) || { } ;
221+ const res = after ?.( data ) ;
222+ if ( ! after || res ) {
218223 message . success ( `${ actionTypeMap [ editTypeRef . value ] } 成功!` ) ;
219224 doClose ( modal ) ;
220225 doQuery ( ) ;
@@ -223,19 +228,19 @@ export const useCurd = (config: CurdConfig) => {
223228
224229 /** 删除记录 */
225230 const doDelete = async ( record : any ) => {
226- debugger ;
227231 const { api, after, before, reqConfig } = ( actions . delete || { } ) as Action ;
228232 if ( ! api ) {
229233 throw new Error ( `action.delete.api required!` ) ;
230234 }
231235 let params : any = { id : record ?. record [ rowKey ] } ;
232- if ( before ) {
233- params = before ( params , record ?. record ) ;
236+ params = before ?.( params , record ?. row ) ;
237+ if ( ! params ) {
238+ getGlobalConfig ( ) . debug && console . warn ( 'actions.delete.before返回false阻止了请求执行,如果是自定义请求流程,可忽略' ) ;
239+ return ;
234240 }
235- const data = await doRequest ( api , params , reqConfig ) ;
236- if ( after ) {
237- after ( data ) ;
238- } else {
241+ const data = ( await doRequest ( api , params , reqConfig ) ) || { } ;
242+ const res = after ?.( data ) ;
243+ if ( ! after || res ) {
239244 message . success ( '删除成功!' ) ;
240245 doQuery ( ) ;
241246 }
0 commit comments