efunRequest.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //http 网络请求封装类(和业务相关的代码请不要写进这个类中)
  2. // import UserDataStorage from './UserDataStorage';
  3. class efunRequest {
  4. constructor() {
  5. this._url = '';
  6. this._params = {};
  7. this._data = {};
  8. this._header = {};
  9. this.xmlHTTPRequest = null;
  10. this._onSuccess = null;
  11. this._onFailed = null;
  12. this._deserialize = null;
  13. }
  14. request(method) {
  15. const paramStr = () => {
  16. if (!this._params) {
  17. return ''
  18. }
  19. var pstr = '';
  20. for (let key in this._params) {
  21. pstr += `${key}=${this._params[key]}&`;
  22. }
  23. return pstr.slice(0, -1);
  24. }
  25. this.xmlHTTPRequest = new XMLHttpRequest();
  26. this.xmlHTTPRequest.open(method, this._url + '?' + paramStr(), true);
  27. this.xmlHTTPRequest.setRequestHeader('Content-Type', 'application/' + (method == 'POST' || method == 'PUT' ? 'x-www-form-urlencoded' : 'text'));
  28. for (let key in this._header) {
  29. this.xmlHTTPRequest.setRequestHeader(key, this._header[key]);
  30. }
  31. this.xmlHTTPRequest.onreadystatechange = () => {
  32. this.whenResponse();
  33. }
  34. var sendBody = null;
  35. if (this._data && (method == 'POST' || method == 'PUT')) {
  36. var arr = new Array();
  37. var i = 0;
  38. for (var attr in this._data) {
  39. arr[i] = encodeURIComponent(attr) + '=' + encodeURIComponent(this._data[attr]);
  40. i++;
  41. }
  42. sendBody = '&' + arr.join('&');
  43. }
  44. this.xmlHTTPRequest.send(sendBody);
  45. return this;
  46. }
  47. get() {
  48. this.request('GET');
  49. return this;
  50. }
  51. post() {
  52. this.request('POST');
  53. return this;
  54. }
  55. put() {
  56. this.request('PUT');
  57. return this;
  58. }
  59. delete() {
  60. this.request('DELETE');
  61. return this;
  62. }
  63. whenResponse() {
  64. if (this.xmlHTTPRequest.readyState != 4) {
  65. return;
  66. }
  67. if (this._deserialize) {
  68. this._deserialize(this.xmlHTTPRequest.responseText, this);
  69. return;
  70. }
  71. let cb = this.xmlHTTPRequest.status == 200 ? this._onSuccess : this._onFailed;
  72. if (!cb) {
  73. return;
  74. }
  75. cb(this.xmlHTTPRequest.status, this.xmlHTTPRequest.responseText);
  76. }
  77. success(callback) {
  78. this._onSuccess = callback;
  79. return this;
  80. }
  81. fail(callback) {
  82. this._onFailed = callback;
  83. return this;
  84. }
  85. header(obj, rewrite) {
  86. if (rewrite) {
  87. this._header = obj;
  88. } else {
  89. for (let key in obj) {
  90. this._header[key] = obj[key];
  91. }
  92. }
  93. return this;
  94. }
  95. params(obj) {
  96. this._params = obj;
  97. return this;
  98. }
  99. data(obj) {
  100. this._data = obj;
  101. return this;
  102. }
  103. url(str) {
  104. this._url = str;
  105. return this;
  106. }
  107. responseDeserialize(func) {
  108. this._deserialize = func;
  109. return this;
  110. }
  111. static getHttpRequest() {
  112. let request = new efunRequest();
  113. // if (UserDataStorage.getToken()) {
  114. // request.header({
  115. // 'Authentication': UserDataStorage.getToken()
  116. // });
  117. // }
  118. request.responseDeserialize(function (res, req) {
  119. function callFailed(data) {
  120. if (req._onFailed) {
  121. req._onFailed(data);
  122. }
  123. }
  124. function callSuccess(data) {
  125. if (req._onSuccess) {
  126. req._onSuccess(data);
  127. }
  128. }
  129. let rObj = null;
  130. try {
  131. rObj = JSON.parse(res);
  132. } catch (e) {
  133. callFailed(e);
  134. return;
  135. }
  136. if (req.xmlHTTPRequest.status != 200) {
  137. callFailed(rObj);
  138. return;
  139. }
  140. if (!rObj.success) {
  141. callFailed(rObj);
  142. return;
  143. }
  144. callSuccess(rObj);
  145. });
  146. return request;
  147. }
  148. }
  149. export default efunRequest;