Xhr.weapp.js 2.9 KB

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