HttpMgr.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. ***********************************************************************************
  3. * 作者:xxj
  4. * 功能:HTTP网络请求
  5. * 日期:2021.6.3
  6. * 备注:
  7. ***********************************************************************************
  8. */
  9. //import { LocalStorageManager } from "../../utils/LocalStorageManager";
  10. export class HttpMgr {
  11. // 单例对象
  12. private static _instance: HttpMgr = null;
  13. //private httpObj:Laya.HttpRequest;
  14. /**
  15. * 获取单例
  16. */
  17. public static getInstance(): HttpMgr {
  18. if (!this._instance) {
  19. this._instance = new HttpMgr();
  20. }
  21. return this._instance;
  22. }
  23. constructor() {
  24. HttpMgr._instance = this;
  25. }
  26. /**
  27. * post请求,参数待扩展
  28. */
  29. public sendPost(url, data, call) {
  30. let httpObj = new Laya.HttpRequest();
  31. httpObj.http.timeout = 5000;//设置超时时间;
  32. httpObj.once(Laya.Event.COMPLETE, this, completeHandler);
  33. httpObj.once(Laya.Event.ERROR, this, errorHandler);
  34. httpObj.send(url, data, 'post', 'text', ["Content-Type", "application/json; charset=UTF-8"]);
  35. function errorHandler(error): void {
  36. console.log("errorHandler:" + error);
  37. alert("Please check your network connection.");
  38. }
  39. function completeHandler(data): void {
  40. if (data && data.length > 0) {
  41. let jsonData = JSON.parse(data);
  42. if (call)
  43. call(jsonData);
  44. }
  45. }
  46. }
  47. private isDev: boolean = false;//是否是开发环境
  48. public getReqData() {
  49. let reqData = JSON.stringify({
  50. gameName: "TQ",
  51. gameUserName: "zhangSan",
  52. isDev: this.isDev
  53. });
  54. return reqData;
  55. }
  56. /**
  57. * post请求,参数待扩展
  58. */
  59. public sendPost2025(reqUrl, data, call) {
  60. let isDev = this.isDev;
  61. // let urlStr ="https://api.kessongame.site/prod-api/api/game/";
  62. //////////////http://api.kessongame.site/prod-api/api/game/getClassicCfg
  63. let urlTop = "https://api.kessongame.site/";
  64. let urlMid = urlTop + "prod-api/api/";
  65. if (isDev) {
  66. urlTop = "http://192.168.1.13:8090/";
  67. urlMid = urlTop + "api/";
  68. }
  69. let url = urlMid + reqUrl;
  70. if (reqUrl == "registerForApp" || reqUrl == "loginForApp") {
  71. if (isDev) {
  72. url = urlTop + reqUrl;
  73. } else {
  74. url = urlTop + "prod-api/" + reqUrl;
  75. }
  76. }
  77. let headers = ["Content-Type", "application/json; charset=UTF-8"];
  78. //---------------
  79. if (reqUrl.indexOf("/api/game/") >= 0) {//这里不需要鉴权
  80. }
  81. const keyForLoginInfo = "userInfo";
  82. const userInfoData = Laya.LocalStorage.getItem(keyForLoginInfo);
  83. if (userInfoData != null) {
  84. try {
  85. let bb = JSON.parse(userInfoData);
  86. if (bb) {
  87. let token = bb.token;
  88. headers.push("Authorization");
  89. headers.push(`Bearer ${token}`);
  90. }
  91. // const token = LocalStorageManager.getItem<{ username: string, token: string }>(keyForLoginInfo)?.token;
  92. } catch (e) {
  93. }
  94. }
  95. //---------------
  96. let httpObj = new Laya.HttpRequest();
  97. httpObj.http.timeout = 5000;
  98. httpObj.once(Laya.Event.COMPLETE, this, completeHandler);
  99. httpObj.once(Laya.Event.ERROR, this, errorHandler);
  100. // httpObj.send(url, data, 'post', 'text', ["Content-Type", "application/json; charset=UTF-8"]);
  101. httpObj.send(url, data, 'post', 'text', headers);
  102. console.log('zh: http url = ' + url)
  103. function errorHandler(error): void {
  104. if (call) {
  105. call("{'msg':'Network exception','code':500}");//固定
  106. } else {
  107. alert("Tip:Please check your network connection.");
  108. }
  109. }
  110. function completeHandler(data): void {
  111. if (data && data.length > 0) {
  112. let jsonData = JSON.parse(data);
  113. if (call)
  114. call(jsonData);
  115. }
  116. }
  117. }
  118. // getHeadInfo() {
  119. // let headers = ["Content-Type", "application/json; charset=UTF-8"];
  120. // const keyForLoginInfo = "loginInfo";
  121. // const token = LocalStorageManager.getItem<{ username: string, token: string }>(keyForLoginInfo)?.token;
  122. // if (token) {
  123. // headers.push("Authorization");
  124. // headers.push(`Bearer ${token}`);
  125. // }
  126. // return headers;
  127. // }
  128. public sendGet(url, call) {
  129. let httpObj = new Laya.HttpRequest();
  130. httpObj.http.timeout = 5000;//设置超时时间;
  131. httpObj.once(Laya.Event.COMPLETE, this, completeHandler);
  132. httpObj.once(Laya.Event.ERROR, this, errorHandler);
  133. httpObj.send(url, '', 'get');
  134. function errorHandler(error): void {
  135. console.log("errorHandler:" + error);
  136. //UIMgr.getInstance().showTip("请求失败,请稍后重试");
  137. //call(null);
  138. }
  139. function completeHandler(data): void {
  140. if (data && data.length > 0) {
  141. //let jsonData = JSON.parse(data);
  142. if (call)
  143. call(data);
  144. }
  145. }
  146. }
  147. }