/* *********************************************************************************** * 作者:xxj * 功能:HTTP网络请求 * 日期:2021.6.3 * 备注: *********************************************************************************** */ export class HttpMgr { // 单例对象 private static _instance: HttpMgr = null; //private httpObj:Laya.HttpRequest; /** * 获取单例 */ public static getInstance(): HttpMgr { if (!this._instance) { this._instance = new HttpMgr(); } return this._instance; } constructor() { HttpMgr._instance = this; } /** * post请求,参数待扩展 */ public sendPost(url, data, call) { let httpObj = new Laya.HttpRequest(); httpObj.http.timeout = 5000;//设置超时时间; httpObj.once(Laya.Event.COMPLETE, this, completeHandler); httpObj.once(Laya.Event.ERROR, this, errorHandler); httpObj.send(url, data, 'post', 'text', ["Content-Type", "application/json; charset=UTF-8"]); function errorHandler(error): void { console.log("errorHandler:" + error); alert("Please check your network connection."); } function completeHandler(data): void { if (data && data.length > 0) { let jsonData = JSON.parse(data); if (call) call(jsonData); } } } /** * post请求,参数待扩展 */ public sendPost2025(reqUrl, data, call) { let isDev = false;//是否是开发环境 // let urlStr ="https://api.kessongame.site/prod-api/api/game/"; //////////////https://api.kessongame.site/prod-api/api/game/getClassicCfg let urlTop = "https://api.kessongame.site/"; let urlMid = urlTop + "prod-api/api/game/"; if (isDev) { urlTop = "http://192.168.1.13:8090/"; urlMid = urlTop + "api/game/"; } let url = urlMid + reqUrl; if (reqUrl == "registerForApp" || reqUrl == "loginForApp") { if (isDev) { url = urlTop + reqUrl; } else { url = urlTop + "prod-api/" + reqUrl; } } let httpObj = new Laya.HttpRequest(); httpObj.http.timeout = 5000;//设置超时时间; httpObj.once(Laya.Event.COMPLETE, this, completeHandler); httpObj.once(Laya.Event.ERROR, this, errorHandler); httpObj.send(url, data, 'post', 'text', ["Content-Type", "application/json; charset=UTF-8"]); function errorHandler(error): void { if (call) { call("{'msg':'Network exception','code':500}");//固定 } else { alert("Tip:Please check your network connection."); } } function completeHandler(data): void { if (data && data.length > 0) { let jsonData = JSON.parse(data); if (call) call(jsonData); } } } public sendGet(url, call) { let httpObj = new Laya.HttpRequest(); httpObj.http.timeout = 5000;//设置超时时间; httpObj.once(Laya.Event.COMPLETE, this, completeHandler); httpObj.once(Laya.Event.ERROR, this, errorHandler); httpObj.send(url, '', 'get'); function errorHandler(error): void { console.log("errorHandler:" + error); //UIMgr.getInstance().showTip("请求失败,请稍后重试"); //call(null); } function completeHandler(data): void { if (data && data.length > 0) { //let jsonData = JSON.parse(data); if (call) call(data); } } } }