Skip to content

Commit

Permalink
fix: curd中before、after中控制后续逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
aringlai committed Nov 28, 2023
1 parent 57cb78f commit 7ba64e1
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 72 deletions.
45 changes: 45 additions & 0 deletions docs/examples/antdUseCurd.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,62 @@ const { render, editTypeRef, selectedRows, openModal } = useCurd({
actions: {
query: {
api: '/user.json',
before(params) {
// 修改请求参数
params.myId = '111';
// return false; // 阻止请求
return params;
},
after(data) {
// return false; // 阻止默认数据绑定逻辑
// 修改请求接口,适配绑定数据
const { pager, list } = data;
return {
list: list,
pager: pager,
};
},
},
create: {
api: '/success.json',
before(params) {
// 修改请求参数
params.myId = '111';
// return false; // 阻止请求
return params;
},
after(data) {
// return false; // 阻止关闭弹窗和刷新列表
return data;
},
},
reset: {},
update: {
hidden: true, // 更新按钮修改行数据判断时,可以隐藏默认更新按钮,在template实现
api: '/error.json',
before(params) {
// 修改请求参数
params.myId = '111';
// return false; // 阻止请求
return params;
},
after(data) {
// return false; // 阻止关闭弹窗和刷新列表
return data;
},
},
delete: {
api: '/success.json',
before(params) {
// 修改请求参数
params.myId = '111';
// return false; // 阻止请求
return params;
},
after(data) {
// return false; // 阻止默认提示和刷新列表
return data;
},
},
view: {},
},
Expand Down
45 changes: 45 additions & 0 deletions docs/examples/elementUseCurd.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,62 @@ const { render, editTypeRef, selectedRows, openModal } = useCurd({
actions: {
query: {
api: '/user.json',
before(params) {
// 修改请求参数
params.myId = '111';
// return false; // 阻止请求
return params;
},
after(data) {
// return false; // 阻止默认数据绑定逻辑
// 修改请求接口,适配绑定数据
const { pager, list } = data;
return {
list: list,
pager: pager,
};
},
},
create: {
api: '/success.json',
before(params) {
// 修改请求参数
params.myId = '111';
// return false; // 阻止请求
return params;
},
after(data) {
// return false; // 阻止关闭弹窗和刷新列表
return data;
},
},
reset: {},
update: {
hidden: true, // 更新按钮修改行数据判断时,可以隐藏默认更新按钮,在template实现
api: '/error.json',
before(params) {
// 修改请求参数
params.myId = '111';
// return false; // 阻止请求
return params;
},
after(data) {
// return false; // 阻止关闭弹窗和刷新列表
return data;
},
},
delete: {
api: '/success.json',
before(params) {
// 修改请求参数
params.myId = '111';
// return false; // 阻止请求
return params;
},
after(data) {
// return false; // 阻止默认提示和刷新列表
return data;
},
},
view: {},
},
Expand Down
45 changes: 45 additions & 0 deletions docs/examples/fesdUseCurd.vue
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,62 @@ const { render, editTypeRef, query, selectedRows, openModal } = useCurd({
actions: {
query: {
api: '/user.json',
before(params) {
// 修改请求参数
params.myId = '111';
// return false; // 阻止请求
return params;
},
after(data) {
// return false; // 阻止默认数据绑定逻辑
// 修改请求接口,适配绑定数据
const { pager, list } = data;
return {
list: list,
pager: pager,
};
},
},
create: {
api: '/success.json',
before(params) {
// 修改请求参数
params.myId = '111';
// return false; // 阻止请求
return params;
},
after(data) {
// return false; // 阻止关闭弹窗和刷新列表
return data;
},
},
reset: {},
update: {
hidden: true, // 更新按钮修改行数据判断时,可以隐藏默认更新按钮,在template实现
api: '/error.json',
before(params) {
// 修改请求参数
params.myId = '111';
// return false; // 阻止请求
return params;
},
after(data) {
// return false; // 阻止关闭弹窗和刷新列表
return data;
},
},
delete: {
api: '/success.json',
before(params) {
// 修改请求参数
params.myId = '111';
// return false; // 阻止请求
return params;
},
after(data) {
// return false; // 阻止默认提示和刷新列表
return data;
},
},
view: {},
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koala-form",
"version": "2.0.2",
"version": "2.0.3",
"description": "vue form helper",
"author": "aringlai",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/antd-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@koala-form/antd-plugin",
"version": "2.0.2",
"version": "2.0.3",
"description": "KoalaForm Ant Design Vue ui插件",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down
49 changes: 27 additions & 22 deletions packages/antd-plugin/src/useCurd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ interface Action extends ComponentDesc {
reqConfig?: any;
/** 隐藏默认按钮 */
hidden?: boolean;
/** 请求前执行,可修改参数 */
before?: (params: Record<string, any>, ...args: any[]) => Record<string, any>;
/** 请求后执行,可修改结果 */
after?: (params: Record<string, any>) => Record<string, any>;
/** 请求前执行,可修改请求参数,返回false可阻止请求发送 */
before?: (params: Record<string, any>, ...args: any[]) => Record<string, any> | boolean;
/** 请求后执行,可修改结果,返回false可阻止默认流程的执行 */
after?: (params: Record<string, any>) => Record<string, any> | boolean;
/** 打开modal前执行,可修改modal里表单的值 */
open?: (params: Record<string, any>) => Record<string, any>;
}
Expand Down Expand Up @@ -125,13 +125,17 @@ export const useCurd = (config: CurdConfig) => {
throw new Error(`action.query.api required!`);
}
const { api, after, before, reqConfig } = actions.query;
let params = doBeforeQuery(query, pager);
if (before) {
params = before(params);
let params: any = doBeforeQuery(query, pager);
params = before?.(params);
if (!params) {
getGlobalConfig().debug && console.warn('actions.query.before返回false阻止了请求执行,如果是自定义请求流程,可忽略');
return;
}
let data = await doRequest(api, params, reqConfig);
if (after) {
data = after(data);
data = after?.(data);
if (!data) {
getGlobalConfig().debug && console.warn('actions.query.after返回false阻止了默认逻辑数据绑定,请自行绑定数据!');
return;
}
table.modelRef.value = data.list;
if (pagerCfg) {
Expand Down Expand Up @@ -208,13 +212,14 @@ export const useCurd = (config: CurdConfig) => {
}
await doValidate(edit);
let params = doGetFormData(edit);
if (before) {
params = before(params);
params = before?.(params);
if (!params) {
getGlobalConfig().debug && console.warn('actions.[create/update].before返回false阻止了请求执行,如果是自定义请求流程,可忽略');
return;
}
const data = await doRequest(api, params, reqConfig);
if (after) {
after(data);
} else {
const data = (await doRequest(api, params, reqConfig)) || {};
const res = after?.(data);
if (!after || res) {
message.success(`${actionTypeMap[editTypeRef.value]}成功!`);
doClose(modal);
doQuery();
Expand All @@ -223,19 +228,19 @@ export const useCurd = (config: CurdConfig) => {

/** 删除记录 */
const doDelete = async (record: any) => {
debugger;
const { api, after, before, reqConfig } = (actions.delete || {}) as Action;
if (!api) {
throw new Error(`action.delete.api required!`);
}
let params: any = { id: record?.record[rowKey] };
if (before) {
params = before(params, record?.record);
params = before?.(params, record?.row);
if (!params) {
getGlobalConfig().debug && console.warn('actions.delete.before返回false阻止了请求执行,如果是自定义请求流程,可忽略');
return;
}
const data = await doRequest(api, params, reqConfig);
if (after) {
after(data);
} else {
const data = (await doRequest(api, params, reqConfig)) || {};
const res = after?.(data);
if (!after || res) {
message.success('删除成功!');
doQuery();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/element-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@koala-form/element-plugin",
"version": "2.0.5",
"version": "2.0.6",
"description": "KoalaForm element plus ui插件",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down
48 changes: 27 additions & 21 deletions packages/element-plugin/src/useCurd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ interface Action extends ComponentDesc {
reqConfig?: any;
/** 隐藏默认按钮 */
hidden?: boolean;
/** 请求前执行,可修改参数 */
before?: (params: Record<string, any>, ...args: any[]) => Record<string, any>;
/** 请求后执行,可修改结果 */
after?: (params: Record<string, any>) => Record<string, any>;
/** 请求前执行,可修改请求参数,返回false可阻止请求发送 */
before?: (params: Record<string, any>, ...args: any[]) => Record<string, any> | boolean;
/** 请求后执行,可修改结果,返回false可阻止默认流程的执行 */
after?: (params: Record<string, any>) => Record<string, any> | boolean;
/** 打开modal前执行,可修改modal里表单的值 */
open?: (params: Record<string, any>) => Record<string, any>;
}
Expand Down Expand Up @@ -123,13 +123,17 @@ export const useCurd = (config: CurdConfig) => {
throw new Error(`action.query.api required!`);
}
const { api, after, before, reqConfig } = actions.query;
let params = doBeforeQuery(query, pager);
if (before) {
params = before(params);
let params: any = doBeforeQuery(query, pager);
params = before?.(params);
if (!params) {
getGlobalConfig().debug && console.warn('actions.query.before返回false阻止了请求执行,如果是自定义请求流程,可忽略');
return;
}
let data = await doRequest(api, params, reqConfig);
if (after) {
data = after(data);
data = after?.(data);
if (!data) {
getGlobalConfig().debug && console.warn('actions.query.after返回false阻止了默认逻辑数据绑定,请自行绑定数据!');
return;
}
table.modelRef.value = data.list;
if (pagerCfg) {
Expand Down Expand Up @@ -194,13 +198,14 @@ export const useCurd = (config: CurdConfig) => {
}
await doValidate(edit);
let params = doGetFormData(edit);
if (before) {
params = before(params);
params = before?.(params);
if (!params) {
getGlobalConfig().debug && console.warn('actions.[create/update].before返回false阻止了请求执行,如果是自定义请求流程,可忽略');
return;
}
const data = await doRequest(api, params, reqConfig);
if (after) {
after(data);
} else {
const data = (await doRequest(api, params, reqConfig)) || {};
const res = after?.(data);
if (!after || res) {
ElMessage.success(`${actionTypeMap[editTypeRef.value]}成功!`);
doClose(modal);
doQuery();
Expand All @@ -214,13 +219,14 @@ export const useCurd = (config: CurdConfig) => {
throw new Error(`action.delete.api required!`);
}
let params: any = { id: record?.row[rowKey] };
if (before) {
params = before(params, record?.row);
params = before?.(params, record?.row);
if (!params) {
getGlobalConfig().debug && console.warn('actions.delete.before返回false阻止了请求执行,如果是自定义请求流程,可忽略');
return;
}
const data = await doRequest(api, params, reqConfig);
if (after) {
after(data);
} else {
const data = (await doRequest(api, params, reqConfig)) || {};
const res = after?.(data);
if (!after || res) {
ElMessage.success('删除成功!');
doQuery();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/fes-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@koala-form/fes-plugin",
"version": "2.0.0",
"version": "2.0.1",
"description": "KoalaForm fes design ui插件",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/fes-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const componentPlugin: PluginFunction<SceneContext, SceneConfig> = (api)
api.onSelfStart(({ ctx }) => {
ctx.getComponent = (name) => {
if (typeof name === 'string') {
const comp = fesD[`F${name}`];
const comp = (fesD as any)[`F${name}`];
if (isComponent(comp)) return comp;
else return name;
} else {
Expand Down
Loading

0 comments on commit 7ba64e1

Please sign in to comment.