cyx-ament-wechat/miniprogram/utils/http.js

58 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-08-31 10:38:54 +08:00
// const base_url = "https://breed.nxcyx.com/wx/"; //配置的域名
2023-09-12 16:28:42 +08:00
const base_url = "http://192.168.0.13:8101"; //配置的域名
2023-08-31 10:38:54 +08:00
// 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",
2023-09-06 15:30:30 +08:00
// "Authorization":""
2023-08-31 10:38:54 +08:00
};
if(token){
2023-09-06 15:30:30 +08:00
// header.Authorization = 'Bearer '+wx.getStorageSync('token');
header.Cookie = wx.getStorageSync('JSessionId')
2023-08-31 10:38:54 +08:00
}
wx.request({
url: base_url + url,
method: method,
data: method === "POST" ? JSON.stringify(data) : data,
header: header,
success(res){
2023-09-06 15:30:30 +08:00
if (url.indexOf('getUserInfo')!==-1){
let JSessionId = res.header["Set-Cookie"].toString().split(';')[0]
wx.setStorageSync("JSessionId", JSessionId);
}
2023-08-31 10:38:54 +08:00
/**
* 请求成功
* 判断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
}