| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- class HTTP {
- constructor() {
- this.ENV = wx.getAccountInfoSync().miniProgram.envVersion;
- this.urls = [{
- name: "美大正式",
- url: "https://crm.meida.com:16691"
- }, {
- name: "楚楚",
- url: "https://cucu.cnyunl.com:8079"
- }, {
- name: "开发环境",
- url: "http://61.164.207.46:8300"
- }]
- if (this.ENV === 'release') { // 正式版
- this.baseUrl = "https://crm.meida.com:16691";
- } else {
- // this.baseUrl = "http://61.164.207.46:8300";
- this.baseUrl = "https://crm.meida.com:16691";
- }
- //得到缩略图或者压缩图 getType默认得到缩略图传true得到压缩图
- this.getSpecifiedImage = (obj, getType = false) => {
- obj.url = this.getImageUrl(obj.url)
- try {
- let type = getType ? 'compressed' : 'thumbnail';
- let imgObj = obj.subfiles.find(v => v.type == type);
- return this.getImageUrl(imgObj.url || obj.url);
- } catch (error) {
- return this.getImageUrl(obj.url);
- }
- }
- // 判断图片是本地还是云存储
- this.getImageUrl = (url) => {
- if (!url) return '';
- //判断url中是否存在http,没有的话要拼接 this.baseUrl
- if (!/^https?:\/\//.test(url)) {
- url = this.baseUrl + url;
- }
- return 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": `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) wx.showLoading({
- title: showLoading,
- mask: true
- }) */
- wx.request({
- url: this.baseUrl + '/yos/rest/index' + url,
- data,
- method,
- header,
- timeout: 60000,
- success: (res) => {
- resolve(res.data);
- },
- fial: (err) => {
- reject(err);
- },
- complete: (res) => {
- // if (showLoading) wx.hideLoading()
- this._handleSessionCookies(res);
- if (res.errMsg != 'request:ok') {
- wx.showToast({
- title: '网络异常,请重新进入',
- icon: "none"
- })
- } else if (res.data.msg == '登陆状态已过期,请重新登陆!') {
- this.jsessionid = '';
- wx.removeStorageSync('JSESSIONID');
- wx.redirectTo({
- url: '/pages/login/phone',
- });
- wx.showToast({
- title: res.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
- }
|