|
@@ -9,62 +9,124 @@ class HTTP {
|
|
|
}, {
|
|
|
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;
|
|
|
}
|
|
|
- console.log("接口地址", this.baseUrl)
|
|
|
+
|
|
|
+ // 从本地存储加载JSESSIONID
|
|
|
+ this.jsessionid = wx.getStorageSync('JSESSIONID') || '';
|
|
|
+ console.log("接口地址", this.baseUrl);
|
|
|
}
|
|
|
+
|
|
|
request({
|
|
|
url,
|
|
|
data = {},
|
|
|
method = "POST",
|
|
|
header = {
|
|
|
- 'content-type': 'application/json'
|
|
|
+ 'content-type': 'application/json',
|
|
|
+ "accesstoken": data.accesstoken || wx.getStorageSync('userMsg')?.token || '',
|
|
|
+ // 添加Cookie字段
|
|
|
+ "Cookie": `JSESSIONID=${this.jsessionid}`
|
|
|
},
|
|
|
showLoading = ''
|
|
|
}) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
this._request(url, resolve, reject, data, method, header, showLoading);
|
|
|
- })
|
|
|
+ });
|
|
|
}
|
|
|
+
|
|
|
_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 => resolve(res.data),
|
|
|
- fial: err => reject(err),
|
|
|
+ success: (res) => {
|
|
|
+ // 提取并保存JSESSIONID
|
|
|
+ this._handleSessionCookies(res);
|
|
|
+ resolve(res.data);
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ reject(err);
|
|
|
+ },
|
|
|
complete: (res) => {
|
|
|
- if (showLoading) uni.hideLoading()
|
|
|
- if (res.errMsg != 'request:ok') {
|
|
|
+ if (showLoading) uni.hideLoading();
|
|
|
+
|
|
|
+ // 网络错误处理
|
|
|
+ if (res.errMsg !== 'request:ok') {
|
|
|
uni.showToast({
|
|
|
title: '网络异常,请稍后再试',
|
|
|
icon: "none"
|
|
|
- })
|
|
|
- } else if (res.data.msg == '登陆状态已过期,请重新登陆!') {
|
|
|
+ });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 会话过期处理
|
|
|
+ if (res.data?.msg === '登陆状态已过期,请重新登陆!') {
|
|
|
+ // 清除过期会话ID
|
|
|
+ this.jsessionid = '';
|
|
|
+ wx.removeStorageSync('JSESSIONID');
|
|
|
+
|
|
|
uni.redirectTo({
|
|
|
url: '/pages/login/login',
|
|
|
- success() {
|
|
|
+ success: () => {
|
|
|
uni.showToast({
|
|
|
- title: res.msg,
|
|
|
+ title: res.data.msg,
|
|
|
icon: "none"
|
|
|
- })
|
|
|
+ });
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
- })
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理会话Cookie
|
|
|
+ _handleSessionCookies(res) {
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查找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;
|
|
|
+ wx.removeStorageSync('JSESSIONID');
|
|
|
+ wx.setStorageSync('JSESSIONID', newSessionId);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error('处理会话Cookie时出错:', error);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
-export {
|
|
|
- HTTP
|
|
|
-}
|
|
|
+
|
|
|
+export { HTTP };
|