Http.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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.redirectTo({
  47. url: '/pages/home/index',
  48. });
  49. wx.showToast({
  50. title: res.data.msg,
  51. icon: "none"
  52. })
  53. }
  54. }
  55. })
  56. }
  57. }
  58. export {
  59. HTTP
  60. }