Http.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.formatNumber = (num, decimalPlaces = 2) => {
  18. if (!num && num !== 0) return '';
  19. let [integer, decimal] = String(num.toFixed(decimalPlaces)).split('.');
  20. integer = integer.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  21. return decimal ? `${integer}.${decimal}` : integer;
  22. }
  23. //得到缩略图或者压缩图 getType默认得到缩略图传true得到压缩图
  24. this.getSpecifiedImage = (obj, getType = false) => {
  25. obj.url = this.getImageUrl(obj.url)
  26. try {
  27. let type = getType ? 'compressed' : 'thumbnail';
  28. let imgObj = obj.subfiles.find(v => v.type == type);
  29. return this.getImageUrl(imgObj.url || obj.url);
  30. } catch (error) {
  31. return this.getImageUrl(obj.url);
  32. }
  33. }
  34. // 判断图片是本地还是云存储
  35. this.getImageUrl = (url) => {
  36. if (!url) return '';
  37. //判断url中是否存在http,没有的话要拼接 this.baseUrl
  38. if (!/^https?:\/\//.test(url)) {
  39. //获取当前域名
  40. url = window.location.origin + url;
  41. }
  42. return url;
  43. }
  44. // 请求拦截器
  45. this.instance.interceptors.request.use((config) => {
  46. config.headers = config.headers || {};
  47. config.headers["content-type"] = "application/json";
  48. config.headers["accesstoken"] = uni.getStorageSync("WuserMsg")?.token || "";
  49. if (this.jsessionid) {
  50. config.headers["Cookie"] = `JSESSIONID=${this.jsessionid}`;
  51. }
  52. return config;
  53. });
  54. // 响应拦截器
  55. this.instance.interceptors.response.use(
  56. (response) => {
  57. this._handleSessionCookies(response);
  58. this._checkSessionExpired(response);
  59. return response;
  60. },
  61. (error) => Promise.reject(error)
  62. );
  63. }
  64. /**
  65. * 发送请求
  66. */
  67. request({ url, data = {}, method = "POST", showLoading = "" }) {
  68. // 分页边界处理
  69. if (data.content?.pageNumber && data.content.pageTotal) {
  70. if (data.content.pageNumber > data.content.pageTotal) {
  71. return Promise.resolve({ code: 0 });
  72. }
  73. }
  74. if (showLoading) {
  75. uni.showLoading({ title: showLoading, mask: true });
  76. }
  77. return this.instance({ url, method, data })
  78. .then((res) => {
  79. let result = res.data;
  80. // 分页处理
  81. if (result.pageNumber) {
  82. result.firstPage = result.pageNumber === 1;
  83. if (data.content?.copyContent) {
  84. result.pageNumber = data.content.copyContent.pageNumber;
  85. result.pageTotal = data.content.copyContent.pageTotal;
  86. data.content.pageSize = data.content.copyContent.pageSize;
  87. delete data.content.copyContent;
  88. } else {
  89. result.pageNumber++;
  90. }
  91. }
  92. return result;
  93. })
  94. .catch((err) => {
  95. uni.showToast({ title: "网络异常,请稍后再试", icon: "none" });
  96. throw err;
  97. })
  98. .finally(() => {
  99. if (showLoading) uni.hideLoading();
  100. });
  101. }
  102. /**
  103. * 处理 JSESSIONID
  104. */
  105. _handleSessionCookies(res) {
  106. const setCookie = res.headers["set-cookie"] || res.headers["Set-Cookie"];
  107. if (setCookie) {
  108. const cookieArray = Array.isArray(setCookie) ? setCookie : [setCookie];
  109. for (const cookie of cookieArray) {
  110. const match = cookie.match(/JSESSIONID=([^;]+)/i);
  111. if (match && match[1]) {
  112. const newSessionId = match[1];
  113. if (newSessionId !== this.jsessionid) {
  114. this.jsessionid = newSessionId;
  115. uni.setStorageSync("JSESSIONID", newSessionId);
  116. }
  117. break;
  118. }
  119. }
  120. }
  121. }
  122. /**
  123. * 会话过期处理
  124. */
  125. _checkSessionExpired(res) {
  126. if (res.data?.msg === "登陆状态已过期,请重新登陆!") {
  127. this.isLoad = false;
  128. }
  129. return
  130. if (res.data?.msg === "登陆状态已过期,请重新登陆!") {
  131. this.jsessionid = "";
  132. uni.removeStorageSync("JSESSIONID");
  133. let currentPages = getCurrentPages();
  134. let currentPage = currentPages[currentPages.length - 1];
  135. if (currentPage.route !== "pages/login/login") {
  136. uni.showModal({
  137. title: "提示",
  138. content: "您的登录状态已过期,请重新登录。",
  139. showCancel: false,
  140. success: () => {
  141. uni.redirectTo({ url: "/pages/login/login" });
  142. }
  143. });
  144. }
  145. }
  146. }
  147. }
  148. export { HTTP };