58 lines
1.5 KiB
JavaScript
58 lines
1.5 KiB
JavaScript
// const base_url = "https://breed.nxcyx.com/wx/"; //配置的域名
|
|
const base_url = "http://192.168.0.13:8101"; //配置的域名
|
|
// const base_url = "http://q43k76.natappfree.cc/wx/"
|
|
|
|
/**
|
|
* author:duxp
|
|
* desc:http请求方法封装
|
|
* @param {*} method 请求方式
|
|
* @param {*} url 接口名
|
|
* @param {*} data 参数名
|
|
* @param {*} token 判断是否携带token
|
|
*/
|
|
function request(method,url,data,token){
|
|
return new Promise(function(resolve,reject){
|
|
let header = {
|
|
"content-type":"application/json",
|
|
// "Authorization":""
|
|
};
|
|
if(token){
|
|
// header.Authorization = 'Bearer '+wx.getStorageSync('token');
|
|
header.Cookie = wx.getStorageSync('JSessionId')
|
|
}
|
|
wx.request({
|
|
url: base_url + url,
|
|
method: method,
|
|
data: method === "POST" ? JSON.stringify(data) : data,
|
|
header: header,
|
|
success(res){
|
|
if (url.indexOf('getUserInfo')!==-1){
|
|
let JSessionId = res.header["Set-Cookie"].toString().split(';')[0]
|
|
wx.setStorageSync("JSessionId", JSessionId);
|
|
}
|
|
/**
|
|
* 请求成功
|
|
* 判断code是否为200
|
|
*/
|
|
if(res.statusCode == 200){
|
|
resolve(res.data);
|
|
}else{
|
|
reject('运行出错,请稍后再试');
|
|
wx.showToast({ //弹出框
|
|
title: res.data.msg,
|
|
icon: 'error',
|
|
duration: 2000
|
|
});
|
|
}
|
|
},
|
|
fail(err) {
|
|
//请求失败
|
|
reject(err)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
module.exports = {
|
|
request: request,
|
|
base_url
|
|
} |