2024-03-19 14:29:00 +08:00
|
|
|
import { WeaLoadingGlobal } from "ecCom";
|
|
|
|
|
|
2023-11-06 09:24:47 +08:00
|
|
|
const server = window.server || "";
|
2023-10-11 16:23:55 +08:00
|
|
|
export const formHeaderPost = (url, method, params, header) => {
|
2023-11-06 09:24:47 +08:00
|
|
|
if (typeof localStorage.access_token === "string" && localStorage.access_token !== "") {
|
|
|
|
|
params.access_token = localStorage.access_token;
|
|
|
|
|
}
|
|
|
|
|
url = server + url + "?__random__=" + (new Date()).valueOf();
|
2023-10-11 16:23:55 +08:00
|
|
|
let formdata = new URLSearchParams();
|
|
|
|
|
Object.keys(params).map(key => {
|
|
|
|
|
formdata.append(key, params[key]);
|
|
|
|
|
});
|
|
|
|
|
return fetch(url, {
|
|
|
|
|
method, mode: "cors",
|
|
|
|
|
headers: { "Content-Type": "application/x-www-form-urlencoded", ...header },
|
|
|
|
|
body: formdata
|
|
|
|
|
}).then(res => res.json());
|
|
|
|
|
};
|
2025-04-25 13:38:43 +08:00
|
|
|
export const postFetch = (url, params, header = {}) => {
|
2023-11-06 09:24:47 +08:00
|
|
|
if (typeof localStorage.access_token === "string" && localStorage.access_token !== "") {
|
|
|
|
|
params.access_token = localStorage.access_token;
|
|
|
|
|
}
|
|
|
|
|
url = server + url + "?__random__=" + (new Date()).valueOf();
|
2025-04-25 13:38:43 +08:00
|
|
|
return fetch(url, getFetchParams("POST", params, header)).then(res => res.json());
|
2023-10-11 16:23:55 +08:00
|
|
|
};
|
|
|
|
|
export const headerRequestFetch = (url, method, params, header) => {
|
2023-11-06 09:24:47 +08:00
|
|
|
if (typeof localStorage.access_token === "string" && localStorage.access_token !== "") {
|
|
|
|
|
params.access_token = localStorage.access_token;
|
|
|
|
|
}
|
|
|
|
|
url = server + url + "?__random__=" + (new Date()).valueOf();
|
|
|
|
|
return fetch(url, getFetchParams(method, params, header)).then(res => res.json());
|
2023-10-11 16:23:55 +08:00
|
|
|
};
|
2023-10-12 15:52:15 +08:00
|
|
|
export const postExportFetch = (url, params) => {
|
2023-11-06 09:24:47 +08:00
|
|
|
if (typeof localStorage.access_token === "string" && localStorage.access_token !== "") {
|
|
|
|
|
params.access_token = localStorage.access_token;
|
|
|
|
|
}
|
|
|
|
|
url = server + url + "?__random__=" + (new Date()).valueOf();
|
2023-12-19 16:18:44 +08:00
|
|
|
return fetch(url, getFetchParams("POST", params)).then(res => {
|
2024-03-19 14:29:00 +08:00
|
|
|
WeaLoadingGlobal.destroy();
|
2023-10-12 15:52:15 +08:00
|
|
|
const filename = res.headers.get("Content-Disposition").split("filename=")[1];
|
|
|
|
|
res.blob().then(blob => {
|
|
|
|
|
const url = window.URL.createObjectURL(blob);
|
|
|
|
|
const a = document.createElement("a");
|
|
|
|
|
a.href = url;
|
|
|
|
|
a.download = decodeURI(filename);
|
|
|
|
|
a.click();
|
|
|
|
|
window.URL.revokeObjectURL(url);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
2023-11-06 09:24:47 +08:00
|
|
|
export const getFd = (values) => {
|
|
|
|
|
let fd = {};
|
|
|
|
|
for (let p in values) {
|
|
|
|
|
values[p] = _.isNil(values[p]) ? "" : values[p];
|
|
|
|
|
let item = values[p];
|
|
|
|
|
if (window.E9Encrypt && __AESEcrypt__) item = __AESEcrypt__.aes_data_encrypt(item);
|
|
|
|
|
fd = { ...fd, [p]: item };
|
|
|
|
|
}
|
|
|
|
|
if (window.E9Encrypt && __AESEcrypt__) {
|
|
|
|
|
fd = { ...fd, rsaAes01: __AESEcrypt__.get_rsa_aes_01(), rsaAes02: __AESEcrypt__.get_rsa_aes_02() };
|
|
|
|
|
}
|
|
|
|
|
return fd;
|
|
|
|
|
};
|
|
|
|
|
const getFetchParams = (method, params, header = {}) => {
|
|
|
|
|
let obj = {
|
|
|
|
|
method, mode: "cors",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
"X-Requested-With": "XMLHttpRequest",
|
|
|
|
|
...header
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
if (server === "") {
|
|
|
|
|
obj.credentials = "include";
|
|
|
|
|
}
|
2023-11-07 11:26:08 +08:00
|
|
|
if (!_.isEmpty(params) && Object.prototype.toString.call(params) === "[object Object]") {
|
2023-11-06 09:24:47 +08:00
|
|
|
obj.body = JSON.stringify(getFd(params));
|
|
|
|
|
} else {
|
|
|
|
|
obj.body = JSON.stringify(params);
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
};
|
2023-10-12 15:52:15 +08:00
|
|
|
|