Http.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // const baseUrl = "https://test.buwanjia.com/bwj/rest/webclientrest";
  2. const baseUrl = "https://www.buwanjia.com/bwj/rest/webclientrest";
  3. class HTTP {
  4. request({
  5. url,
  6. data = {},
  7. method = "POST",
  8. header = {
  9. 'content-type': 'application/json'
  10. },
  11. loading = true
  12. }) {
  13. return new Promise((resolve, reject) => {
  14. this._request(url, resolve, reject, data, method, header, loading);
  15. })
  16. }
  17. _request(url, resolve, reject, data, method, header, loading) {
  18. if (loading) wx.showLoading({
  19. title: '加载中...',
  20. mask: true
  21. })
  22. wx.request({
  23. url: baseUrl + url,
  24. data: data,
  25. method: method,
  26. header: header,
  27. timeout: 20000,
  28. success: (res) => {
  29. resolve(res.data);
  30. if (loading) wx.hideLoading()
  31. },
  32. fial: (err) => {
  33. reject(err);
  34. if (loading) wx.hideLoading()
  35. },
  36. complete: (res) => {
  37. if (res.errMsg != 'request:ok') {
  38. wx.hideLoading()
  39. wx.showToast({
  40. title: '网络异常,请重新进入',
  41. icon: "none"
  42. })
  43. }
  44. }
  45. })
  46. }
  47. }
  48. export {
  49. HTTP
  50. }