HttpSystem.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * @description http请求管理类
  3. * @author 邹勇
  4. */
  5. export default class HttpSystem {
  6. constructor() {
  7. }
  8. /**
  9. * 游戏接口,通用
  10. * @param url 接口完整地址
  11. * @param data 加密前数据
  12. * @param async 是否异步请求(后端要求防阻塞)
  13. */
  14. public async sendData(url, data: any, async = true) {
  15. let encode = '';
  16. let dataN = null;
  17. data.timestamp = Math.floor(new Date().getTime() / 1000);
  18. data.appVersion = gData.appData.appVersion;
  19. data.tfChannel = gData.appData.tfChannel;
  20. data.versioncfg = gData.appData.version;
  21. encode = mk.encrypt.encrypt(JSON.stringify(data), gData.loginData.sessionKey, gData.appData.appId);
  22. dataN = {
  23. "uin": gData.loginData.uin,
  24. "encrypt": encode
  25. }
  26. let result = await this.sendRequest(url, 'POST', JSON.stringify(dataN), false, async);
  27. return this.checkData(result);
  28. }
  29. /**
  30. * http请求 登录用
  31. * @param url 请求头
  32. * @param method 'GET' || 'POST'
  33. * @param data 数据
  34. * @param isRsa 是否加密
  35. * @param async 是否异步请求
  36. */
  37. public sendRequest(url, method: string = 'POST', data: string = null, isRsa: boolean = true, async = true) {
  38. url = gData.httpData.getServerUrl() + url;
  39. return new Promise<any>((resolve: (data: any) => void, reject) => {
  40. let requesttimes = 0;
  41. let httpRequest = new XMLHttpRequest();
  42. let send = () => {
  43. httpRequest.open(method, url, async);
  44. httpRequest.setRequestHeader("Content-Type", 'application/json;charset=UTF-8');
  45. if (isRsa && data != null) {
  46. data = mk.encrypt.rsaEncrypt(data);
  47. }
  48. httpRequest.send(data);
  49. };
  50. // httpRequest.timeout = 30000;
  51. httpRequest.onreadystatechange = function () {
  52. if (httpRequest.readyState === 4) {
  53. if (httpRequest.status === 200) {
  54. var responseStr = httpRequest.responseText;
  55. let response = JSON.parse(responseStr);
  56. mk.console.logSingle("response:" + url, response)
  57. resolve(response);
  58. }
  59. else {
  60. mk.console.error("httpRequest.status", httpRequest.status);
  61. reject({ errcode: 1, errmsg: "status..." + httpRequest.status });
  62. }
  63. }
  64. }
  65. httpRequest.ontimeout = function (e) {
  66. requesttimes++;
  67. if (requesttimes >= 3) {
  68. mk.console.error("http.ontimeout", url);
  69. reject({ errcode: 1, errmsg: "ontimeout" });
  70. }
  71. else {
  72. send();
  73. }
  74. };
  75. httpRequest.onerror = function (e) {
  76. mk.console.error("http.onerror", url);
  77. reject({ errcode: 1, errmsg: "onerror" });
  78. };
  79. send();
  80. });
  81. }
  82. /**
  83. * 检测用明文还是密文
  84. * @param response 返回消息
  85. * @param forceData true 明文 false 有密文优先密文,没密文情况下用明文
  86. */
  87. checkData(response, forceData = false) {
  88. let data = null
  89. if (forceData) {
  90. data = response.data;
  91. }
  92. else {
  93. if (response.encrypt) {
  94. data = mk.encrypt.decrypt(response.encrypt, gData.loginData.sessionKey, gData.appData.appId);
  95. data = JSON.parse(data);
  96. }
  97. else if (response.data) {
  98. data = response.data;
  99. }
  100. }
  101. response.data = data;
  102. if (response.errcode != 0) {
  103. if (response.errcode == -10003 || response.errcode == -20003) {
  104. mk.ui.openPanel('module/warnTip/WarnTipPanel');
  105. }
  106. }
  107. return response;
  108. }
  109. }