Http.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. } else if (res.data.msg == '登陆状态已过期,请重新登陆!') {
  43. wx.redirectTo({
  44. url: '/pages/login/phone',
  45. });
  46. wx.showToast({
  47. title: '登陆状态已过期,请重新登陆!',
  48. icon: "none"
  49. })
  50. }
  51. }
  52. })
  53. }
  54. }
  55. export {
  56. HTTP
  57. }