Http.js 4.0 KB

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