mirror of
https://gitee.com/baidu/amis.git
synced 2024-11-29 18:48:45 +08:00
api 返回格式兼容没有 status 和 data 的情况,在更多场景下无需适配 (#1054)
* api 返回格式兼容没有 status 和 data 的情况,在更多场景下无需适配 * 补充 api 兼容模式的文档
This commit is contained in:
parent
b9a81c37ac
commit
99e914197b
@ -77,18 +77,21 @@ API 类型用于配置请求接口的格式,涉及请求方式、请求地址
|
||||
}
|
||||
```
|
||||
|
||||
### 不推荐的格式
|
||||
### 兼容格式
|
||||
|
||||
部分组件为了可以兼容,支持下面这种直接返回数组的用法,但并不推荐这种方式。
|
||||
> 1.0.19 及以上版本。
|
||||
|
||||
为了支持多种后端,amis 支持直接返回数据的方式,无需返回 status 和将数据放在 data 字段中,比如下面的例子:
|
||||
|
||||
```json
|
||||
{
|
||||
"status": 0,
|
||||
"msg": "",
|
||||
"data": ["a", "b"] // 不推荐,使用 key 包装
|
||||
"username": "amis",
|
||||
"email": "amis@amis.com"
|
||||
}
|
||||
```
|
||||
|
||||
但这种方式无法显示错误信息,只能通过返回 http 状态码来标识错误。
|
||||
|
||||
## 复杂配置
|
||||
|
||||
API 还支持配置对象类型
|
||||
|
340
mock/test.json
340
mock/test.json
@ -1,340 +0,0 @@
|
||||
{
|
||||
"status": 0,
|
||||
"result": null,
|
||||
"msg": null,
|
||||
"data": {
|
||||
"count": 5,
|
||||
"pageNo": 1,
|
||||
"pageSize": 10,
|
||||
"orderBy": null,
|
||||
"order": null,
|
||||
"rows": [
|
||||
{
|
||||
"userId": 630152,
|
||||
"planId": 0,
|
||||
"unitId": 2201543308,
|
||||
"creativeId": 187,
|
||||
"ideaId": 0,
|
||||
"title": "闪投创意_橱窗测试",
|
||||
"brand": "橱窗样式品牌",
|
||||
"url": "http://www.tuiguang.com",
|
||||
"userPortrait": "http://fc-feed.cdn.bcebos.com/0/pic/65e55b02f73ea07b4079e247a2ffd05b.jpg",
|
||||
"pictures": [
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/164c83612b9837f9f565cd80a0d6245e.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/4258ea37ba97236ff661be0a09ceea02.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/62c40ba3e297ea57c5b80f41e958b4f2.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/d6362946b69a71340595e56d8527a31a.jpg"
|
||||
}
|
||||
],
|
||||
"signature": "b4299ee3ed76b2f439d7e29dfc5cf6ab",
|
||||
"auditResult": "",
|
||||
"status": 1,
|
||||
"type": "DPA",
|
||||
"lastModifiedTime": "2018-05-16 20:39:19"
|
||||
},
|
||||
{
|
||||
"userId": 630152,
|
||||
"planId": 0,
|
||||
"unitId": 2201543308,
|
||||
"creativeId": 187,
|
||||
"ideaId": 54009256299,
|
||||
"title": "闪投创意_橱窗测试",
|
||||
"brand": "橱窗样式品牌",
|
||||
"url": "http://www.tuiguang.com1297102669",
|
||||
"userPortrait": "http://fc-feed.cdn.bcebos.com/0/pic/65e55b02f73ea07b4079e247a2ffd05b.jpg",
|
||||
"pictures": [
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/164c83612b9837f9f565cd80a0d6245e.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/4258ea37ba97236ff661be0a09ceea02.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/d6362946b69a71340595e56d8527a31a.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/62c40ba3e297ea57c5b80f41e958b4f2.jpg"
|
||||
}
|
||||
],
|
||||
"signature": "801bb193d8ed2d44aa8d672d30d0b2a1",
|
||||
"auditResult": "",
|
||||
"status": 1,
|
||||
"type": "DPA",
|
||||
"lastModifiedTime": "2018-05-16 20:42:22"
|
||||
},
|
||||
{
|
||||
"userId": 630152,
|
||||
"planId": 0,
|
||||
"unitId": 2201545776,
|
||||
"creativeId": 187,
|
||||
"ideaId": 0,
|
||||
"title": "闪投创意_橱窗测试",
|
||||
"brand": "橱窗样式品牌",
|
||||
"url": "http://www.tuiguang.com",
|
||||
"userPortrait": "http://fc-feed.cdn.bcebos.com/0/pic/65e55b02f73ea07b4079e247a2ffd05b.jpg",
|
||||
"pictures": [
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/164c83612b9837f9f565cd80a0d6245e.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/4258ea37ba97236ff661be0a09ceea02.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/62c40ba3e297ea57c5b80f41e958b4f2.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/d6362946b69a71340595e56d8527a31a.jpg"
|
||||
}
|
||||
],
|
||||
"signature": "b4299ee3ed76b2f439d7e29dfc5cf6ab",
|
||||
"auditResult": "0",
|
||||
"status": 1,
|
||||
"type": "DPA",
|
||||
"lastModifiedTime": "2018-05-16 20:39:19"
|
||||
},
|
||||
{
|
||||
"userId": 630152,
|
||||
"planId": 0,
|
||||
"unitId": 2201545776,
|
||||
"creativeId": 187,
|
||||
"ideaId": 54009256074,
|
||||
"title": "橱窗样式",
|
||||
"brand": "橱窗品牌",
|
||||
"url": "http://www.ronhe.comww",
|
||||
"userPortrait": "http://fc-feed.cdn.bcebos.com/0/pic/e1228057dc685dcd90d2be52e7160e11.jpg",
|
||||
"pictures": [
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/62c40ba3e297ea57c5b80f41e958b4f2.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/d6362946b69a71340595e56d8527a31a.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/164c83612b9837f9f565cd80a0d6245e.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/4258ea37ba97236ff661be0a09ceea02.jpg"
|
||||
}
|
||||
],
|
||||
"signature": "5803a7eba7cbe6ba111e310549f0204d",
|
||||
"auditResult": "",
|
||||
"status": 1,
|
||||
"type": "DPA",
|
||||
"lastModifiedTime": "2018-05-16 19:53:11"
|
||||
},
|
||||
{
|
||||
"userId": 630152,
|
||||
"planId": 0,
|
||||
"unitId": 2201545776,
|
||||
"creativeId": 171,
|
||||
"ideaId": 54009255238,
|
||||
"title": "闪投创意_测试aa",
|
||||
"brand": "闪投测试ff",
|
||||
"url": "http://www.baidu.com",
|
||||
"userPortrait": "http://fc-feed.cdn.bcebos.com/0/pic/e2a076e03f1624997b27eb5f0f8d69ff.jpgdd",
|
||||
"pictures": [
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/62c40ba3e297ea57c5b80f41e958b4f2.jpg"
|
||||
}
|
||||
],
|
||||
"signature": "10a208706d1c1bf4716d85e4e1ae5447",
|
||||
"auditResult": "",
|
||||
"status": 1,
|
||||
"type": "DPA",
|
||||
"lastModifiedTime": "2018-05-16 19:55:45"
|
||||
}
|
||||
]
|
||||
},
|
||||
"warning": null,
|
||||
"debug": {
|
||||
"request": {
|
||||
"url": "http://prim2.jpaas-off00.baidu.com/audit/feed/idealist",
|
||||
"params": {
|
||||
"_url": "http://prim2.jpaas-off00.baidu.com/audit/feed/idealist"
|
||||
},
|
||||
"data": {
|
||||
"userId": "630152",
|
||||
"pageSize": "10",
|
||||
"pageNo": "1",
|
||||
"page": 1,
|
||||
"perPage": 10
|
||||
},
|
||||
"headers": {
|
||||
"AMIS_GROUP_KEY": "prim",
|
||||
"AMIS_ROLES": "",
|
||||
"AMIS_PERMS": "__is_uuap_user",
|
||||
"AMIS_USER_IP": "10.91.140.154",
|
||||
"AMIS_USER_TYPE": 0,
|
||||
"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aKDtjKsb5vfsJQ-24OoHRjP2DTjhPrM-P6lbMT-0bFH_nQTtxnKEJO3QhbMLl-E2JJZ-M3rJGn7_JjO2lbAOI_R3tovjnFSjx50bxQxtNRR2CnjtpvhHlRTy5JobUPUDMc9LUvqKH4E3-oJqCKBbKt93f; BAIDUID=5B2BBA9D53B86EE8ECE6F4A51151026A:FG=1; Hm_lvt_1f80f2c9dbe21dc3af239cf9eee90f1f=1525227445; Hm_lvt_0b20b782e46454330553f136ffb8c303=1525658669; Hm_lpvt_0b20b782e46454330553f136ffb8c303=1525661732; BDUSS=FnZHhkam9mcW15c2NPNEhxRWlzNEQ0cWRXS1ByZEd5NzlWdmx0ZTFiV2d-aUZiQUFBQUFBJCQAAAAAAAAAAAEAAAAQl~oAbGlhb3h1ZXpoaQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKBx-lqgcfpaV1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; SIGNIN_UC=70a2711cf1d3d9b1a82d2f87d633bd8a02764772933; H_PS_PSSID=1420_26458_21118_18559_22159; BAIDU_WISE_UID=wapp_1526524424557_688; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; PSINO=2; amisid=s%3Am2bXcd_ZZ0HGuOlFzFQIO-7MXYpTyplI.rnSQoUh4LG0n22lZWJM0dEdfFi1AOCly8JeLn4gPaW0; amis_env=2; Hm_lpvt_1f80f2c9dbe21dc3af239cf9eee90f1f=1526544494",
|
||||
"Content-Type": "application/json",
|
||||
"Content-Length": 70
|
||||
},
|
||||
"method": "post"
|
||||
},
|
||||
"response": {
|
||||
"status": 0,
|
||||
"result": null,
|
||||
"msg": null,
|
||||
"data": {
|
||||
"count": 5,
|
||||
"pageNo": 1,
|
||||
"pageSize": 10,
|
||||
"orderBy": null,
|
||||
"order": null,
|
||||
"rows": [
|
||||
{
|
||||
"userId": 630152,
|
||||
"planId": 0,
|
||||
"unitId": 2201543308,
|
||||
"creativeId": 187,
|
||||
"ideaId": 0,
|
||||
"title": "闪投创意_橱窗测试",
|
||||
"brand": "橱窗样式品牌",
|
||||
"url": "http://www.tuiguang.com",
|
||||
"userPortrait": "http://fc-feed.cdn.bcebos.com/0/pic/65e55b02f73ea07b4079e247a2ffd05b.jpg",
|
||||
"pictures": [
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/164c83612b9837f9f565cd80a0d6245e.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/4258ea37ba97236ff661be0a09ceea02.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/62c40ba3e297ea57c5b80f41e958b4f2.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/d6362946b69a71340595e56d8527a31a.jpg"
|
||||
}
|
||||
],
|
||||
"signature": "b4299ee3ed76b2f439d7e29dfc5cf6ab",
|
||||
"auditResult": "",
|
||||
"status": 1,
|
||||
"type": "DPA",
|
||||
"lastModifiedTime": "2018-05-16 20:39:19"
|
||||
},
|
||||
{
|
||||
"userId": 630152,
|
||||
"planId": 0,
|
||||
"unitId": 2201543308,
|
||||
"creativeId": 187,
|
||||
"ideaId": 54009256299,
|
||||
"title": "闪投创意_橱窗测试",
|
||||
"brand": "橱窗样式品牌",
|
||||
"url": "http://www.tuiguang.com1297102669",
|
||||
"userPortrait": "http://fc-feed.cdn.bcebos.com/0/pic/65e55b02f73ea07b4079e247a2ffd05b.jpg",
|
||||
"pictures": [
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/164c83612b9837f9f565cd80a0d6245e.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/4258ea37ba97236ff661be0a09ceea02.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/d6362946b69a71340595e56d8527a31a.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/62c40ba3e297ea57c5b80f41e958b4f2.jpg"
|
||||
}
|
||||
],
|
||||
"signature": "801bb193d8ed2d44aa8d672d30d0b2a1",
|
||||
"auditResult": "",
|
||||
"status": 1,
|
||||
"type": "DPA",
|
||||
"lastModifiedTime": "2018-05-16 20:42:22"
|
||||
},
|
||||
{
|
||||
"userId": 630152,
|
||||
"planId": 0,
|
||||
"unitId": 2201545776,
|
||||
"creativeId": 187,
|
||||
"ideaId": 0,
|
||||
"title": "闪投创意_橱窗测试",
|
||||
"brand": "橱窗样式品牌",
|
||||
"url": "http://www.tuiguang.com",
|
||||
"userPortrait": "http://fc-feed.cdn.bcebos.com/0/pic/65e55b02f73ea07b4079e247a2ffd05b.jpg",
|
||||
"pictures": [
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/164c83612b9837f9f565cd80a0d6245e.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/4258ea37ba97236ff661be0a09ceea02.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/62c40ba3e297ea57c5b80f41e958b4f2.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/d6362946b69a71340595e56d8527a31a.jpg"
|
||||
}
|
||||
],
|
||||
"signature": "b4299ee3ed76b2f439d7e29dfc5cf6ab",
|
||||
"auditResult": "0",
|
||||
"status": 1,
|
||||
"type": "DPA",
|
||||
"lastModifiedTime": "2018-05-16 20:39:19"
|
||||
},
|
||||
{
|
||||
"userId": 630152,
|
||||
"planId": 0,
|
||||
"unitId": 2201545776,
|
||||
"creativeId": 187,
|
||||
"ideaId": 54009256074,
|
||||
"title": "橱窗样式",
|
||||
"brand": "橱窗品牌",
|
||||
"url": "http://www.ronhe.comww",
|
||||
"userPortrait": "http://fc-feed.cdn.bcebos.com/0/pic/e1228057dc685dcd90d2be52e7160e11.jpg",
|
||||
"pictures": [
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/62c40ba3e297ea57c5b80f41e958b4f2.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/d6362946b69a71340595e56d8527a31a.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/164c83612b9837f9f565cd80a0d6245e.jpg"
|
||||
},
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/4258ea37ba97236ff661be0a09ceea02.jpg"
|
||||
}
|
||||
],
|
||||
"signature": "5803a7eba7cbe6ba111e310549f0204d",
|
||||
"auditResult": "",
|
||||
"status": 1,
|
||||
"type": "DPA",
|
||||
"lastModifiedTime": "2018-05-16 19:53:11"
|
||||
},
|
||||
{
|
||||
"userId": 630152,
|
||||
"planId": 0,
|
||||
"unitId": 2201545776,
|
||||
"creativeId": 171,
|
||||
"ideaId": 54009255238,
|
||||
"title": "闪投创意_测试aa",
|
||||
"brand": "闪投测试ff",
|
||||
"url": "http://www.baidu.com",
|
||||
"userPortrait": "http://fc-feed.cdn.bcebos.com/0/pic/e2a076e03f1624997b27eb5f0f8d69ff.jpgdd",
|
||||
"pictures": [
|
||||
{
|
||||
"pic": "http://bj.bcebos.com/fc-feed/0/pic/62c40ba3e297ea57c5b80f41e958b4f2.jpg"
|
||||
}
|
||||
],
|
||||
"signature": "10a208706d1c1bf4716d85e4e1ae5447",
|
||||
"auditResult": "",
|
||||
"status": 1,
|
||||
"type": "DPA",
|
||||
"lastModifiedTime": "2018-05-16 19:55:45"
|
||||
}
|
||||
]
|
||||
},
|
||||
"warning": null
|
||||
}
|
||||
}
|
||||
}
|
@ -89,7 +89,6 @@ export interface Action extends Button {
|
||||
| 'delete'
|
||||
| 'edit'
|
||||
| 'cancel'
|
||||
| 'close'
|
||||
| 'next'
|
||||
| 'prev'
|
||||
| 'reset';
|
||||
|
@ -168,9 +168,8 @@ function responseAdaptor(ret: fetcherResult) {
|
||||
if (!data) {
|
||||
throw new Error('Response is empty!');
|
||||
} else if (!data.hasOwnProperty('status')) {
|
||||
throw new Error(
|
||||
'接口返回格式不符合,请参考 http://amis.baidu.com/v2/docs/api'
|
||||
);
|
||||
// 兼容不返回 status 字段的情况
|
||||
data.status = 0;
|
||||
}
|
||||
|
||||
const payload: Payload = {
|
||||
@ -178,7 +177,7 @@ function responseAdaptor(ret: fetcherResult) {
|
||||
status: data.status,
|
||||
msg: data.msg,
|
||||
msgTimeout: data.msgTimeout,
|
||||
data: data.data
|
||||
data: data.data ? data.data : data // 兼容直接返回数据的情况
|
||||
};
|
||||
|
||||
if (payload.status == 422) {
|
||||
|
Loading…
Reference in New Issue
Block a user