mirror of
https://gitee.com/baidu/amis.git
synced 2024-11-29 18:39:05 +08:00
chore: jssdk 的 responseAdaptor 扩充参数,支持拿 response 整个对象,包括返回的 headers 和… (#4229)
* chore: jssdk 的 responseAdpator 扩充参数,支持拿 response 整个对象,包括返回的 headers 和状态码信息 * chore: jssdk 的 responseAdpator 扩充参数,支持拿 response 整个对象,包括返回的 headers 和状态码信息
This commit is contained in:
parent
0e317b8f97
commit
773700cf68
@ -156,8 +156,8 @@ let amisScoped = amis.embed(
|
||||
//
|
||||
// 全局 api 适配器。
|
||||
// 另外在 amis 配置项中的 api 也可以配置适配器,针对某个特定接口单独处理。
|
||||
// responseAdaptor(api, response, query, request) {
|
||||
// return response;
|
||||
// responseAdaptor(api, payload, query, request, response) {
|
||||
// return payload;
|
||||
// }
|
||||
//
|
||||
// 用来接管页面跳转,比如用 location.href 或 window.open,或者自己实现 amis 配置更新
|
||||
|
@ -58,35 +58,35 @@ export function embed(
|
||||
return request;
|
||||
};
|
||||
|
||||
const responseAdaptor = (api: any) => (value: any) => {
|
||||
let response = value.data || {}; // blob 下可能会返回内容为空?
|
||||
const responseAdaptor = (api: any) => (response: any) => {
|
||||
let payload = response.data || {}; // blob 下可能会返回内容为空?
|
||||
// 之前拼写错了,需要兼容
|
||||
if (env && env.responseAdpater) {
|
||||
env.responseAdaptor = env.responseAdpater;
|
||||
}
|
||||
if (env && env.responseAdaptor) {
|
||||
const url = api.url;
|
||||
const idx = api.url.indexOf('?');
|
||||
const query = ~idx ? qs.parse(api.url.substring(idx)) : {};
|
||||
const idx = url.indexOf('?');
|
||||
const query = ~idx ? qs.parse(url.substring(idx)) : {};
|
||||
const request = {
|
||||
...api,
|
||||
query: query,
|
||||
body: api.data
|
||||
};
|
||||
response = env.responseAdaptor(api, response, query, request);
|
||||
payload = env.responseAdaptor(api, payload, query, request, response);
|
||||
} else {
|
||||
if (response.hasOwnProperty('errno')) {
|
||||
response.status = response.errno;
|
||||
response.msg = response.errmsg;
|
||||
} else if (response.hasOwnProperty('no')) {
|
||||
response.status = response.no;
|
||||
response.msg = response.error;
|
||||
if (payload.hasOwnProperty('errno')) {
|
||||
payload.status = payload.errno;
|
||||
payload.msg = payload.errmsg;
|
||||
} else if (payload.hasOwnProperty('no')) {
|
||||
payload.status = payload.no;
|
||||
payload.msg = payload.error;
|
||||
}
|
||||
}
|
||||
|
||||
const result = {
|
||||
...value,
|
||||
data: response
|
||||
...response,
|
||||
data: payload
|
||||
};
|
||||
return result;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user