initital commit
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
|
||||
let headerDefault = {
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
let headerDefault1 = {
|
||||
'content-type': 'application/x-www-form-urlencoded'
|
||||
}
|
||||
let TokenKey = 'X-Access-Token'
|
||||
let baseUrl = 'http://10.0.1.63:8080'
|
||||
// let baseUrl = 'http://localhost:8080'
|
||||
const http = (method, url, data, header= headerDefault) => {
|
||||
let Authorization = wx.getStorageSync(TokenKey) ? wx.getStorageSync(TokenKey) : '';
|
||||
header[TokenKey] = Authorization == '' ? '' : JSON.parse(Authorization)
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.showLoading({
|
||||
title: '加载中...',
|
||||
mask: true
|
||||
})
|
||||
wx.request({
|
||||
method: method,
|
||||
url: baseUrl + url,
|
||||
header: header,
|
||||
data: data,
|
||||
success: res => {
|
||||
console.log(res)
|
||||
if (res.statusCode === 200) {
|
||||
// 接口请求错误的时候提示
|
||||
if((res.data.code !== 200 && res.data.code !== 0) && (res.data.message)) {
|
||||
wx.showToast({
|
||||
title: res.data.message,
|
||||
icon: "none",
|
||||
duration: 800,
|
||||
mask: true
|
||||
});
|
||||
}
|
||||
// 200: 服务端业务处理正常结束
|
||||
resolve(res.data)
|
||||
wx.hideLoading();
|
||||
} else {
|
||||
console.error(res.data);
|
||||
reject(res.data);
|
||||
wx.hideLoading();
|
||||
wx.showToast({
|
||||
title: "登录失效,请重新登录",
|
||||
icon: "none",
|
||||
duration: 1500,
|
||||
mask: true
|
||||
});
|
||||
wx.navigateTo({
|
||||
url: '/pages/login/login',
|
||||
})
|
||||
console.error("登录失效3");
|
||||
}
|
||||
},
|
||||
fail: err => {
|
||||
console.error(err);
|
||||
reject(err);
|
||||
wx.hideLoading();
|
||||
},
|
||||
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
get: (url, data, header) => http('GET', url, data, header),
|
||||
post: (url, data, header) => http('POST', url, data, header),
|
||||
post1: (url, data, header) => http('POST', url, data, headerDefault1),
|
||||
put: (url, data, header) => http('PUT', url, data, header),
|
||||
delete: (url, data, header) => http('DELETE', url, data, header)
|
||||
}
|
||||
Reference in New Issue
Block a user