Http.js 2.1 KB

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