Http.js 4.0 KB

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