Http.js 2.0 KB

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