WXHttpRequest.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. module.exports = {
  2. getInstance: function() {
  3. wx.showLoading({
  4. title: '加载中...',
  5. mast: true
  6. })
  7. return {
  8. _sucCallback: null,
  9. _failCallback: null,
  10. _method: 'GET',
  11. _data: {},
  12. _header: { 'content-type': 'application/json' },
  13. _url: '',
  14. send: function() {
  15. wx.request({
  16. url: this._url,
  17. data: this._data,
  18. method: this._method,
  19. header: this._header,
  20. success: function(res) {
  21. this._sucCallback(res);
  22. }.bind(this),
  23. fail: function(res) {
  24. this._failCallback(res);
  25. }.bind(this)
  26. });
  27. return this;
  28. },
  29. success: function(callback) {
  30. this._sucCallback = callback;
  31. setTimeout(function () {
  32. wx.hideLoading()
  33. }, 200)
  34. return this;
  35. },
  36. fail: function(callback) {
  37. this._failCallback = callback;
  38. setTimeout(function () {
  39. wx.hideLoading()
  40. }, 200)
  41. return this;
  42. },
  43. url: function(url) {
  44. this._url = url;
  45. return this;
  46. },
  47. data: function(data) {
  48. this._data = data;
  49. return this;
  50. },
  51. header: function(header) {
  52. this._header = header;
  53. return this;
  54. },
  55. method: function(method) {
  56. this._method = method;
  57. return this;
  58. }
  59. };
  60. }
  61. };