import config from "../config/index.js"; function getdata(data) { let text = ""; for (const key in data) { text += key + "=" + data[key] + "&"; } text ? (text = "?" + text) : ""; text = text.replace(/&$/, ""); return text; } export default function (ori) { let baseurl = config.base[ori.urlType || "defaultURl"]; let url = baseurl + ori.url + getdata(ori.data || {}); return new Promise((resolve, reject) => { var xhttp; if (window.XMLHttpRequest) xhttp = new XMLHttpRequest(); else if (window.ActiveXObject) xhttp = new window.ActiveXObject("Microsoft.XMLHTTP"); let method = ori.method.toUpperCase(); xhttp.open(method, url, true); xhttp.setRequestHeader("Content-type", "application/json"); method === "GET" ? xhttp.send() : xhttp.send(JSON.stringify(ori.data)); xhttp.onreadystatechange = function () { if (this.readyState != 4 || this.status != 200) return; let data = {}; try { data = this.responseText !== "null" ? JSON.parse(this.responseText || "{}") : {}; } catch (err) { console.error(err); reject(err); } if (data.code === 0) resolve(data.data); else reject(data); }; }); }