| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 | 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;        }        // 从本地存储加载JSESSIONID        this.jsessionid = wx.getStorageSync('JSESSIONID') || '';        console.log("接口地址", this.baseUrl);    }    request({        url,        data = {},        method = "POST",        header = {            '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) => {                // 提取并保存JSESSIONID                this._handleSessionCookies(res);                resolve(res.data);            },            fail: (err) => {                reject(err);            },            complete: (res) => {                if (showLoading) uni.hideLoading();                // 网络错误处理                if (res.errMsg !== 'request:ok') {                    uni.showToast({                        title: '网络异常,请稍后再试',                        icon: "none"                    });                    return;                }                // 会话过期处理                if (res.data?.msg === '登陆状态已过期,请重新登陆!') {                    // 清除过期会话ID                    this.jsessionid = '';                    wx.removeStorageSync('JSESSIONID');                    uni.redirectTo({                        url: '/pages/login/login',                        success: () => {                            uni.showToast({                                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 };
 |