mirror of
https://gitee.com/baidu/amis.git
synced 2024-11-30 02:48:55 +08:00
Merge pull request #7234 from pianruijie/feat/ajaxActionArgs
feat:ajaxAction传参处理
This commit is contained in:
commit
f5ccaf7d90
@ -256,18 +256,16 @@ run action ajax
|
||||
actions: [
|
||||
{
|
||||
actionType: 'ajax',
|
||||
args: {
|
||||
api: {
|
||||
url: 'https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/form/saveForm?name=${name}',
|
||||
method: 'get',
|
||||
"responseData": {
|
||||
"resId": "${id}"
|
||||
}
|
||||
api: {
|
||||
url: 'https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/form/saveForm?name=${name}',
|
||||
method: 'post',
|
||||
responseData: {
|
||||
"resId": "${id}",
|
||||
},
|
||||
messages: {
|
||||
success: '成功了!欧耶',
|
||||
failed: '失败了呢。。'
|
||||
}
|
||||
},
|
||||
},
|
||||
data: {
|
||||
age: 18
|
||||
@ -296,18 +294,16 @@ run action ajax
|
||||
actions: [
|
||||
{
|
||||
actionType: 'ajax',
|
||||
args: {
|
||||
api: {
|
||||
url: 'https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/initData',
|
||||
method: 'post'
|
||||
},
|
||||
api: {
|
||||
url: 'https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/initData',
|
||||
method: 'post',
|
||||
messages: {
|
||||
success: '成功了!欧耶',
|
||||
failed: '失败了呢。。'
|
||||
},
|
||||
options: {
|
||||
silent: true
|
||||
}
|
||||
},
|
||||
options: {
|
||||
silent: true,
|
||||
},
|
||||
data: {
|
||||
age: 18
|
||||
@ -328,9 +324,7 @@ run action ajax
|
||||
}
|
||||
```
|
||||
|
||||
**动作属性(args)**
|
||||
|
||||
> `< 1.8.0 及以下版本`,以下属性与 args 同级。
|
||||
**动作属性**
|
||||
|
||||
| 属性名 | 类型 | 默认值 | 说明 |
|
||||
| -------- | ----------------------------------- | ------ | ------------------------- |
|
||||
|
@ -12,15 +12,13 @@ import {
|
||||
|
||||
export interface IAjaxAction extends ListenerAction {
|
||||
action: 'ajax';
|
||||
args: {
|
||||
api: Api;
|
||||
messages?: {
|
||||
success: string;
|
||||
failed: string;
|
||||
};
|
||||
options?: Record<string, any>;
|
||||
[propName: string]: any;
|
||||
api: Api;
|
||||
messages?: {
|
||||
success: string;
|
||||
failed: string;
|
||||
};
|
||||
options?: Record<string, any>;
|
||||
[propName: string]: any;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -45,17 +43,25 @@ export class AjaxAction implements RendererAction {
|
||||
throw new Error('env.fetcher is required!');
|
||||
}
|
||||
if (this.fetcherType === 'download' && action.actionType === 'download') {
|
||||
// 兼容老的格式
|
||||
if ((action as any).args?.api) {
|
||||
(action as any).args.api.responseType = 'blob';
|
||||
}
|
||||
if ((action as any)?.api) {
|
||||
(action as any).api.responseType = 'blob';
|
||||
}
|
||||
}
|
||||
|
||||
const env = event.context.env;
|
||||
const silent = action?.options?.silent ?? action.args?.options?.silent;
|
||||
const messages =
|
||||
(action?.api as ApiObject)?.messages ??
|
||||
(action.args?.api as ApiObject)?.messages;
|
||||
try {
|
||||
const result = await env.fetcher(
|
||||
action.args?.api,
|
||||
action?.api ?? action.args?.api,
|
||||
action.data ?? {},
|
||||
action.args?.options ?? {}
|
||||
action?.options ?? action.args?.options ?? {}
|
||||
);
|
||||
const responseData =
|
||||
!isEmpty(result.data) || result.ok
|
||||
@ -75,18 +81,15 @@ export class AjaxAction implements RendererAction {
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
if (!action.args?.options?.silent) {
|
||||
if (!silent) {
|
||||
if (!result.ok) {
|
||||
throw new ServerError(
|
||||
(action.args?.api as ApiObject)?.messages?.failed ??
|
||||
action.args?.messages?.failed ??
|
||||
result.msg,
|
||||
messages?.failed ?? action.args?.messages?.failed ?? result.msg,
|
||||
result
|
||||
);
|
||||
} else {
|
||||
const msg =
|
||||
(action.args?.api as ApiObject)?.messages?.success ??
|
||||
messages?.success ??
|
||||
action.args?.messages?.success ??
|
||||
result.msg ??
|
||||
result.defaultMsg;
|
||||
@ -106,7 +109,7 @@ export class AjaxAction implements RendererAction {
|
||||
|
||||
return result.data;
|
||||
} catch (e) {
|
||||
if (!action.args?.options?.silent) {
|
||||
if (!silent) {
|
||||
if (e.type === 'ServerError') {
|
||||
const result = (e as ServerError).response;
|
||||
env.notify(
|
||||
|
@ -12,8 +12,8 @@ export const BASE_ACTION_PROPS = [
|
||||
'__actionDesc',
|
||||
'preventDefault',
|
||||
'stopPropagation',
|
||||
'expression',
|
||||
'outputVar'
|
||||
'expression'
|
||||
// 'outputVar'
|
||||
];
|
||||
|
||||
export default class CmptActionSelect extends React.Component<RendererProps> {
|
||||
|
@ -690,9 +690,9 @@ export const ACTION_TYPE_TREE = (manager: any): RendererPluginAction[] => {
|
||||
actionLabel: '发送请求',
|
||||
actionType: 'ajax',
|
||||
description: '配置并发送API请求',
|
||||
innerArgs: ['api', 'options'],
|
||||
// innerArgs: ['api', 'options'],
|
||||
descDetail: (info: any) => {
|
||||
let apiInfo = info?.args?.api;
|
||||
let apiInfo = info?.api ?? info?.args?.api;
|
||||
if (typeof apiInfo === 'string') {
|
||||
apiInfo = normalizeApi(apiInfo);
|
||||
}
|
||||
@ -711,43 +711,72 @@ export const ACTION_TYPE_TREE = (manager: any): RendererPluginAction[] => {
|
||||
type: 'wrapper',
|
||||
className: 'p-none',
|
||||
body: [
|
||||
getArgsWrapper(
|
||||
[
|
||||
getSchemaTpl('apiControl', {
|
||||
name: 'api',
|
||||
label: '配置请求',
|
||||
mode: 'horizontal',
|
||||
size: 'lg',
|
||||
inputClassName: 'm-b-none',
|
||||
renderLabel: true,
|
||||
required: true
|
||||
}),
|
||||
// getArgsWrapper(
|
||||
// [
|
||||
// getSchemaTpl('apiControl', {
|
||||
// name: 'api',
|
||||
// label: '配置请求',
|
||||
// mode: 'horizontal',
|
||||
// size: 'lg',
|
||||
// inputClassName: 'm-b-none',
|
||||
// renderLabel: true,
|
||||
// required: true
|
||||
// }),
|
||||
// {
|
||||
// name: 'options',
|
||||
// type: 'combo',
|
||||
// label: tipedLabel(
|
||||
// '静默请求',
|
||||
// '开启后,服务请求将以静默模式发送,即不会弹出成功或报错提示。'
|
||||
// ),
|
||||
// mode: 'horizontal',
|
||||
// items: [
|
||||
// {
|
||||
// type: 'switch',
|
||||
// name: 'silent',
|
||||
// label: false,
|
||||
// onText: '开启',
|
||||
// offText: '关闭',
|
||||
// mode: 'horizontal',
|
||||
// pipeIn: defaultValue(false)
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// ],
|
||||
// false,
|
||||
// {
|
||||
// className: 'action-apiControl'
|
||||
// }
|
||||
// ),
|
||||
getSchemaTpl('apiControl', {
|
||||
name: 'api',
|
||||
label: '配置请求',
|
||||
mode: 'horizontal',
|
||||
size: 'lg',
|
||||
inputClassName: 'm-b-none',
|
||||
renderLabel: true,
|
||||
required: true
|
||||
}),
|
||||
{
|
||||
name: 'options',
|
||||
type: 'combo',
|
||||
label: tipedLabel(
|
||||
'静默请求',
|
||||
'开启后,服务请求将以静默模式发送,即不会弹出成功或报错提示。'
|
||||
),
|
||||
mode: 'horizontal',
|
||||
items: [
|
||||
{
|
||||
name: 'options',
|
||||
type: 'combo',
|
||||
label: tipedLabel(
|
||||
'静默请求',
|
||||
'开启后,服务请求将以静默模式发送,即不会弹出成功或报错提示。'
|
||||
),
|
||||
type: 'switch',
|
||||
name: 'silent',
|
||||
label: false,
|
||||
onText: '开启',
|
||||
offText: '关闭',
|
||||
mode: 'horizontal',
|
||||
items: [
|
||||
{
|
||||
type: 'switch',
|
||||
name: 'silent',
|
||||
label: false,
|
||||
onText: '开启',
|
||||
offText: '关闭',
|
||||
mode: 'horizontal',
|
||||
pipeIn: defaultValue(false)
|
||||
}
|
||||
]
|
||||
pipeIn: defaultValue(false)
|
||||
}
|
||||
],
|
||||
false,
|
||||
{
|
||||
className: 'action-apiControl'
|
||||
}
|
||||
),
|
||||
]
|
||||
},
|
||||
{
|
||||
name: 'outputVar',
|
||||
type: 'input-text',
|
||||
@ -787,26 +816,35 @@ export const ACTION_TYPE_TREE = (manager: any): RendererPluginAction[] => {
|
||||
actionLabel: '下载文件',
|
||||
actionType: 'download',
|
||||
description: '触发下载文件',
|
||||
innerArgs: ['api'],
|
||||
// innerArgs: ['api'],
|
||||
schema: {
|
||||
type: 'wrapper',
|
||||
style: {padding: '0'},
|
||||
body: [
|
||||
getArgsWrapper(
|
||||
getSchemaTpl('apiControl', {
|
||||
name: 'api',
|
||||
label: '配置请求',
|
||||
mode: 'horizontal',
|
||||
inputClassName: 'm-b-none',
|
||||
size: 'lg',
|
||||
renderLabel: true,
|
||||
required: true
|
||||
}),
|
||||
false,
|
||||
{
|
||||
className: 'action-apiControl'
|
||||
}
|
||||
)
|
||||
// getArgsWrapper(
|
||||
// getSchemaTpl('apiControl', {
|
||||
// name: 'api',
|
||||
// label: '配置请求',
|
||||
// mode: 'horizontal',
|
||||
// inputClassName: 'm-b-none',
|
||||
// size: 'lg',
|
||||
// renderLabel: true,
|
||||
// required: true
|
||||
// }),
|
||||
// false,
|
||||
// {
|
||||
// className: 'action-apiControl'
|
||||
// }
|
||||
// )
|
||||
getSchemaTpl('apiControl', {
|
||||
name: 'api',
|
||||
label: '配置请求',
|
||||
mode: 'horizontal',
|
||||
inputClassName: 'm-b-none',
|
||||
size: 'lg',
|
||||
renderLabel: true,
|
||||
required: true
|
||||
})
|
||||
]
|
||||
}
|
||||
}
|
||||
@ -2738,11 +2776,13 @@ export const getEventControlConfig = (
|
||||
config.__actionExpression = action.args?.value;
|
||||
}
|
||||
|
||||
if (
|
||||
action.actionType === 'ajax' &&
|
||||
typeof action?.args?.api === 'string'
|
||||
) {
|
||||
action.args.api = normalizeApi(action?.args?.api);
|
||||
if (['ajax', 'download'].includes(action.actionType)) {
|
||||
config.api = action.api ?? action?.args?.api;
|
||||
config.options = action.options ?? action?.args?.options;
|
||||
if (typeof action?.api === 'string') {
|
||||
config.api = normalizeApi(action?.api);
|
||||
}
|
||||
delete config.args;
|
||||
}
|
||||
|
||||
// 获取动作专有配置参数
|
||||
|
@ -491,8 +491,12 @@ export class EventControl extends React.Component<
|
||||
}
|
||||
|
||||
buildEventDataSchema(data: any, manager: EditorManager) {
|
||||
const {actionTree, pluginActions, commonActions, allComponents} =
|
||||
this.props;
|
||||
const {
|
||||
actionTree,
|
||||
actions: pluginActions,
|
||||
commonActions,
|
||||
allComponents
|
||||
} = this.props;
|
||||
const {events, onEvent} = this.state;
|
||||
|
||||
const eventConfig = events.find(
|
||||
@ -564,7 +568,7 @@ export class EventControl extends React.Component<
|
||||
properties: {
|
||||
...jsonSchema.properties?.data?.properties,
|
||||
[action.outputVar!]: {
|
||||
...actionSchema[0],
|
||||
...(Array.isArray(actionSchema) && (actionSchema[0] || {})),
|
||||
title: `${action.outputVar}(${actionLabel}动作出参)`
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user