export const callApi = (
url,
method = "GET",
_params = {},
type = "json",
deep = false
) => {
return new Promise((resolve, reject) => {
fetch(url, getFetchParams(method, params, deep))
.then(function(response) {
return response.text().then(data => {
if (type === "json") {
try {
return JSON.parse(data);
} catch (error) {
data = data || "{}";
return evil("(" + data + ")"); // JSON.parse 解析反斜杠报错处理
}
} else {
return data;
}
});
})
.then(data => {
//weaHistory
if (
type === "json" &&
data &&
(data.status === "false" || !data.status) &&
data.showType === "url" &&
data.url
) {
setTimeout(() => (window.location.href = data.url), 1000);
resolve(data);
} else if (
type === "json" &&
data &&
(data.status === "false" || !data.status) &&
data.showType === "auth"
) {
if (window.weaHistory) {
let pathname = "/main/com/auth";
if (window.location.href.indexOf("/wui/engine.html") > -1) {
pathname = "/com/auth";
}
window.weaHistory.push({
pathname: pathname
});
}
resolve(data);
} else if (
type === "json" &&
data &&
(data.status === "false" || !data.status) &&
(data.showMsg || data.showType === "msg")
) {
data.error && message.warn(data.error);
resolve(data);
} else if (
type === "json" &&
data &&
(data.status === "false" || !data.status) &&
data.showType === "none"
) {
resolve(data);
} else if (
type === "json" &&
data &&
(data.status === "false" || !data.status) &&
(data.showModal || data.showType === "modal")
) {
data.error &&
Modal.warning({
title: data.title || "消息提醒",
content: data.error
});
resolve(data);
} else if (type === "json" && checkReject(data)) {
console &&
console.error(`${url} fetch 请求异常: ${data.error || "error"}`);
reject(`${url} fetch 请求异常: ${data.error || "error"}`, data);
} else {
resolve(data);
}
})
.catch(function(ex) {
console &&
console.error(`${url} fetch 数据处理异常: ${ex}, params:`, params);
reject(`${url} fetch 数据处理异常: ${ex}`);
});
});
};
console