let COMMON_FALSE = -1
let AlERT_CODE = 201
let AlERT_RETURN_CODE = 202
let AlERT_REFRESH_CODE = 203
let CONFIRM_RETURN_CODE = 211
let OAUTH_CODE = 101
let NO_POWER_CODE = 401
// let URL = wx.config.URL
class HttpClass {
constructor() {
this.api = null;
this.errFunc = null;
this.succFuc = null;
this.finallyFunc = null;
this.abnormalCodeFunc = null;
this.data = null;
this.isShowLoading = true;
this.header = {
'content-type': 'application/json'
}
}
defaultAbnormalCodeHandler(res) {
switch (res.data.errcode) {
case COMMON_FALSE:
wx.showToast({
title: res.data.errmsg,
icon: 'none'
})
break;
case AlERT_CODE:
wx.showModal({
title: '温馨提示',
content: res.data.errmsg,
showCancel: false
})
break;
case AlERT_RETURN_CODE:
wx.showModal({
title: '温馨提示',
content: res.data.errmsg,
showCancel: false,
confirmText: '返回',
success: () => {
wx.navigateBack(1)
}
})
break;
case AlERT_REFRESH_CODE:
//todo
break;
case CONFIRM_RETURN_CODE:
wx.showModal({
title: '温馨提示',
content: res.data.errmsg,
showCancel: false,
confirmText: '返回',
success: () => {
wx.navigateBack(1)
}
})
break;
case NO_POWER_CODE:
wx.showModal({
title: '权限提示',
content: res.data.errmsg,
showCancel: false,
confirmText: '知道了'
})
break;
case OAUTH_CODE:
wx.Global.setToken('')
new Interceptors().login(() => {
})
//todo token
break;
default:
wx.showToast({
title: '系统异常',
icon: 'none'
})
break
}
}
onAbnormalCode(func) {
this.abnormalCodeFunc = func;
return this;
}
/**
* 禁用loading
*/
disableLoading() {
this.isShowLoading = false;
return this;
}
/**
* 设置请求地址和参数
* @param api
* @param data
* @returns {HttpClass}
*/
setApi(api, data = {}) {
if (wx.BaseClass.isNullOrEmpty(api.method)) {
if (api.indexOf('https') != -1) {
this.api = {
method: 'POST',
url: api
};
} else {
this.api = {
method: 'POST',
url: `${wx.Config.URL}${api}`
};
}
} else {
if (api.url.indexOf('https') != -1) {
this.api = api
} else {
api.url = `${wx.Config.URL}${api.url}`
this.api = api;
}
this.api = api
}
this.data = data
return this
}
/**
* 设置成功方法
* @param succ
* @returns {HttpClass}
*/
onSucc(succ) {
this.succFuc = succ
return this
}
/**
* 设置失败方法
* @param errorFunc
* @returns {HttpClass}
*/
onError(errorFunc) {
this.errFunc = errorFunc
return this
}
onFinally(finallyFunc) {
this.finallyFunc = finallyFunc
return this
}
showLoading() {
wx.showLoading({
title: '加载中',
})
}
hideLoading() {
wx.hideLoading()
}
HttpRequest(jsonData = null) {
new Interceptors().login(() => {
this.runHttpRequest();
});
}
runHttpRequest() {
if (this.isShowLoading) {
this.showLoading()
}
if (!wx.BaseClass.isNullOrEmpty(wx.Global.getToken())) {
this.header.token = wx.Global.getToken();
}
wx.request({
url: this.api.url, //仅为示例,并非真实的接口地址
method: this.api.method,
data: this.data,
header: this.header,
success: (res) => {
if (res.data.errcode === 0) {
if (typeof (this.succFuc) === 'function') {
try {
this.succFuc(res)
} catch (e) {
console.log('哇~处理请求的onSucc出错了,请检查onSucc的代码!');
console.log(e);
}
}
} else {
if (typeof(this.abnormalCodeFunc) === 'function') {
this.abnormalCodeFunc(res)
} else {
console.log('this.api.url')
console.log(this.api.url)
this.defaultAbnormalCodeHandler(res)
}
}
},
fail: (err) => {
console.log('哇~请求出错了!');
console.log(err);
if (typeof (this.errFunc) === 'function') {
this.errFunc(err)
} else {
wx.showToast({
title: '服务器异常,请稍后重试',
icon: 'none'
})
}
},
complete: (res) => {
if (typeof (this.finallyFunc) === 'function') {
this.finallyFunc(res)
}
if (this.isShowLoading) {
this.hideLoading()
}
}
})
}
}
export default HttpClass