Http.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. let count = null;
  5. class HTTP {
  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: baseUrl + 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. clearTimeout(count);
  47. wx.showToast({
  48. title: '登陆状态已过期,请重新登陆!',
  49. icon: "none"
  50. })
  51. count = setTimeout(() => {
  52. wx.redirectTo({
  53. url: '/pages/login/phone',
  54. });
  55. }, 500)
  56. }
  57. }
  58. })
  59. }
  60. }
  61. export {
  62. HTTP
  63. }