HttpMgr.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. ***********************************************************************************
  3. * 作者:xxj
  4. * 功能:HTTP网络请求
  5. * 日期:2021.6.3
  6. * 备注:
  7. ***********************************************************************************
  8. */
  9. export class HttpMgr {
  10. // 单例对象
  11. private static _instance: HttpMgr = null;
  12. //private httpObj:Laya.HttpRequest;
  13. /**
  14. * 获取单例
  15. */
  16. public static getInstance(): HttpMgr {
  17. if (!this._instance) {
  18. this._instance = new HttpMgr();
  19. }
  20. return this._instance;
  21. }
  22. constructor() {
  23. HttpMgr._instance = this;
  24. }
  25. /**
  26. * post请求,参数待扩展
  27. */
  28. public sendPost(url, data, call) {
  29. let httpObj = new Laya.HttpRequest();
  30. httpObj.http.timeout = 5000;//设置超时时间;
  31. httpObj.once(Laya.Event.COMPLETE, this, completeHandler);
  32. httpObj.once(Laya.Event.ERROR, this, errorHandler);
  33. httpObj.send(url, data, 'post', 'text', ["Content-Type", "application/json; charset=UTF-8"]);
  34. function errorHandler(error): void {
  35. console.log("errorHandler:" + error);
  36. alert("Please check your network connection.");
  37. }
  38. function completeHandler(data): void {
  39. if (data && data.length > 0) {
  40. let jsonData = JSON.parse(data);
  41. if (call)
  42. call(jsonData);
  43. }
  44. }
  45. }
  46. /**
  47. * post请求,参数待扩展
  48. */
  49. public sendPost2025(reqUrl, data, call) {
  50. let isDev = true;//是否是开发环境
  51. // let urlStr ="https://api.kessongame.site/prod-api/api/game/";
  52. //////////////https://api.kessongame.site/prod-api/api/game/getClassicCfg
  53. let urlTop = "https://api.kessongame.site/";
  54. let urlMid = urlTop + "prod-api/api/game/";
  55. if (isDev) {
  56. urlTop = "http://192.168.1.13:8090/";
  57. urlMid = urlTop + "api/game/";
  58. }
  59. let url = urlMid + reqUrl;
  60. if (reqUrl == "registerForApp" || reqUrl == "loginForApp") {
  61. if (isDev) {
  62. url = urlTop + reqUrl;
  63. } else {
  64. url = urlTop + "prod-api/" + reqUrl;
  65. }
  66. }
  67. let httpObj = new Laya.HttpRequest();
  68. httpObj.http.timeout = 5000;//设置超时时间;
  69. httpObj.once(Laya.Event.COMPLETE, this, completeHandler);
  70. httpObj.once(Laya.Event.ERROR, this, errorHandler);
  71. httpObj.send(url, data, 'post', 'text', ["Content-Type", "application/json; charset=UTF-8"]);
  72. function errorHandler(error): void {
  73. if (call) {
  74. call("{'msg':'Network exception','code':500}");//固定
  75. } else {
  76. alert("Tip:Please check your network connection.");
  77. }
  78. }
  79. function completeHandler(data): void {
  80. if (data && data.length > 0) {
  81. let jsonData = JSON.parse(data);
  82. if (call)
  83. call(jsonData);
  84. }
  85. }
  86. }
  87. public sendGet(url, call) {
  88. let httpObj = new Laya.HttpRequest();
  89. httpObj.http.timeout = 5000;//设置超时时间;
  90. httpObj.once(Laya.Event.COMPLETE, this, completeHandler);
  91. httpObj.once(Laya.Event.ERROR, this, errorHandler);
  92. httpObj.send(url, '', 'get');
  93. function errorHandler(error): void {
  94. console.log("errorHandler:" + error);
  95. //UIMgr.getInstance().showTip("请求失败,请稍后重试");
  96. //call(null);
  97. }
  98. function completeHandler(data): void {
  99. if (data && data.length > 0) {
  100. //let jsonData = JSON.parse(data);
  101. if (call)
  102. call(data);
  103. }
  104. }
  105. }
  106. }