let basicurl = "http://119.97.164.198:8080/"; const request = ( params = { url: null, method: "get", data: null, } ) => { if (!params.url) { throw error("url is not allowed be null!"); } if (promise) { return new promise((resolve, reject) => { const xml = new xmlhttprequest(); let url = basicurl + params.url; let datastring = null; if (params.data) { datastring = object.keys(params.data) .map((key) => `${key}=${json.stringify(params.data[key])}`) .join("&"); if (params.method.tolowercase() === "get") { url = url + "?" + datastring; } } xml.open(params.method, url, true); xml.setrequestheader("content-type", "application/json"); xml.send(json.stringify(params.data)); xml.onload = () => { if (xml.status === 200) { resolve(json.parse(xml.response)); } else { reject(json.parse(xml.response)); } }; xml.onerror = () => { reject("network connect is failed!"); }; }); } else { var resultobj = { then: function(fun) { const xml = new xmlhttprequest(); let url = basicurl + params.url; let datastring = null; if (params.data) { datastring = object.keys(params.data) .map((key) => `${key}=${json.stringify(params.data[key])}`) .join("&"); if (params.method.tolowercase() === "get") { url = url + "?" + datastring; } } xml.open(params.method, url, true); xml.setrequestheader("content-type", "application/json"); xml.send(json.stringify(params.data)); xml.onload = () => { if (xml.status === 200) { fun(json.parse(xml.response)); } else { throw error("abnormal state"); } }; xml.onerror = () => { throw error("network connect is failed!"); }; } } return resultobj; } }; /* 搜索用 */ const search_request = (url, data, bool = true) => { if (!url) { throw error("url is not allowed be null!"); } if (promise) { return new promise((resolve, reject) => { const xml = new xmlhttprequest(); xml.open("post", url, true); xml.setrequestheader("content-type", "application/json"); if (bool == true) xml.send(json.stringify({ title: data })); else xml.send(json.stringify({ keywords: data })); xml.onload = () => { if (xml.status === 200) { resolve(json.parse(xml.response)); } else { reject(json.parse(xml.response)); } }; xml.onerror = () => { reject("network connect is failed!"); }; }); } else { var resultobj = { then: function(fun) { const xml = new xmlhttprequest(); xml.open("post", url, true); xml.setrequestheader("content-type", "application/json"); if (bool == true) xml.send(json.stringify({ title: data })); else xml.send(json.stringify({ keywords: data })); xml.onload = () => { if (xml.status === 200) { fun(json.parse(xml.response)); } else { throw error("abnormal state"); } }; xml.onerror = () => { throw error("network connect is failed!"); }; } } return resultobj; } }; /* 最新版搜索 */ /* data 对象 参考接口文档 { title: "" } */ const max_search_request = ( argument = { url: null, data: {} } ) => { if (!argument.url) { throw error("url is not allowed be null!"); } if (promise) { return new promise((resolve, reject) => { let url = basicurl + argument.url; const xml = new xmlhttprequest(); xml.open("post", url, true); xml.setrequestheader("content-type", "application/json"); xml.send(json.stringify(argument.data)); xml.onload = () => { if (xml.status === 200) { resolve(json.parse(xml.response)); } else { reject(json.parse(xml.response)); } }; xml.onerror = () => { reject("network connect is failed!"); }; }); } else { var resultobj = { then: function(fun) { let url = basicurl + argument.url; const xml = new xmlhttprequest(); xml.open("post", url, true); xml.setrequestheader("content-type", "application/json"); xml.send(json.stringify(argument.data)); xml.onload = () => { if (xml.status === 200) { fun(json.parse(xml.response)); } else { throw error("abnormal state"); } }; xml.onerror = () => { throw error("network connect is failed!"); }; } } return resultobj; } };