Http.js 1.7 KB

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