|
|
@@ -1,174 +1,153 @@
|
|
|
-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"
|
|
|
- }];
|
|
|
- this.updateList = (content, getList) => {
|
|
|
- content.copyContent = JSON.parse(JSON.stringify(content));
|
|
|
- content.pageSize = (content.pageNumber - 1) * (content.pageSize || 20); // 确保pageSize存在
|
|
|
- content.pageNumber = 1;
|
|
|
- getList()
|
|
|
- }
|
|
|
-
|
|
|
- //得到缩略图或者压缩图 getType默认得到缩略图传true得到压缩图
|
|
|
- this.getSpecifiedImage = (obj, getType = false) => {
|
|
|
- let type = getType ? 'compressed' : 'thumbnail';
|
|
|
- let imgObj = obj.subfiles.find(v => v.type == type);
|
|
|
- return imgObj.url;
|
|
|
- }
|
|
|
+import axios from "axios";
|
|
|
|
|
|
- // if (process.env.NODE_ENV === 'development') {
|
|
|
- // this.baseUrl = this.urls[0].url;
|
|
|
- // } else {
|
|
|
- // this.baseUrl = this.urls[1].url;
|
|
|
- // }
|
|
|
-
|
|
|
- this.baseUrl = "/api";
|
|
|
-
|
|
|
- // 从本地存储加载JSESSIONID
|
|
|
- this.jsessionid = uni.getStorageSync('JSESSIONID') || '';
|
|
|
- console.log("接口地址", this.baseUrl);
|
|
|
- }
|
|
|
-
|
|
|
- request({
|
|
|
- url,
|
|
|
- data = {},
|
|
|
- method = "POST",
|
|
|
- header = {
|
|
|
- 'content-type': 'application/json',
|
|
|
- "accesstoken": data.accesstoken || uni.getStorageSync('userMsg')?.token || '',
|
|
|
- // 添加Cookie字段
|
|
|
- // "Cookie": `JSESSIONID=${this.jsessionid}`
|
|
|
- },
|
|
|
- showLoading = ''
|
|
|
- }) {
|
|
|
- // 如果data.content.pageNumber存在但pageTotal不存在,则设置pageTotal为1
|
|
|
- // 如果pageNumber大于pageTotal,则直接返回空结果
|
|
|
- // 在接口中拿到的pageNumber自动加1,目的是在这里直接处理分页逻辑
|
|
|
- try {
|
|
|
- if (data.content.pageNumber) {
|
|
|
- if (data.content.pageNumber > data.content.pageTotal) return new Promise((resolve) => resolve({ code: 0 }));;
|
|
|
- }
|
|
|
- } catch (error) { }
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- this._request(url, resolve, reject, data, method, header, showLoading);
|
|
|
- });
|
|
|
+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;
|
|
|
}
|
|
|
|
|
|
- _request(url, resolve, reject, data, method, header, showLoading) {
|
|
|
- if (showLoading) uni.showLoading({
|
|
|
- title: showLoading,
|
|
|
- mask: true
|
|
|
- });
|
|
|
-
|
|
|
- uni.request({
|
|
|
- url: this.baseUrl + '/yos/rest' + url,
|
|
|
- data: data,
|
|
|
- method: method,
|
|
|
- header: header,
|
|
|
- timeout: 60000,
|
|
|
- success: (res) => {
|
|
|
- // 提取并保存JSESSIONID
|
|
|
- this._handleSessionCookies(res);
|
|
|
- try {
|
|
|
- if (res.data.pageNumber) {
|
|
|
- res.data.firstPage = res.data.pageNumber === 1; // 判断是否为第一页
|
|
|
- if (data.content.copyContent) {
|
|
|
- res.data.pageNumber = data.content.copyContent.pageNumber;
|
|
|
- res.data.pageTotal = data.content.copyContent.pageTotal;
|
|
|
- data.content.pageSize = data.content.copyContent.pageSize;
|
|
|
- delete data.content.copyContent
|
|
|
- } else {
|
|
|
- res.data.pageNumber++
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (error) {
|
|
|
-
|
|
|
- }
|
|
|
- resolve(res.data);
|
|
|
- },
|
|
|
- fail: (err) => {
|
|
|
- reject(err);
|
|
|
- },
|
|
|
- complete: (res) => {
|
|
|
- if (showLoading) uni.hideLoading();
|
|
|
-
|
|
|
- // 网络错误处理
|
|
|
- if (res.errMsg !== 'request:ok') {
|
|
|
- uni.showToast({
|
|
|
- title: '网络异常,请稍后再试',
|
|
|
- icon: "none"
|
|
|
- });
|
|
|
- return;
|
|
|
- }
|
|
|
+ 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 (res.data.msg === '登陆状态已过期,请重新登陆!') {
|
|
|
- // 清除过期会话ID
|
|
|
- this.jsessionid = '';
|
|
|
- uni.removeStorageSync('JSESSIONID');
|
|
|
- let currentPages = getCurrentPages()[getCurrentPages().length - 1];
|
|
|
- // 如果当前页面不是登录页面,则跳转到登录页面
|
|
|
- if (currentPages.route !== 'pages/login/login') {
|
|
|
- uni.showModal({
|
|
|
- title: '提示',
|
|
|
- content: '您的登录状态已过期,请重新登录。',
|
|
|
- showCancel: false,
|
|
|
- success: () => {
|
|
|
- uni.redirectTo({
|
|
|
- url: '/pages/login/login',
|
|
|
- })
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
+ if (showLoading) {
|
|
|
+ uni.showLoading({ title: showLoading, mask: true });
|
|
|
}
|
|
|
|
|
|
- // 处理会话Cookie
|
|
|
- _handleSessionCookies(res) {
|
|
|
+ return this.instance({
|
|
|
+ url,
|
|
|
+ method,
|
|
|
+ data
|
|
|
+ })
|
|
|
+ .then((res) => {
|
|
|
+ let result = res.data;
|
|
|
+ // 分页处理
|
|
|
try {
|
|
|
- const cookies = res.cookies || [];
|
|
|
- const headers = res.header || {};
|
|
|
-
|
|
|
- // 检查响应头中的Set-Cookie
|
|
|
- const setCookie = headers['Set-Cookie'] || headers['set-cookie'];
|
|
|
- if (setCookie) {
|
|
|
- // 将字符串转换为数组
|
|
|
- const cookieArray = Array.isArray(setCookie)
|
|
|
- ? setCookie
|
|
|
- : [setCookie];
|
|
|
-
|
|
|
- cookies.push(...cookieArray);
|
|
|
+ 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++;
|
|
|
}
|
|
|
-
|
|
|
- // 查找JSESSIONID
|
|
|
- for (const cookie of cookies) {
|
|
|
- const match = cookie.match(/JSESSIONID=([^;]+)/i);
|
|
|
- if (match && match[1]) {
|
|
|
- const newSessionId = match[1];
|
|
|
-
|
|
|
- // 更新JSESSIONID
|
|
|
- if (newSessionId !== this.jsessionid) {
|
|
|
- this.jsessionid = newSessionId;
|
|
|
- uni.removeStorageSync('JSESSIONID');
|
|
|
- uni.setStorageSync('JSESSIONID', newSessionId);
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
+ }
|
|
|
+ } 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);
|
|
|
}
|
|
|
- } catch (error) {
|
|
|
- console.error('处理会话Cookie时出错:', error);
|
|
|
+ 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 };
|
|
|
+export { HTTP };
|