Http.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. let currentPages = getCurrentPages()[getCurrentPages().length - 1];
  73. // 如果当前页面不是登录页面,则跳转到登录页面
  74. if (currentPages.route !== 'pages/login/login') {
  75. uni.showModal({
  76. title: '提示',
  77. content: '您的登录状态已过期,请重新登录。',
  78. showCancel: false,
  79. success: () => {
  80. // 跳转到登录页面
  81. this._redirectToLogin(res);
  82. }
  83. });
  84. }
  85. }
  86. }
  87. });
  88. }
  89. // 处理会话Cookie
  90. _handleSessionCookies(res) {
  91. try {
  92. const cookies = res.cookies || [];
  93. const headers = res.header || {};
  94. // 检查响应头中的Set-Cookie
  95. const setCookie = headers['Set-Cookie'] || headers['set-cookie'];
  96. if (setCookie) {
  97. // 将字符串转换为数组
  98. const cookieArray = Array.isArray(setCookie)
  99. ? setCookie
  100. : [setCookie];
  101. cookies.push(...cookieArray);
  102. }
  103. // 查找JSESSIONID
  104. for (const cookie of cookies) {
  105. const match = cookie.match(/JSESSIONID=([^;]+)/i);
  106. if (match && match[1]) {
  107. const newSessionId = match[1];
  108. // 更新JSESSIONID
  109. if (newSessionId !== this.jsessionid) {
  110. this.jsessionid = newSessionId;
  111. wx.removeStorageSync('JSESSIONID');
  112. wx.setStorageSync('JSESSIONID', newSessionId);
  113. }
  114. break;
  115. }
  116. }
  117. } catch (error) {
  118. console.error('处理会话Cookie时出错:', error);
  119. }
  120. }
  121. }
  122. export { HTTP };