Http.js 2.0 KB

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