Http.js 2.1 KB

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