Http.js 1.5 KB

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