Http.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. class HTTP {
  2. constructor() {
  3. // this.baseUrl = "https://meida.cnyunl.com";
  4. this.baseUrl = "http://192.168.3.111:8100";
  5. }
  6. request({
  7. url,
  8. data = {},
  9. method = "POST",
  10. header = {
  11. 'content-type': 'application/json'
  12. },
  13. loading = true
  14. }) {
  15. return new Promise((resolve, reject) => {
  16. this._request(url, resolve, reject, data, method, header, loading);
  17. })
  18. }
  19. _request(url, resolve, reject, data, method, header, loading) {
  20. if (loading) wx.showLoading({
  21. title: '加载中...',
  22. mask: true
  23. })
  24. wx.request({
  25. url: this.baseUrl + '/yos/rest/index' + url,
  26. data: data,
  27. method: method,
  28. header: header,
  29. timeout: 60000,
  30. success: (res) => {
  31. resolve(res.data);
  32. if (loading) wx.hideLoading();
  33. },
  34. fial: (err) => {
  35. reject(err);
  36. if (loading) wx.hideLoading()
  37. },
  38. complete: (res) => {
  39. if (res.errMsg != 'request:ok') {
  40. wx.hideLoading()
  41. wx.showToast({
  42. title: '网络异常,请重新进入',
  43. icon: "none"
  44. })
  45. } else if (res.data.msg == '登陆状态已过期,请重新登陆!') {
  46. wx.redirectTo({
  47. url: '/pages/login/phone',
  48. });
  49. wx.showToast({
  50. title: '登陆状态已过期,请重新登陆!',
  51. icon: "none"
  52. })
  53. }
  54. }
  55. })
  56. }
  57. }
  58. export {
  59. HTTP
  60. }