Http.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // const baseUrl = "https://meida.cnyunl.com/yos/rest/index";
  2. const baseUrl = "http://192.168.3.111:8100/yos/rest/index";
  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: 60000,
  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. } else if (res.data.msg == '登陆状态已过期,请重新登陆!') {
  44. wx.redirectTo({
  45. url: '/pages/login/phone',
  46. });
  47. wx.showToast({
  48. title: '登陆状态已过期,请重新登陆!',
  49. icon: "none"
  50. })
  51. }
  52. }
  53. })
  54. }
  55. }
  56. export {
  57. HTTP
  58. }