// import {config} from "./config"; // let HttpHost = wx.getExtConfigSync().HttpHost; // HttpHost.indexOf('https://') > -1 ? HttpHost : (HttpHost = 'https://' + HttpHost); // let WssHost = wx.getExtConfigSync().WssHost; // WssHost.indexOf('wss://') > -1 ? WssHost : (WssHost = 'wss://' + WssHost); // let AppId = wx.getExtConfigSync().AppId; // console.log(HttpHost); // console.log(WssHost); // console.log(AppId); const app = getApp(); class Http { // constructor(app){ // this.app = app; // } request({url, data = {}, method = 'GET'}) { return new Promise((resolve, reject) => { this._request(url, resolve, reject, data, method); }); } _request(url, resolve, reject, data = {}, method = 'GET') { wx.request({ url: this._getUrl(url), data: data, header: { "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID') }, method: method, success: (res) => { if (this._requestError(res)) { resolve(res.data); } else{ reject(res.data); } }, fail: (err) => { reject(err); this._requestFail(err, url); } }); } _getUrl(url, params) { // AppId: app.globalData.AppId // url = app.globalData.reqBase + url; // // if (app.globalData.AppId != null) { // url = url + (url.indexOf('?') >= 0 ? "&" : "?") + "AppId=" + app.globalData.AppId + '&wx=3'; // } // if (params != null) { // url = url + (url.indexOf('?') >= 0 ? "&" : "?") + params; // } // return url; url = app.globalData.reqBase + url; if(params != null){ url = url + (url.indexOf('?') >= 0 ? "&" : "?") + params; } if (app.globalData.AppId != null && url.indexOf('AppId=') < 0) { url = url + (url.indexOf('?') >= 0 ? "&" : "?") + "AppId=" + app.globalData.AppId; } if (url.indexOf('wx=3') < 0) { url = url + (url.indexOf('?') >= 0 ? "&" : "?") +'wx=3'; } if (url.indexOf('FromUserId=') < 0) { url = url + (url.indexOf('?') >= 0 ? "&" : "?") +'FromUserId=' + app.globalData.FromUserId; } return url ; } // 请求成功但是返回错误结果时 _requestError(res) { if (res.data['error']) { if (res.data['error']['warning']) { wx.showModal({ title: '提示', content: res.data['error']['warning'], success: res => { if (res.confirm) { } }, showCancel: false }); } else { for (let key in res.data['error']) { wx.showModal({ title: '提示', content: res.data['error'][key], success: res => { if (res.confirm) { } }, showCancel: false }); } } return false; } return true; // if (res.data['error'] && res.data['error']['warning']) { // wx.showModal({ // title: '提示', // content: res.data['error']['warning'], // success: res => { // if (res.confirm) { // // } // } // }); // return false; // } } //请求失败时 _requestFail(err, url) { console.log('请求失败promise', url); wx.hideLoading(); wx.showModal({ title: url, content: err.errMsg, success: res => { if (res.confirm) { } }, showCancel: false }); wx.showToast({ title: '请求失败!', icon: 'none', duration: 1000 }); } } export {Http};