/** * Created by Lemon on 17/5/8. */ const TAG_REQUEST_UTIL = 'RequestUtil'; const URL_BASE = "http://apijson.cn:8080"; // åºå°å const URL_GET = URL_BASE + "/get"; // 常è§è·åæ°æ®æ¹å¼ const URL_HEAD = URL_BASE + "/head"; // æ£æ¥ï¼é»è®¤æ¯éç©ºæ£æ¥ï¼è¿åæ°æ®æ»æ° const URL_GETS = URL_BASE + "/gets"; // éè¿POSTæ¥GETæ°æ®ï¼ä¸æ¾ç¤ºè¯·æ±å 容åè¿åç»æï¼ä¸è¬ç¨äºå¯¹å®å ¨è¦æ±æ¯è¾é«çè¯·æ± const URL_HEADS = URL_BASE + "/heads"; // éè¿POSTæ¥HEADæ°æ®ï¼ä¸æ¾ç¤ºè¯·æ±å 容åè¿åç»æï¼ä¸è¬ç¨äºå¯¹å®å ¨è¦æ±æ¯è¾é«çè¯·æ± const URL_POST = URL_BASE + "/post"; // æ°å¢(æè 说æå ¥)æ°æ® const URL_PUT = URL_BASE + "/put"; // ä¿®æ¹æ°æ®ï¼åªä¿®æ¹ä¼ å ¥åæ®µå¯¹åºçå¼ const URL_DELETE = URL_BASE + "/delete"; // å 餿°æ® /**请æ±ï¼å ¨èµ°HTTP POST * @param url * @param rq */ function request(url, json, notAlertRequest, onreadystatechange) { if (json == null || json instanceof Array || (json instanceof Object) == false) { alertOfDebug("request json == null || json instanceof Array || (json instanceof Object) == false !!! >> return null;"); return null; } if (url == null || (typeof url != "string")) { alertOfDebug("request url == null || typeof url != string !!! >> return null;"); return null; } if (url.length < 3 || url.indexOf(".") < 0) { alertOfDebug("request url.length < 3 || url.indexOf(.) < 0 !!! >> return null;"); return null; } var rqf = format(JSON.stringify(json)); var branch = url.substring(URL_BASE.length + 1, url.length); var end = branch.indexOf("/"); var method = branch.substring(0, end < 0 ? branch.length : end); var METHOD = method.toUpperCase(); if (! notAlertRequest) { alert("Request(" + METHOD + "):\n" + rqf); } var request = new XMLHttpRequest(); request.open("POST", url, true); request.setRequestHeader("Content-type", "application/json"); request.onreadystatechange = onreadystatechange != null ? onreadystatechange : function () { if (request.readyState !== 4) { return; } if (request.status === 200) { alert("Response(" + METHOD + "):\n" + format(request.responseText)); } else { alert("Response(" + METHOD + "):\nstatus" + request.status + "\nerror:" + request.error); } } request.send(rqf); //åç请æ±>>>>>>>>>>>>>>>>>>>>>>>>>> //JQuery ajax请æ±<<<<<<<<<<<<<<<<<<<<<<<<<< // $.ajax({ // type: isGet ? "GET" : "POST", // url: isGet ? url + "/" + rq : url, // contentType: "application/json", //å¿ é¡» // dataType: "json", //è¿åå¼ç±»åï¼éå¿ é¡» // data: isGet ? null : rq, // success: function (response) { // alertOfDebug(response); // } // }); //JQuery ajax请æ±>>>>>>>>>>>>>>>>>>>>>>>>>> //VUE axios请æ±<<<<<<<<<<<<<<<<<<<<<<<<<< // if (isGet) { // axios.get(url + "/" + rq, null) // .then(function (response) { // console.log(response); // }) // .catch(function (error) { // console.log(error); // }); // } else { // axios({ // method: 'post', // url: url + "/", // data: json // }).then(function (response) { // alertOfDebug(response); // }).catch(function (error) { // alertOfDebug(error); // }); // } //VUE axios请æ±>>>>>>>>>>>>>>>>>>>>>>>>>> return request; } /**ç¼ç JSONï¼è½¬ä¹ææString * @param json ä»»æç±»å */ function encode(json) { // alertOfDebug("encode before:\n" + format(JSON.stringify(json))); if (typeof json == "string") { //json instanceof String) { json = encodeURIComponent(json); } else if (json instanceof Array) { // alertOfDebug("encode json instanceof Array"); for (var i = 0; i < json.length; i ++) { // alertOfDebug("json[" + i + "] = " + format(JSON.stringify(json[i]))); json[i] = encode(json[i]); } } else if (json instanceof Object) { // alertOfDebug("encode json instanceof Object"); for (var key in json) { // alertOfDebug("encode json[" + key + "] = " + format(JSON.stringify(json[key]))); json[key] = encode(json[key]); } } // alertOfDebug("encode after:\n" + format(JSON.stringify(json))); return json; } /**æ ¼å¼åJSON串 * @param json */ function format(json) { try { return JSON.stringify(JSON.parse(json), null, "\t"); } catch(e) { log(TAG_REQUEST_UTIL, 'format try { ... } catch (err) { \n ' + e); return json; } // å¯¼è´æ ¼å¼åå代ç å¾é¾çï¼åæ²¡æ ¼å¼å䏿 · // if (json == null || json == '') { // console.log('format json == null || json == "" >> return json;'); // return json; // } // // if (json instanceof Object) { //é¿å èµå¼å½±åä¼ è¿æ¥çjson // return JSON.stringify(json, null, "\t"); // } // // var jsonObj; // if (typeof json == 'string'){ // try { // jsonObj = JSON.parse(json); // } catch (err) { // console.log('format try { jsonObj = JSON.parse(json); } catch (err) { \n ' + err); // return json; // } // } // else { // console.log('format json type error !'); // return json; // } // return JSON.stringify(jsonObj, null, "\t"); } function log(tag, msg) { console.log(tag + '.' + msg); } /**å°jsonå符串转为JSON对象 * @param s */ function parseJSON(s) { if (s instanceof Object) { alertOfDebug("parseJSON s instanceof JSON >> return s;"); return s; } if (typeof s != "string") { alertOfDebug("parseJSON typeof json != string >> s = \"\" + s;"); s = "" + s; } // alertOfDebug("parseJSON s = \n" + s); return JSON.parse(s); } /**æµè¯ç¨çæç¤º * @param s */ function alertOfDebug(s) { // alert(s); //注éæå°±é½ä¸ä¼å¼¹çªäº } /**æ¯å¦ä¸ºç©º * @param s * @returns {boolean} */ function isEmpty(s) { return s == null || s.trim() == ''; } //常ç¨è¯·æ±<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**è·åå个对象 * @param table String, 对象åï¼å¦ "User" * @param id Long, 对象idï¼å¦ 1 * @param notAlert ä¸å¼¹çªæ¾ç¤º * @param callBack è¯·æ±æååè° */ function getObject(table, id, notAlert, callBack) { alertOfDebug("getObject table = " + table + "; id = " + id); return request(URL_GET, newSingleJSON(table, { "id": id }, null), notAlert, callBack); } /**è·åæ°ç» * @param table String, 对象åï¼å¦ "User" * @param json {}, 对象å 容ï¼å¦ {"sex":1} * @param count int, æ¯é¡µæ°é * @param page int, 页ç * @param notAlert ä¸å¼¹çªæ¾ç¤º * @param callBack è¯·æ±æååè° */ function getArray(table, json, count, page, notAlert, callBack) { alertOfDebug("getArray table = " + table + "; count = " + count + "; page = " + page + "; json = \n" + format(json)); return request(URL_GET, newArrayJSON(table, json, count, page), notAlert, callBack); } /**æ°å¢å个对象 * @param table String, 对象åï¼å¦ "User" * @param json {}, 对象å 容ï¼å¦ {"sex":1} * @param notAlert ä¸å¼¹çªæ¾ç¤º * @param callBack è¯·æ±æååè° */ function postObject(table, json, notAlert, callBack) { alertOfDebug("postObject table = " + table + "; json = \n" + format(json)); var id = json == null ? null : json['id']; if (id != null) { alertOfDebug('postObject POST 请æ±ä¸è½è®¾ç½® id ï¼'); return; } return request(URL_POST, newSingleJSON(table, json, table), notAlert, callBack); } /**ä¿®æ¹å个对象 * @param table String, 对象åï¼å¦ "User" * @param json {}, 对象å 容ï¼å¦ {"sex":1} * @param notAlert ä¸å¼¹çªæ¾ç¤º * @param callBack è¯·æ±æååè° */ function putObject(table, json, notAlert, callBack) { alertOfDebug("putObject table = " + table + "; json = \n" + format(json)); if (json == null) { alertOfDebug('putObject PUT 请æ±å¿ 须设置 table对象 ï¼'); return; } if (json["id"] == null || json["id"] <= 0) { alertOfDebug('putObject PUT 请æ±å¿ 须设置 id ä¸ id > 0 ï¼'); return; } return request(URL_PUT, newSingleJSON(table, json, table), notAlert, callBack); } /**å é¤å个对象 * @param table String, 对象åï¼å¦ "User" * @param id Long, 对象idï¼å¦ 1 * @param notAlert ä¸å¼¹çªæ¾ç¤º * @param callBack è¯·æ±æååè° */ function deleteObject(table, id, notAlert, callBack) { alertOfDebug("deleteObject table = " + table + "; id = " + id); if (id == null || id <= 0) { alertOfDebug('deleteObject DELETE 请æ±å¿ 须设置 id ä¸ id > 0 ï¼'); return; } return request(URL_DELETE, newSingleJSON(table, { "id": id }, table), notAlert, callBack); } /**ä¿®æ¹å¤ä¸ªå¯¹è±¡ * @param table String, 对象åï¼å¦ "User" * @param json {}, 对象å 容ï¼å¦ {"sex":1} * @param notAlert ä¸å¼¹çªæ¾ç¤º * @param callBack è¯·æ±æååè° */ function putArray(table, json, notAlert, callBack) { alertOfDebug("putArray table = " + table + "; json = \n" + format(json)); var idArray = json == null ? null : json["id{}"]; if (idArray == null || (idArray instanceof Array) == false) { alertOfDebug('putArray idArray == null || (idArray instanceof Array) == false' + ' >> return; PUT 请æ±å¿ 须设置 id{}:[] ï¼'); return; } return request(URL_PUT, newSingleJSON(table, json, table + "[]"), notAlert, callBack); } /**å é¤å¤ä¸ªå¯¹è±¡ * @param table String, 对象åï¼å¦ "User" * @param idArray [], 对象idæ°ç»ï¼å¦ [1, 2, 3] * @param notAlert ä¸å¼¹çªæ¾ç¤º * @param callBack è¯·æ±æååè° */ function deleteArray(table, idArray, notAlert, callBack) { alertOfDebug("deleteArray table = " + table + "; idArray = \n" + format(idArray)); if (idArray == null || (idArray instanceof Array) == false) { alertOfDebug('deleteArray idArray == null ! DELETE 请æ±å¿ 须设置 id{}:[] ï¼'); return; } return request(URL_DELETE, newSingleJSON(table, { "id{}": idArray }, table + "[]"), notAlert, callBack); } /**æ°å»ºåä¸ªå¯¹è±¡è¯·æ± * @param table String, 对象åï¼å¦ "User" * @param json {}, 对象å 容ï¼å¦ {"sex":1} * @param tag String, åæä½æ è¯ï¼ä¸è¬æ¥è¯´ï¼æä½å个对象æ¶åtableç¸åï¼æä½å¤ä¸ªå¯¹è±¡æ¶æ¯ table[] */ function newSingleJSON(table, json, tag) { // alertOfDebug("newSingleJSON table = " + table + "; tag = " + tag // + "; json = \n" + format(json)); return parseJSON(newSingleString(table, json, tag)); } /**æ°å»ºæ°ç»è¯·æ± * @param table String, 对象åï¼å¦ "User" * @param json {}, 对象å 容ï¼å¦ {"sex":1} * @param count int, æ¯é¡µæ°é * @param page int, 页ç */ function newArrayJSON(table, json, count, page) { // alertOfDebug("newArrayJSON table = " + table + "; count = " + count + "; page = " + page // + "; json = \n" + format(json)); return parseJSON(newArrayString(table, json, count, page)); } /**æ°å»ºåä¸ªå¯¹è±¡è¯·æ± * @param table String, 对象åï¼å¦ "User" * @param json {}, 对象å 容ï¼å¦ {"sex":1} * @param tag String, åæä½æ è¯ï¼ä¸è¬æ¥è¯´ï¼æä½å个对象æ¶åtableç¸åï¼æä½å¤ä¸ªå¯¹è±¡æ¶æ¯ table[] */ function newSingleString(table, json, tag) { if (json == null) { alertOfDebug('newSingleString json == null >> return;'); return; } // alertOfDebug("newSingleString table = " + table + "; tag = " + tag // + "; json = \n" + format(json)); return "{\"" + table + "\":" + JSON.stringify(json) + (isEmpty(tag) ? "" : ",\"tag\":\"" + tag + "\"") + "}"; } /**æ°å»ºæ°ç»è¯·æ± * @param table String, 对象åï¼å¦ "User" * @param json {}, 对象å 容ï¼å¦ {"sex":1} * @param count int, æ¯é¡µæ°é * @param page int, 页ç */ function newArrayString(table, json, count, page) { if (json == null) { alertOfDebug('newArrayString json == null >> return;'); return; } // alertOfDebug("newArrayString table = " + table + "; count = " + count + "; page = " + page // + "; json = \n" + format(json)); return "{\"" + table + "[]\":{" + "\"count\":" + count + "," + "\"page\":" + page + ",\"" + table + "\":" + JSON.stringify(json) + "}}"; } //常ç¨è¯·æ±>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>