export const baseUrl = “”;
// 拼接get参数
export const getParamsUrl = (url, params) => {
let paramsArray = [];
Object.keys(params).forEach(key => paramsArray.push(key + “=” + params[key]));
if (url.search(/?/) === -1) {
url += “?” + paramsArray.join("&");
} else {
url += “&” + paramsArray.join("&");
}
return url;
};
export const getData = async (url, obj) => {
url = obj ? getParamsUrl(url, obj) : url;
let res = await fetch(baseUrl + url, { method: “GET” });
let json = res.json();
return json;
};
export const postData = async (url, obj) => {
let res = await fetch(baseUrl + url, {
method: “POST”,
headers: {
Accept: “application/json”,
“Content-Type”: “application/json”
},
body: JSON.stringify(obj)
});
let json = await res.json();
return json;
};