Http.js 1.6 KB

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