Http.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. this.updateList = (content, getList) => {
  14. content.copyContent = JSON.parse(JSON.stringify(content));
  15. content.pageSize = (content.pageNumber - 1) * (content.pageSize || 20); // 确保pageSize存在
  16. content.pageNumber = 1;
  17. getList()
  18. }
  19. //得到缩略图或者压缩图 getType默认得到缩略图传true得到压缩图
  20. this.getSpecifiedImage = (obj, getType = false) => {
  21. let type = getType ? 'compressed' : 'thumbnail';
  22. let imgObj = obj.subfiles.find(v => v.type == type);
  23. return imgObj.url;
  24. }
  25. if (process.env.NODE_ENV === 'development') {
  26. this.baseUrl = this.urls[0].url;
  27. } else {
  28. this.baseUrl = this.urls[1].url;
  29. }
  30. // 从本地存储加载JSESSIONID
  31. this.jsessionid = wx.getStorageSync('JSESSIONID') || '';
  32. console.log("接口地址", this.baseUrl);
  33. }
  34. request({
  35. url,
  36. data = {},
  37. method = "POST",
  38. header = {
  39. 'content-type': 'application/json',
  40. "accesstoken": data.accesstoken || wx.getStorageSync('userMsg')?.token || '',
  41. // 添加Cookie字段
  42. "Cookie": `JSESSIONID=${this.jsessionid}`
  43. },
  44. showLoading = ''
  45. }) {
  46. // 如果data.content.pageNumber存在但pageTotal不存在,则设置pageTotal为1
  47. // 如果pageNumber大于pageTotal,则直接返回空结果
  48. // 在接口中拿到的pageNumber自动加1,目的是在这里直接处理分页逻辑
  49. try {
  50. if (data.content.pageNumber) {
  51. if (data.content.pageNumber > data.content.pageTotal) return new Promise((resolve) => resolve({ code: 0 }));;
  52. }
  53. } catch (error) { }
  54. return new Promise((resolve, reject) => {
  55. this._request(url, resolve, reject, data, method, header, showLoading);
  56. });
  57. }
  58. _request(url, resolve, reject, data, method, header, showLoading) {
  59. if (showLoading) uni.showLoading({
  60. title: showLoading,
  61. mask: true
  62. });
  63. uni.request({
  64. url: this.baseUrl + '/yos/rest' + url,
  65. data: data,
  66. method: method,
  67. header: header,
  68. timeout: 60000,
  69. success: (res) => {
  70. // 提取并保存JSESSIONID
  71. this._handleSessionCookies(res);
  72. try {
  73. if (res.data.pageNumber) {
  74. res.data.firstPage = res.data.pageNumber === 1; // 判断是否为第一页
  75. if (data.content.copyContent) {
  76. res.data.pageNumber = data.content.copyContent.pageNumber;
  77. res.data.pageTotal = data.content.copyContent.pageTotal;
  78. data.content.pageSize = data.content.copyContent.pageSize;
  79. delete data.content.copyContent
  80. } else {
  81. res.data.pageNumber++
  82. }
  83. }
  84. } catch (error) {
  85. }
  86. resolve(res.data);
  87. },
  88. fail: (err) => {
  89. reject(err);
  90. },
  91. complete: (res) => {
  92. if (showLoading) uni.hideLoading();
  93. // 网络错误处理
  94. if (res.errMsg !== 'request:ok') {
  95. uni.showToast({
  96. title: '网络异常,请稍后再试',
  97. icon: "none"
  98. });
  99. return;
  100. }
  101. // 会话过期处理
  102. if (res.data.msg === '登陆状态已过期,请重新登陆!') {
  103. // 清除过期会话ID
  104. this.jsessionid = '';
  105. wx.removeStorageSync('JSESSIONID');
  106. let currentPages = getCurrentPages()[getCurrentPages().length - 1];
  107. // 如果当前页面不是登录页面,则跳转到登录页面
  108. if (currentPages.route !== 'pages/login/login') {
  109. uni.showModal({
  110. title: '提示',
  111. content: '您的登录状态已过期,请重新登录。',
  112. showCancel: false,
  113. success: () => {
  114. wx.redirectTo({
  115. url: '/pages/login/login',
  116. })
  117. }
  118. });
  119. }
  120. }
  121. }
  122. });
  123. }
  124. // 处理会话Cookie
  125. _handleSessionCookies(res) {
  126. try {
  127. const cookies = res.cookies || [];
  128. const headers = res.header || {};
  129. // 检查响应头中的Set-Cookie
  130. const setCookie = headers['Set-Cookie'] || headers['set-cookie'];
  131. if (setCookie) {
  132. // 将字符串转换为数组
  133. const cookieArray = Array.isArray(setCookie)
  134. ? setCookie
  135. : [setCookie];
  136. cookies.push(...cookieArray);
  137. }
  138. // 查找JSESSIONID
  139. for (const cookie of cookies) {
  140. const match = cookie.match(/JSESSIONID=([^;]+)/i);
  141. if (match && match[1]) {
  142. const newSessionId = match[1];
  143. // 更新JSESSIONID
  144. if (newSessionId !== this.jsessionid) {
  145. this.jsessionid = newSessionId;
  146. wx.removeStorageSync('JSESSIONID');
  147. wx.setStorageSync('JSESSIONID', newSessionId);
  148. }
  149. break;
  150. }
  151. }
  152. } catch (error) {
  153. console.error('处理会话Cookie时出错:', error);
  154. }
  155. }
  156. }
  157. export { HTTP };