Xhr.weapp.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  7. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  8. var XhrWeapp = function () {
  9. function XhrWeapp() {
  10. (0, _classCallCheck2.default)(this, XhrWeapp);
  11. this.UNSENT = 0;
  12. this.OPENED = 1;
  13. this.HEADERS_RECEIVED = 2;
  14. this.LOADING = 3;
  15. this.DONE = 4;
  16. this.header = {};
  17. this.readyState = this.DONE;
  18. this.status = 0;
  19. this.response = '';
  20. this.responseType = '';
  21. this.responseText = '';
  22. this.responseHeader = {};
  23. this.method = '';
  24. this.url = '';
  25. this.onabort = function () {};
  26. this.onprogress = function () {};
  27. this.onerror = function () {};
  28. this.onreadystatechange = function () {};
  29. this.requestTask = null;
  30. }
  31. return (0, _createClass2.default)(XhrWeapp, [{
  32. key: "getAllResponseHeaders",
  33. value: function () {
  34. var header = '';
  35. for (var key in this.responseHeader) {
  36. header += key + ':' + this.getResponseHeader(key) + '\r\n';
  37. }
  38. return header;
  39. }
  40. }, {
  41. key: "getResponseHeader",
  42. value: function (key) {
  43. return this.responseHeader[key];
  44. }
  45. }, {
  46. key: "setRequestHeader",
  47. value: function (key, value) {
  48. this.header[key] = value;
  49. }
  50. }, {
  51. key: "open",
  52. value: function (method, url) {
  53. this.method = method;
  54. this.url = url;
  55. }
  56. }, {
  57. key: "abort",
  58. value: function () {
  59. if (!this.requestTask) {
  60. return;
  61. }
  62. this.requestTask.abort();
  63. this.status = 0;
  64. this.response = undefined;
  65. this.onabort();
  66. this.onreadystatechange();
  67. }
  68. }, {
  69. key: "send",
  70. value: function (data) {
  71. var _this = this;
  72. this.requestTask = wx.request({
  73. url: this.url,
  74. method: this.method,
  75. data: data,
  76. header: this.header,
  77. responseType: this.responseType,
  78. success: function (res) {
  79. _this.status = res.statusCode;
  80. _this.response = res.data;
  81. _this.responseHeader = res.header;
  82. _this.responseText = JSON.stringify(res.data);
  83. _this.requestTask = null;
  84. _this.onreadystatechange();
  85. },
  86. fail: function (err) {
  87. _this.requestTask = null;
  88. _this.onerror(err);
  89. }
  90. });
  91. this.requestTask.onProgressUpdate(function (res) {
  92. var event = {
  93. lengthComputable: res.totalBytesExpectedToWrite !== 0,
  94. loaded: res.totalBytesWritten,
  95. total: res.totalBytesExpectedToWrite
  96. };
  97. _this.onprogress(event);
  98. });
  99. }
  100. }]);
  101. }();
  102. module.exports = XhrWeapp;
  103. var _default = exports.default = XhrWeapp;