| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- import axios from "axios";
- class HTTP {
- constructor() {
- this.urls = [
- { name: "测试", url: "http://61.164.207.46:8300" },
- { name: "正式", url: "https://crm.meida.com:16691" },
- { name: "楚楚", url: "https://cucu.cnyunl.com:8079" }
- ];
- if (process.env.NODE_ENV === "development") {
- this.baseUrl = this.urls[0].url;
- } else {
- this.baseUrl = this.urls[1].url;
- }
- this.jsessionid = uni.getStorageSync("JSESSIONID") || "";
- // 工具方法 - 更新分页列表
- this.updateList = (content, getList) => {
- content.copyContent = JSON.parse(JSON.stringify(content));
- content.pageSize = (content.pageNumber - 1) * (content.pageSize || 20);
- content.pageNumber = 1;
- getList();
- };
- // 工具方法 - 获取缩略图 / 压缩图
- this.getSpecifiedImage = (obj, getType = false) => {
- let type = getType ? "compressed" : "thumbnail";
- let imgObj = obj.subfiles.find(v => v.type == type);
- return imgObj?.url;
- };
- // 创建 axios 实例
- this.instance = axios.create({
- baseURL: "/yos/rest",
- withCredentials: true
- });
- // 请求拦截器 - 自动加 JSESSIONID
- this.instance.interceptors.request.use((config) => {
- if (!config.headers) config.headers = {};
- config.headers["content-type"] = "application/json";
- config.headers["accesstoken"] = uni.getStorageSync("userMsg")?.token || "";
- if (this.jsessionid) {
- config.headers["Cookie"] = `JSESSIONID=${this.jsessionid}`;
- }
- return config;
- });
- // 响应拦截器 - 自动更新 JSESSIONID + 会话过期处理
- this.instance.interceptors.response.use((response) => {
- this._handleSessionCookies(response);
- this._checkSessionExpired(response);
- return response;
- }, (error) => {
- return Promise.reject(error);
- });
- console.log("接口地址", this.baseUrl);
- }
- request({ url, data = {}, method = "POST", showLoading = "" }) {
- // 分页边界处理
- try {
- if (data.content?.pageNumber && data.content.pageTotal) {
- if (data.content.pageNumber > data.content.pageTotal) {
- return Promise.resolve({ code: 0 });
- }
- }
- } catch (e) { }
- if (showLoading) {
- uni.showLoading({ title: showLoading, mask: true });
- }
- return this.instance({
- url,
- method,
- data
- })
- .then((res) => {
- let result = res.data;
- // 分页处理
- try {
- if (result.pageNumber) {
- result.firstPage = result.pageNumber === 1;
- if (data.content?.copyContent) {
- result.pageNumber = data.content.copyContent.pageNumber;
- result.pageTotal = data.content.copyContent.pageTotal;
- data.content.pageSize = data.content.copyContent.pageSize;
- delete data.content.copyContent;
- } else {
- result.pageNumber++;
- }
- }
- } catch (e) { }
- return result;
- })
- .catch((err) => {
- uni.showToast({ title: "网络异常,请稍后再试", icon: "none" });
- throw err;
- })
- .finally(() => {
- if (showLoading) uni.hideLoading();
- });
- }
- // 处理 JSESSIONID
- _handleSessionCookies(res) {
- try {
- const setCookie = res.headers["set-cookie"] || res.headers["Set-Cookie"];
- if (setCookie) {
- const cookieArray = Array.isArray(setCookie) ? setCookie : [setCookie];
- for (const cookie of cookieArray) {
- const match = cookie.match(/JSESSIONID=([^;]+)/i);
- if (match && match[1]) {
- const newSessionId = match[1];
- if (newSessionId !== this.jsessionid) {
- this.jsessionid = newSessionId;
- uni.setStorageSync("JSESSIONID", newSessionId);
- }
- break;
- }
- }
- }
- } catch (error) {
- console.error("处理 JSESSIONID 失败:", error);
- }
- }
- // 会话过期处理
- _checkSessionExpired(res) {
- if (res.data?.msg === "登陆状态已过期,请重新登陆!") {
- this.jsessionid = "";
- uni.removeStorageSync("JSESSIONID");
- let currentPages = getCurrentPages();
- let currentPage = currentPages[currentPages.length - 1];
- if (currentPage.route !== "pages/login/login") {
- uni.showModal({
- title: "提示",
- content: "您的登录状态已过期,请重新登录。",
- showCancel: false,
- success: () => {
- uni.redirectTo({ url: "/pages/login/login" });
- }
- });
- }
- }
- }
- }
- export { HTTP };
|