Http.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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: getApp().globalData.Language.getMapText('加载中...'),
  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.data.code != 1) {
  64. console.error("错误的请求", {
  65. request: data,
  66. result: res.data
  67. })
  68. }
  69. if (res.errMsg != 'request:ok') {
  70. wx.showToast({
  71. title: getApp().globalData.Language.getMapText('网络异常,请重新进入'),
  72. icon: "none"
  73. })
  74. } else if (res.data.msg == '登陆状态已过期,请重新登陆!' || res.data.msg == getApp().globalData.Language.getMapText('登陆状态已过期,请重新登陆!')) {
  75. wx.showToast({
  76. title: getApp().globalData.Language.getMapText(res.data.msg),
  77. icon: "none",
  78. mask: true
  79. })
  80. getApp().globalData.LaunchOptions = wx.getLaunchOptionsSync();
  81. count = setTimeout(() => {
  82. wx.reLaunch({
  83. url: '/pages/login/phone',
  84. });
  85. }, 1000)
  86. }
  87. }
  88. })
  89. }
  90. }
  91. export {
  92. HTTP
  93. }