Http.js 2.3 KB

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