Http.js 1.3 KB

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