Http.js 1.6 KB

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