Http.js 2.0 KB

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