Http.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import axios from "axios";
  2. class HTTP {
  3. constructor() {
  4. this.urls = [
  5. { name: "测试", url: "http://61.164.207.46:8300" },
  6. { name: "正式", url: "https://crm.meida.com:16691" },
  7. { name: "楚楚", url: "https://cucu.cnyunl.com:8079" }
  8. ];
  9. if (process.env.NODE_ENV === "development") {
  10. this.baseUrl = this.urls[0].url;
  11. } else {
  12. this.baseUrl = this.urls[1].url;
  13. }
  14. this.jsessionid = uni.getStorageSync("JSESSIONID") || "";
  15. // 工具方法 - 更新分页列表
  16. this.updateList = (content, getList) => {
  17. content.copyContent = JSON.parse(JSON.stringify(content));
  18. content.pageSize = (content.pageNumber - 1) * (content.pageSize || 20);
  19. content.pageNumber = 1;
  20. getList();
  21. };
  22. // 工具方法 - 获取缩略图 / 压缩图
  23. this.getSpecifiedImage = (obj, getType = false) => {
  24. let type = getType ? "compressed" : "thumbnail";
  25. let imgObj = obj.subfiles.find(v => v.type == type);
  26. return imgObj?.url;
  27. };
  28. // 创建 axios 实例
  29. this.instance = axios.create({
  30. baseURL: "/yos/rest",
  31. withCredentials: true
  32. });
  33. // 请求拦截器 - 自动加 JSESSIONID
  34. this.instance.interceptors.request.use((config) => {
  35. if (!config.headers) config.headers = {};
  36. config.headers["content-type"] = "application/json";
  37. config.headers["accesstoken"] = uni.getStorageSync("userMsg")?.token || "";
  38. if (this.jsessionid) {
  39. config.headers["Cookie"] = `JSESSIONID=${this.jsessionid}`;
  40. }
  41. return config;
  42. });
  43. // 响应拦截器 - 自动更新 JSESSIONID + 会话过期处理
  44. this.instance.interceptors.response.use((response) => {
  45. this._handleSessionCookies(response);
  46. this._checkSessionExpired(response);
  47. return response;
  48. }, (error) => {
  49. return Promise.reject(error);
  50. });
  51. console.log("接口地址", this.baseUrl);
  52. }
  53. request({ url, data = {}, method = "POST", showLoading = "" }) {
  54. // 分页边界处理
  55. try {
  56. if (data.content?.pageNumber && data.content.pageTotal) {
  57. if (data.content.pageNumber > data.content.pageTotal) {
  58. return Promise.resolve({ code: 0 });
  59. }
  60. }
  61. } catch (e) { }
  62. if (showLoading) {
  63. uni.showLoading({ title: showLoading, mask: true });
  64. }
  65. return this.instance({
  66. url,
  67. method,
  68. data
  69. })
  70. .then((res) => {
  71. let result = res.data;
  72. // 分页处理
  73. try {
  74. if (result.pageNumber) {
  75. result.firstPage = result.pageNumber === 1;
  76. if (data.content?.copyContent) {
  77. result.pageNumber = data.content.copyContent.pageNumber;
  78. result.pageTotal = data.content.copyContent.pageTotal;
  79. data.content.pageSize = data.content.copyContent.pageSize;
  80. delete data.content.copyContent;
  81. } else {
  82. result.pageNumber++;
  83. }
  84. }
  85. } catch (e) { }
  86. return result;
  87. })
  88. .catch((err) => {
  89. uni.showToast({ title: "网络异常,请稍后再试", icon: "none" });
  90. throw err;
  91. })
  92. .finally(() => {
  93. if (showLoading) uni.hideLoading();
  94. });
  95. }
  96. // 处理 JSESSIONID
  97. _handleSessionCookies(res) {
  98. try {
  99. const setCookie = res.headers["set-cookie"] || res.headers["Set-Cookie"];
  100. if (setCookie) {
  101. const cookieArray = Array.isArray(setCookie) ? setCookie : [setCookie];
  102. for (const cookie of cookieArray) {
  103. const match = cookie.match(/JSESSIONID=([^;]+)/i);
  104. if (match && match[1]) {
  105. const newSessionId = match[1];
  106. if (newSessionId !== this.jsessionid) {
  107. this.jsessionid = newSessionId;
  108. uni.setStorageSync("JSESSIONID", newSessionId);
  109. }
  110. break;
  111. }
  112. }
  113. }
  114. } catch (error) {
  115. console.error("处理 JSESSIONID 失败:", error);
  116. }
  117. }
  118. // 会话过期处理
  119. _checkSessionExpired(res) {
  120. if (res.data?.msg === "登陆状态已过期,请重新登陆!") {
  121. this.jsessionid = "";
  122. uni.removeStorageSync("JSESSIONID");
  123. let currentPages = getCurrentPages();
  124. let currentPage = currentPages[currentPages.length - 1];
  125. if (currentPage.route !== "pages/login/login") {
  126. uni.showModal({
  127. title: "提示",
  128. content: "您的登录状态已过期,请重新登录。",
  129. showCancel: false,
  130. success: () => {
  131. uni.redirectTo({ url: "/pages/login/login" });
  132. }
  133. });
  134. }
  135. }
  136. }
  137. }
  138. export { HTTP };