Http.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. class HTTP {
  2. constructor() {
  3. let ENV = wx.getAccountInfoSync().miniProgram.envVersion;
  4. if (ENV === 'release') { // 正式版
  5. this.baseUrl = "https://lsa.cnyunl.com";
  6. } else {
  7. // this.baseUrl = "https://lsa.cnyunl.com";
  8. this.baseUrl = "http://61.164.207.46:8200";
  9. // this.baseUrl = "https://cucu.cnyunl.com:8079";
  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. showLoading = '加载中...'
  21. }) {
  22. return new Promise((resolve, reject) => {
  23. this._request(url, resolve, reject, data, method, header, showLoading);
  24. })
  25. }
  26. _request(url, resolve, reject, data, method, header, showLoading) {
  27. /* if (showLoading) wx.showLoading({
  28. title: showLoading,
  29. mask: true
  30. }) */
  31. wx.request({
  32. url: this.baseUrl + '/yos/rest/index' + url,
  33. data,
  34. method,
  35. header,
  36. timeout: 60000,
  37. success: (res) => {
  38. resolve(res.data);
  39. },
  40. fial: (err) => {
  41. reject(err);
  42. },
  43. complete: (res) => {
  44. // if (showLoading) wx.hideLoading()
  45. if (res.errMsg != 'request:ok') {
  46. wx.showToast({
  47. title: '网络异常,请重新进入',
  48. icon: "none"
  49. })
  50. } else if (res.data.msg == '登陆状态已过期,请重新登陆!') {
  51. wx.redirectTo({
  52. url: '/pages/login/phone',
  53. });
  54. wx.showToast({
  55. title: res.msg,
  56. icon: "none"
  57. })
  58. }
  59. }
  60. })
  61. }
  62. }
  63. export {
  64. HTTP
  65. }