efunRequest.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. this.uid = this.getuid();
  15. }
  16. async getuid() {
  17. const userinfo = await storage
  18. .load({
  19. key: 'userInfo'
  20. })
  21. .catch((error) => {
  22. return '';
  23. });
  24. if (userinfo === '') {
  25. return '';
  26. }
  27. return JSON.parse(userinfo).uid;
  28. }
  29. request(method) {
  30. const paramStr = () => {
  31. if (!this._params) {
  32. return '';
  33. }
  34. var pstr = '';
  35. for (let key in this._params) {
  36. pstr += `${key}=${this._params[key]}&`;
  37. }
  38. return pstr.slice(0, -1);
  39. };
  40. this.xmlHTTPRequest = new XMLHttpRequest();
  41. this.xmlHTTPRequest.open(method, this._url + '?' + paramStr(), true);
  42. this.xmlHTTPRequest.setRequestHeader(
  43. 'Content-Type',
  44. 'application/' + (method == 'POST' || method == 'PUT' ? 'x-www-form-urlencoded' : 'text')
  45. );
  46. console.log('====================================');
  47. console.log('uid', this.uid);
  48. console.log('====================================');
  49. this.xmlHTTPRequest.setRequestHeader('uid', this.uid ? this.uid : '');
  50. for (let key in this._header) {
  51. this.xmlHTTPRequest.setRequestHeader(key, this._header[key]);
  52. }
  53. this.xmlHTTPRequest.onreadystatechange = () => {
  54. this.whenResponse();
  55. };
  56. var sendBody = null;
  57. if (this._data && (method == 'POST' || method == 'PUT')) {
  58. var arr = new Array();
  59. var i = 0;
  60. for (var attr in this._data) {
  61. arr[i] = encodeURIComponent(attr) + '=' + encodeURIComponent(this._data[attr]);
  62. i++;
  63. }
  64. sendBody = '&' + arr.join('&');
  65. }
  66. this.xmlHTTPRequest.send(sendBody);
  67. return this;
  68. }
  69. get() {
  70. this.request('GET');
  71. return this;
  72. }
  73. post() {
  74. this.request('POST');
  75. return this;
  76. }
  77. put() {
  78. this.request('PUT');
  79. return this;
  80. }
  81. delete() {
  82. this.request('DELETE');
  83. return this;
  84. }
  85. whenResponse() {
  86. if (this.xmlHTTPRequest.readyState != 4) {
  87. return;
  88. }
  89. if (this._deserialize) {
  90. this._deserialize(this.xmlHTTPRequest.responseText, this);
  91. return;
  92. }
  93. let cb = this.xmlHTTPRequest.status == 200 ? this._onSuccess : this._onFailed;
  94. if (!cb) {
  95. return;
  96. }
  97. cb(this.xmlHTTPRequest.status, this.xmlHTTPRequest.responseText);
  98. }
  99. success(callback) {
  100. this._onSuccess = callback;
  101. return this;
  102. }
  103. fail(callback) {
  104. this._onFailed = callback;
  105. return this;
  106. }
  107. header(obj, rewrite) {
  108. if (rewrite) {
  109. this._header = obj;
  110. } else {
  111. for (let key in obj) {
  112. this._header[key] = obj[key];
  113. }
  114. }
  115. return this;
  116. }
  117. params(obj) {
  118. this._params = obj;
  119. return this;
  120. }
  121. data(obj) {
  122. this._data = obj;
  123. return this;
  124. }
  125. url(str) {
  126. this._url = str;
  127. return this;
  128. }
  129. responseDeserialize(func) {
  130. this._deserialize = func;
  131. return this;
  132. }
  133. static getHttpRequest() {
  134. let request = new efunRequest();
  135. // if (UserDataStorage.getToken()) {
  136. // request.header({
  137. // 'Authentication': UserDataStorage.getToken()
  138. // });
  139. // }
  140. request.responseDeserialize(function(res, req) {
  141. function callFailed(data) {
  142. if (req._onFailed) {
  143. req._onFailed(data);
  144. }
  145. }
  146. function callSuccess(data) {
  147. if (req._onSuccess) {
  148. req._onSuccess(data);
  149. }
  150. }
  151. let rObj = null;
  152. try {
  153. rObj = JSON.parse(res);
  154. } catch (e) {
  155. callFailed(e);
  156. return;
  157. }
  158. if (req.xmlHTTPRequest.status != 200) {
  159. callFailed(rObj);
  160. return;
  161. }
  162. if (!rObj.success) {
  163. callFailed(rObj);
  164. return;
  165. }
  166. callSuccess(rObj);
  167. });
  168. return request;
  169. }
  170. }
  171. export default efunRequest;