HttpMgr.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. public getReqData() {
  47. let reqData = JSON.stringify({
  48. gameName: "TQ",
  49. gameUserName: "zhangSan"
  50. });
  51. return reqData;
  52. }
  53. /**
  54. * post请求,参数待扩展
  55. */
  56. public sendPost2025(reqUrl, data, call) {
  57. let isDev = true;//是否是开发环境
  58. // let urlStr ="https://api.kessongame.site/prod-api/api/game/";
  59. //////////////https://api.kessongame.site/prod-api/api/game/getClassicCfg
  60. let urlTop = "https://api.kessongame.site/";
  61. let urlMid = urlTop + "prod-api/api/game/";
  62. if (isDev) {
  63. urlTop = "http://192.168.1.13:8090/";
  64. urlMid = urlTop + "api/game/";
  65. }
  66. let url = urlMid + reqUrl;
  67. if (reqUrl == "registerForApp" || reqUrl == "loginForApp") {
  68. if (isDev) {
  69. url = urlTop + reqUrl;
  70. } else {
  71. url = urlTop + "prod-api/" + reqUrl;
  72. }
  73. }
  74. let httpObj = new Laya.HttpRequest();
  75. httpObj.http.timeout = 5000;//设置超时时间;
  76. httpObj.once(Laya.Event.COMPLETE, this, completeHandler);
  77. httpObj.once(Laya.Event.ERROR, this, errorHandler);
  78. httpObj.send(url, data, 'post', 'text', ["Content-Type", "application/json; charset=UTF-8"]);
  79. function errorHandler(error): void {
  80. if (call) {
  81. call("{'msg':'Network exception','code':500}");//固定
  82. } else {
  83. alert("Tip:Please check your network connection.");
  84. }
  85. }
  86. function completeHandler(data): void {
  87. if (data && data.length > 0) {
  88. let jsonData = JSON.parse(data);
  89. if (call)
  90. call(jsonData);
  91. }
  92. }
  93. }
  94. public sendGet(url, call) {
  95. let httpObj = new Laya.HttpRequest();
  96. httpObj.http.timeout = 5000;//设置超时时间;
  97. httpObj.once(Laya.Event.COMPLETE, this, completeHandler);
  98. httpObj.once(Laya.Event.ERROR, this, errorHandler);
  99. httpObj.send(url, '', 'get');
  100. function errorHandler(error): void {
  101. console.log("errorHandler:" + error);
  102. //UIMgr.getInstance().showTip("请求失败,请稍后重试");
  103. //call(null);
  104. }
  105. function completeHandler(data): void {
  106. if (data && data.length > 0) {
  107. //let jsonData = JSON.parse(data);
  108. if (call)
  109. call(data);
  110. }
  111. }
  112. }
  113. }