HttpSystem.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 errorPop 请求失败是否弹窗
  13. * @param async 是否异步请求(后端要求防阻塞)
  14. */
  15. public async sendData(url, data: any, errorPop = true, async = true) {
  16. let encode = '';
  17. let dataN = null;
  18. data.timestamp = Math.floor(new Date().getTime() / 1000);
  19. data.appVersion = g.appData.appVersion;
  20. data.tfChannel = g.appData.tfChannel;
  21. data.versioncfg = g.appData.version;
  22. encode = mk.encrypt.encrypt(JSON.stringify(data), g.loginData.sessionKey, g.appData.appId);
  23. dataN = {
  24. "uin": g.loginData.uin,
  25. "encrypt": encode
  26. }
  27. mk.log.log('httpRequest', url, data, dataN);
  28. let result = await this.sendRequest(url, 'POST', JSON.stringify(dataN), false, errorPop, async);
  29. return this.checkData(result);
  30. }
  31. /**
  32. * http请求 登录用
  33. * @param url 请求头
  34. * @param method 'GET' || 'POST'
  35. * @param data 数据
  36. * @param isRsa 是否加密
  37. * @param errorPop 请求失败时是否弹窗
  38. * @param async 是否异步请求
  39. */
  40. public sendRequest(url, method: string = 'POST', data: string = null, isRsa: boolean = true, errorPop = true, async = true) {
  41. return new Promise<any>((resolve: (data: any) => void, reject) => {
  42. let requesttimes = 0;
  43. let httpRequest = new XMLHttpRequest();
  44. let send = () => {
  45. httpRequest.open(method, url, async);
  46. httpRequest.setRequestHeader("Content-Type", 'application/json;charset=UTF-8');
  47. if (isRsa && data != null) {
  48. data = mk.encrypt.rsaEncrypt(data);
  49. }
  50. httpRequest.send(data);
  51. };
  52. // httpRequest.timeout = 30000;
  53. httpRequest.onreadystatechange = function () {
  54. if (httpRequest.readyState === 4) {
  55. if (httpRequest.status === 200) {
  56. var responseStr = httpRequest.responseText;
  57. let response = JSON.parse(responseStr);
  58. response.errorcode = response.errcode;
  59. mk.log.log('httpResponse', url, response);
  60. if (response.errcode == -10003) {
  61. reject(10003);
  62. }
  63. else {
  64. if (response.errcode != 0) {
  65. resolve(response);
  66. }
  67. }
  68. }
  69. else {
  70. mk.log.log("httpRequest.status", httpRequest.status);
  71. reject(httpRequest.status);
  72. }
  73. }
  74. }
  75. httpRequest.ontimeout = function (e) {
  76. requesttimes++;
  77. if (requesttimes >= 3) {
  78. mk.log.log("http.ontimeout", url);
  79. reject("timeout");
  80. }
  81. else {
  82. send();
  83. }
  84. };
  85. httpRequest.onerror = function (e) {
  86. mk.log.log("http.onerror", url);
  87. reject("onerror");
  88. };
  89. send();
  90. });
  91. }
  92. /**
  93. * 检测用明文还是密文
  94. * @param response 返回消息
  95. * @param forceData true 明文 false 有密文优先密文,没密文情况下用明文
  96. */
  97. checkData(response, forceData = false) {
  98. let data = null
  99. if (forceData) {
  100. data = response.data;
  101. }
  102. else {
  103. if (response.encrypt) {
  104. data = mk.encrypt.decrypt(response.encrypt, g.loginData.sessionKey, g.appData.appId);
  105. data = JSON.parse(data);
  106. }
  107. else if (response.data) {
  108. data = response.data;
  109. }
  110. }
  111. response.data = data;
  112. return response;
  113. }
  114. }