Http.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. class HTTP {
  2. constructor() {
  3. this.ENV = wx.getAccountInfoSync().miniProgram.envVersion;
  4. this.urls = [{
  5. name: "美大正式",
  6. url: "https://crm.meida.com:16691"
  7. }, {
  8. name: "楚楚",
  9. url: "https://cucu.cnyunl.com:8079"
  10. }, {
  11. name: "开发环境",
  12. url: "http://61.164.207.46:8300"
  13. }]
  14. if (this.ENV === 'release') { // 正式版
  15. this.baseUrl = "https://crm.meida.com:16691";
  16. } else {
  17. // this.baseUrl = "http://61.164.207.46:8300";
  18. this.baseUrl = "https://crm.meida.com:16691";
  19. }
  20. console.log("接口地址:", this.baseUrl)
  21. // 从本地存储加载JSESSIONID
  22. this.jsessionid = wx.getStorageSync('JSESSIONID') || '';
  23. console.log("接口地址", this.baseUrl);
  24. }
  25. request({
  26. url,
  27. data = {},
  28. method = "POST",
  29. header = {
  30. 'content-type': 'application/json',
  31. "accesstoken": data.accesstoken || wx.getStorageSync('userMsg').token || '',
  32. "Cookie": `JSESSIONID=${this.jsessionid}`
  33. },
  34. showLoading = '加载中...'
  35. }) {
  36. return new Promise((resolve, reject) => {
  37. this._request(url, resolve, reject, data, method, header, showLoading);
  38. })
  39. }
  40. _request(url, resolve, reject, data, method, header, showLoading) {
  41. /* if (showLoading) wx.showLoading({
  42. title: showLoading,
  43. mask: true
  44. }) */
  45. wx.request({
  46. url: this.baseUrl + '/yos/rest/index' + url,
  47. data,
  48. method,
  49. header,
  50. timeout: 60000,
  51. success: (res) => {
  52. resolve(res.data);
  53. },
  54. fial: (err) => {
  55. reject(err);
  56. },
  57. complete: (res) => {
  58. // if (showLoading) wx.hideLoading()
  59. this._handleSessionCookies(res);
  60. if (res.errMsg != 'request:ok') {
  61. wx.showToast({
  62. title: '网络异常,请重新进入',
  63. icon: "none"
  64. })
  65. } else if (res.data.msg == '登陆状态已过期,请重新登陆!') {
  66. this.jsessionid = '';
  67. wx.removeStorageSync('JSESSIONID');
  68. wx.redirectTo({
  69. url: '/pages/login/phone',
  70. });
  71. wx.showToast({
  72. title: res.msg,
  73. icon: "none"
  74. })
  75. }
  76. }
  77. })
  78. }
  79. // 处理会话Cookie
  80. _handleSessionCookies(res) {
  81. try {
  82. const cookies = res.cookies || [];
  83. const headers = res.header || {};
  84. // 检查响应头中的Set-Cookie
  85. const setCookie = headers['Set-Cookie'] || headers['set-cookie'];
  86. if (setCookie) {
  87. // 将字符串转换为数组
  88. const cookieArray = Array.isArray(setCookie) ?
  89. setCookie : [setCookie];
  90. cookies.push(...cookieArray);
  91. }
  92. // 查找JSESSIONID
  93. for (const cookie of cookies) {
  94. const match = cookie.match(/JSESSIONID=([^;]+)/i);
  95. if (match && match[1]) {
  96. const newSessionId = match[1];
  97. // 更新JSESSIONID
  98. if (newSessionId !== this.jsessionid) {
  99. this.jsessionid = newSessionId;
  100. wx.removeStorageSync('JSESSIONID');
  101. wx.setStorageSync('JSESSIONID', newSessionId);
  102. }
  103. break;
  104. }
  105. }
  106. } catch (error) {
  107. console.error('处理会话Cookie时出错:', error);
  108. }
  109. }
  110. }
  111. export {
  112. HTTP
  113. }