Http.js 1.9 KB

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