From d5788cea377fe2bb99e00e6e54f31d80017df54b Mon Sep 17 00:00:00 2001
From: RUNZE LU <36724300+lurunze1226@users.noreply.github.com>
Date: Fri, 30 Dec 2022 12:45:47 +0800
Subject: [PATCH] =?UTF-8?q?chore:=20Service=E7=BB=84=E4=BB=B6=E6=8F=90?=
=?UTF-8?q?=E4=BE=9B=5F=5Fresponse=E5=AD=97=E6=AE=B5=E7=94=A8=E4=BA=8E?=
=?UTF-8?q?=E5=8C=BA=E5=88=86=E6=8E=A5=E5=8F=A3=E5=A4=B1=E8=B4=A5=E6=83=85?=
=?UTF-8?q?=E5=86=B5=20(#5792)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
docs/zh-CN/components/service.md | 10 +--
.../cmpt-event-action/ServiceEvent.jsx | 62 +++++++++++++++++++
mock/cfc/mock/service/fail.json | 9 +++
packages/amis/src/renderers/Service.tsx | 19 ++++--
4 files changed, 89 insertions(+), 11 deletions(-)
create mode 100644 mock/cfc/mock/service/fail.json
diff --git a/docs/zh-CN/components/service.md b/docs/zh-CN/components/service.md
index f3d3bc9a9..7b1168652 100755
--- a/docs/zh-CN/components/service.md
+++ b/docs/zh-CN/components/service.md
@@ -703,11 +703,11 @@ ws.on('connection', function connection(ws) {
> `[name]`为当前数据域中的字段名,例如:当前数据域为 {username: 'amis'},则可以通过${username}获取对应的值。
-| 事件名称 | 事件参数 | 说明 |
-| ----------------- | ---------------------------------------------------------------------------------------- | --------------------------------------------------- |
-| init | - | 组件实例被创建并插入 DOM 中时触发。2.4.1 及以上版本 |
-| fetchInited | `event.data` api 远程请求返回的初始化数据
`[name]: any` 当前数据域中指定字段的值 | 远程初始化接口请求成功时触发 |
-| fetchSchemaInited | `event.data` schemaApi 远程请求返回的 UI 内容
`[name]: any` 当前数据域中指定字段的值 | 远程 schemaApi UI 内容接口请求成功 |
+| 事件名称 | 事件参数 | 说明 |
+| ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------- |
+| init | - | 组件实例被创建并插入 DOM 中时触发。2.4.1 及以上版本 |
+| fetchInited | `event.data` api 远程请求返回的初始化数据
`[name]: any` 当前数据域中指定字段的值`__response: {msg: string; error: boolean}`接口元数据, `msg`为消息体, `error`表示接口是否成功 | 远程初始化接口请求成功时触发 |
+| fetchSchemaInited | `event.data` schemaApi 远程请求返回的 UI 内容
`[name]: any` 当前数据域中指定字段的值`__response: {msg: string; error: boolean}`接口元数据, `msg`为消息体, `error`表示接口是否成功 | 远程 schemaApi UI 内容接口请求成功 |
## 动作表
diff --git a/examples/components/EventAction/cmpt-event-action/ServiceEvent.jsx b/examples/components/EventAction/cmpt-event-action/ServiceEvent.jsx
index 4eea90332..1164e02c6 100644
--- a/examples/components/EventAction/cmpt-event-action/ServiceEvent.jsx
+++ b/examples/components/EventAction/cmpt-event-action/ServiceEvent.jsx
@@ -37,6 +37,68 @@ export default {
actions: [
{
actionType: 'toast',
+ expression: '${!__response.error}',
+ args: {
+ msgType: 'success',
+ msg: 'API inited: ${date}'
+ }
+ }
+ ]
+ }
+ }
+ }
+ ]
+ }
+ },
+ {
+ type: 'divider'
+ },
+ {
+ type: 'tpl',
+ tpl: '对于amis的接口标准来说, status不为0, 则视为请求失败. 该事件额外提供了__response
参数用于区分接口状态码实际是200成功, 但接口中的status字段不为0的情况(接口状态码如果为非200, 则不会触发该事件)',
+ inline: false
+ },
+ {
+ type: 'action',
+ level: 'danger',
+ label: 'fetchInited(failed)',
+ actionType: 'dialog',
+ dialog: {
+ title: 'fetchInited(failed)',
+ body: [
+ {
+ type: 'service',
+ name: 'service-api',
+ id: 'service-failed',
+ api: '/api/mock2/service/fail',
+ body: {
+ type: 'panel',
+ title: '$title',
+ body: [
+ {
+ type: 'tpl',
+ tpl: 'toast消息提示设置了触发条件, 当接口失败时不显示toast, 所以结果只会有1个全局默认的toast, 如果不设置条件则会弹出2个toast.',
+ inline: false
+ },
+ {
+ type: 'tpl',
+ tpl: '错误信息是: ${__response.msg}',
+ inline: false
+ }
+ ]
+ },
+ onEvent: {
+ fetchInited: {
+ actions: [
+ {
+ actionType: 'setValue',
+ args: {
+ value: '${event.data}'
+ }
+ },
+ {
+ actionType: 'toast',
+ expression: '${!__response.error}',
args: {
msgType: 'success',
msg: 'API inited: ${date}'
diff --git a/mock/cfc/mock/service/fail.json b/mock/cfc/mock/service/fail.json
new file mode 100644
index 000000000..affe17e97
--- /dev/null
+++ b/mock/cfc/mock/service/fail.json
@@ -0,0 +1,9 @@
+{
+ "status": 500,
+ "msg": "Service fetch failed",
+ "error": {
+ "message": "Service fetch failed",
+ "code": 500
+ },
+ "data": null
+}
diff --git a/packages/amis/src/renderers/Service.tsx b/packages/amis/src/renderers/Service.tsx
index cdd1d2565..2ef4b6d5b 100644
--- a/packages/amis/src/renderers/Service.tsx
+++ b/packages/amis/src/renderers/Service.tsx
@@ -20,7 +20,8 @@ import {
isObjectShallowModified,
isVisible,
qsstringify,
- createObject
+ createObject,
+ extendObject
} from 'amis-core';
import {
BaseSchema,
@@ -502,10 +503,13 @@ export default class Service extends React.Component {
// todo 应该统一这块
// 初始化接口返回的是整个 response,
// 保存 ajax 请求的时候返回时数据部分。
- const data = result?.hasOwnProperty('ok') ? result.data : result;
- const {onBulkChange, dispatchEvent} = this.props;
+ const data = result?.hasOwnProperty('ok') ? result.data ?? {} : result;
+ const {onBulkChange, dispatchEvent, store} = this.props;
- dispatchEvent?.('fetchInited', data);
+ dispatchEvent?.('fetchInited', {
+ ...data,
+ __response: {msg: store.msg, error: store.error}
+ });
if (!isEmpty(data) && onBulkChange) {
onBulkChange(data);
@@ -515,9 +519,12 @@ export default class Service extends React.Component {
}
afterSchemaFetch(schema: any) {
- const {onBulkChange, formStore, dispatchEvent} = this.props;
+ const {onBulkChange, formStore, dispatchEvent, store} = this.props;
- dispatchEvent?.('fetchSchemaInited', schema);
+ dispatchEvent?.('fetchSchemaInited', {
+ ...schema,
+ __response: {msg: store.msg, error: store.error}
+ });
if (formStore && schema?.data && onBulkChange) {
onBulkChange && onBulkChange(schema.data);