Http.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. class HTTP {
  2. constructor() {
  3. let ENV = wx.getAccountInfoSync().miniProgram.envVersion;
  4. if (ENV === 'release') { // 正式版
  5. // this.baseUrl = "https://lsa.cnyunl.com";
  6. this.baseUrl = "https://www.cnyunl.com";
  7. } else {
  8. // this.baseUrl = "https://www.cnyunl.com";
  9. this.baseUrl = "http://61.164.207.46:8200";
  10. // this.baseUrl = "https://cucu.cnyunl.com:8079";
  11. }
  12. console.log("接口地址:", this.baseUrl)
  13. }
  14. request({
  15. url,
  16. data = {},
  17. method = "POST",
  18. header = {
  19. 'content-type': 'application/json'
  20. },
  21. showLoading = '加载中...'
  22. }) {
  23. return new Promise((resolve, reject) => {
  24. this._request(url, resolve, reject, data, method, header, showLoading);
  25. })
  26. }
  27. _request(url, resolve, reject, data, method, header, showLoading) {
  28. /* if (showLoading) wx.showLoading({
  29. title: showLoading,
  30. mask: true
  31. }) */
  32. wx.request({
  33. url: this.baseUrl + '/yos/rest/index' + url,
  34. data,
  35. method,
  36. header,
  37. timeout: 60000,
  38. success: (res) => {
  39. resolve(res.data);
  40. },
  41. fial: (err) => {
  42. reject(err);
  43. },
  44. complete: (res) => {
  45. // if (showLoading) wx.hideLoading()
  46. if (res.errMsg != 'request:ok') {
  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: res.msg,
  57. icon: "none"
  58. })
  59. }
  60. }
  61. })
  62. }
  63. }
  64. export {
  65. HTTP
  66. }