Http.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // 如果data.content.pageNumber存在但pageTotal不存在,则设置pageTotal为1
  35. // 如果pageNumber大于pageTotal,则直接返回空结果
  36. // 在接口中拿到的pageNumber自动加1,目的是在这里直接处理分页逻辑
  37. if (data.content.pageNumber && !data.content.pageTotal) {
  38. data.content.pageTotal = 1;
  39. if (data.content.pageNumber > data.content.pageTotal) return new Promise((resolve) => resolve({ code: 0 }));;
  40. }
  41. return new Promise((resolve, reject) => {
  42. this._request(url, resolve, reject, data, method, header, showLoading);
  43. });
  44. }
  45. _request(url, resolve, reject, data, method, header, showLoading) {
  46. if (showLoading) uni.showLoading({
  47. title: showLoading,
  48. mask: true
  49. });
  50. uni.request({
  51. url: this.baseUrl + '/yos/rest' + url,
  52. data: data,
  53. method: method,
  54. header: header,
  55. timeout: 60000,
  56. success: (res) => {
  57. // 提取并保存JSESSIONID
  58. this._handleSessionCookies(res);
  59. if (res.data.pageNumber) res.data.pageNumber++
  60. resolve(res.data);
  61. },
  62. fail: (err) => {
  63. reject(err);
  64. },
  65. complete: (res) => {
  66. if (showLoading) uni.hideLoading();
  67. // 网络错误处理
  68. if (res.errMsg !== 'request:ok') {
  69. uni.showToast({
  70. title: '网络异常,请稍后再试',
  71. icon: "none"
  72. });
  73. return;
  74. }
  75. // 会话过期处理
  76. if (res.data.msg === '登陆状态已过期,请重新登陆!') {
  77. // 清除过期会话ID
  78. this.jsessionid = '';
  79. wx.removeStorageSync('JSESSIONID');
  80. let currentPages = getCurrentPages()[getCurrentPages().length - 1];
  81. console.log("currentPages", currentPages)
  82. // 如果当前页面不是登录页面,则跳转到登录页面
  83. if (currentPages.route !== 'pages/login/login') {
  84. uni.showModal({
  85. title: '提示',
  86. content: '您的登录状态已过期,请重新登录。',
  87. showCancel: false,
  88. success: () => {
  89. wx.redirectTo({
  90. url: '/pages/login/login',
  91. })
  92. }
  93. });
  94. }
  95. }
  96. }
  97. });
  98. }
  99. // 处理会话Cookie
  100. _handleSessionCookies(res) {
  101. try {
  102. const cookies = res.cookies || [];
  103. const headers = res.header || {};
  104. // 检查响应头中的Set-Cookie
  105. const setCookie = headers['Set-Cookie'] || headers['set-cookie'];
  106. if (setCookie) {
  107. // 将字符串转换为数组
  108. const cookieArray = Array.isArray(setCookie)
  109. ? setCookie
  110. : [setCookie];
  111. cookies.push(...cookieArray);
  112. }
  113. // 查找JSESSIONID
  114. for (const cookie of cookies) {
  115. const match = cookie.match(/JSESSIONID=([^;]+)/i);
  116. if (match && match[1]) {
  117. const newSessionId = match[1];
  118. // 更新JSESSIONID
  119. if (newSessionId !== this.jsessionid) {
  120. this.jsessionid = newSessionId;
  121. wx.removeStorageSync('JSESSIONID');
  122. wx.setStorageSync('JSESSIONID', newSessionId);
  123. }
  124. break;
  125. }
  126. }
  127. } catch (error) {
  128. console.error('处理会话Cookie时出错:', error);
  129. }
  130. }
  131. }
  132. export { HTTP };