123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- class HTTP {
- constructor() {
- this.urls = [{
- name: '测试',
- url: "https://crm.meida.com:16691"
- }, {
- name: '8300',
- url: "http://61.164.207.46:8300"
- }, {
- 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()
- }
- this.formatNumber = (num, decimalPlaces = 2) => {
- if (!num && num !== 0) return '';
- let [integer, decimal] = String(num.toFixed(decimalPlaces)).split('.');
- integer = integer.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
- return decimal ? `${integer}.${decimal}` : integer;
- }
- //得到缩略图或者压缩图 getType默认得到缩略图传true得到压缩图
- this.getSpecifiedImage = (obj, getType = false) => {
- try {
- let type = getType ? 'compressed' : 'thumbnail';
- let imgObj = obj.subfiles.find(v => v.type == type);
- return imgObj.url || obj.url;
- } catch (error) {
- return obj.url;
- }
- }
- if (process.env.NODE_ENV === 'development') {
- this.baseUrl = this.urls[0].url;
- } else {
- this.baseUrl = this.urls[1].url;
- }
- // 从本地存储加载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);
- });
- }
- _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;
- }
- // 会话过期处理
- 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',
- })
- }
- });
- }
- }
- }
- });
- }
- // 处理会话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;
- uni.removeStorageSync('JSESSIONID');
- uni.setStorageSync('JSESSIONID', newSessionId);
- }
- break;
- }
- }
- } catch (error) {
- console.error('处理会话Cookie时出错:', error);
- }
- }
- }
- export { HTTP };
|