Http.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. this.init = (content, init = false) => {
  18. return new Promise((resolve, reject) => {
  19. if (init.detail != undefined) init = init.detail;
  20. if (init) content.pageNumber = 1;
  21. content.pageNumber > content.pageTotal ? reject() : resolve(content)
  22. })
  23. }
  24. this.paging = (data, res) => {
  25. let content = JSON.parse(JSON.stringify(data))
  26. content.pageNumber = res.pageNumber + 1;
  27. content.pageTotal = res.pageTotal;
  28. content.total = res.total;
  29. content.sort = res.sort;
  30. if (this.isShowEmpty) this.empty = res.total == 0;
  31. return content;
  32. }
  33. }
  34. request({
  35. url,
  36. data = {},
  37. method = "POST",
  38. header = {
  39. 'content-type': 'application/json'
  40. },
  41. loading = true
  42. }) {
  43. return new Promise((resolve, reject) => {
  44. this._request(url, resolve, reject, data, method, header, loading);
  45. })
  46. }
  47. _request(url, resolve, reject, data, method, header, loading) {
  48. /* if (loading) wx.showLoading({
  49. title: '加载中...',
  50. mask: true
  51. }) */
  52. if (data.method && data.method != "query_userauth") data.languagecode = wx.getStorageSync('languagecode') || 'ZH'
  53. wx.request({
  54. url: this.baseUrl + '/yos/rest/index' + url,
  55. data: data,
  56. method: method,
  57. header: header,
  58. timeout: 60000,
  59. success: res => resolve(res.data),
  60. fial: err => reject(err),
  61. complete: (res) => {
  62. // if (loading) wx.hideLoading()
  63. if (res.errMsg != 'request:ok') {
  64. wx.showToast({
  65. title: '网络异常,请重新进入',
  66. icon: "none"
  67. })
  68. } else if (res.data.msg == '登陆状态已过期,请重新登陆!') {
  69. wx.showToast({
  70. title: '登陆状态已过期,请重新登陆!',
  71. icon: "none",
  72. mask: true
  73. })
  74. getApp().globalData.LaunchOptions = wx.getLaunchOptionsSync();
  75. count = setTimeout(() => {
  76. wx.reLaunch({
  77. url: '/pages/login/phone',
  78. });
  79. }, 1000)
  80. }
  81. }
  82. })
  83. }
  84. }
  85. export {
  86. HTTP
  87. }