123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- /*
- ***********************************************************************************
- * 作者: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 = true;//是否是开发环境
- // 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);
- }
- }
- }
- }
|