Http.js 1.3 KB

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