HttpMgr.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. public getReqData() {
  48. let reqData = JSON.stringify({
  49. gameName: "TQ",
  50. gameUserName: "zhangSan"
  51. });
  52. return reqData;
  53. }
  54. /**
  55. * post请求,参数待扩展
  56. */
  57. public sendPost2025(reqUrl, data, call) {
  58. let isDev = false;//是否是开发环境
  59. // let urlStr ="https://api.kessongame.site/prod-api/api/game/";
  60. //////////////https://api.kessongame.site/prod-api/api/game/getClassicCfg
  61. let urlTop = "https://api.kessongame.site/";
  62. let urlMid = urlTop + "prod-api/api/";
  63. if (isDev) {
  64. urlTop = "http://192.168.1.13:8090/";
  65. urlMid = urlTop + "api/";
  66. }
  67. let url = urlMid + reqUrl;
  68. if (reqUrl == "registerForApp" || reqUrl == "loginForApp") {
  69. if (isDev) {
  70. url = urlTop + reqUrl;
  71. } else {
  72. url = urlTop + "prod-api/" + reqUrl;
  73. }
  74. }
  75. let headers = ["Content-Type", "application/json; charset=UTF-8"];
  76. //---------------
  77. if(reqUrl.indexOf("/api/game/") >= 0){//这里不需要鉴权
  78. }
  79. const keyForLoginInfo = "userInfo";
  80. const userInfoData = Laya.LocalStorage.getItem(keyForLoginInfo);
  81. if (userInfoData != null) {
  82. try {
  83. let bb = JSON.parse(userInfoData);
  84. if (bb) {
  85. let token = bb.token;
  86. headers.push("Authorization");
  87. headers.push(`Bearer ${token}`);
  88. }
  89. // const token = LocalStorageManager.getItem<{ username: string, token: string }>(keyForLoginInfo)?.token;
  90. } catch (e) {
  91. }
  92. }
  93. //---------------
  94. let httpObj = new Laya.HttpRequest();
  95. httpObj.http.timeout = 5000;
  96. httpObj.once(Laya.Event.COMPLETE, this, completeHandler);
  97. httpObj.once(Laya.Event.ERROR, this, errorHandler);
  98. // httpObj.send(url, data, 'post', 'text', ["Content-Type", "application/json; charset=UTF-8"]);
  99. httpObj.send(url, data, 'post', 'text', headers);
  100. function errorHandler(error): void {
  101. if (call) {
  102. call("{'msg':'Network exception','code':500}");//固定
  103. } else {
  104. alert("Tip:Please check your network connection.");
  105. }
  106. }
  107. function completeHandler(data): void {
  108. if (data && data.length > 0) {
  109. let jsonData = JSON.parse(data);
  110. if (call)
  111. call(jsonData);
  112. }
  113. }
  114. }
  115. // getHeadInfo() {
  116. // let headers = ["Content-Type", "application/json; charset=UTF-8"];
  117. // const keyForLoginInfo = "loginInfo";
  118. // const token = LocalStorageManager.getItem<{ username: string, token: string }>(keyForLoginInfo)?.token;
  119. // if (token) {
  120. // headers.push("Authorization");
  121. // headers.push(`Bearer ${token}`);
  122. // }
  123. // return headers;
  124. // }
  125. public sendGet(url, call) {
  126. let httpObj = new Laya.HttpRequest();
  127. httpObj.http.timeout = 5000;//设置超时时间;
  128. httpObj.once(Laya.Event.COMPLETE, this, completeHandler);
  129. httpObj.once(Laya.Event.ERROR, this, errorHandler);
  130. httpObj.send(url, '', 'get');
  131. function errorHandler(error): void {
  132. console.log("errorHandler:" + error);
  133. //UIMgr.getInstance().showTip("请求失败,请稍后重试");
  134. //call(null);
  135. }
  136. function completeHandler(data): void {
  137. if (data && data.length > 0) {
  138. //let jsonData = JSON.parse(data);
  139. if (call)
  140. call(data);
  141. }
  142. }
  143. }
  144. }