120 lines
3.1 KiB
JavaScript
120 lines
3.1 KiB
JavaScript
![]() |
// const ipAddress = "https://wechatapi.yuedongsports.com/"; // 根据实际情况修改
|
||
|
const ipAddress = "https://wechatapi-qa.yuedongsports.com/"; // 根据实际情况修改
|
||
|
// const ipAddress = "http://192.168.0.32:9993/"; // 根据实际情况修改
|
||
|
//const ipAddress = "http://localhost:9993/"; // 根据实际情况修改
|
||
|
|
||
|
function getToken() {
|
||
|
return wx.getStorageSync('token'); // 获取存储的 token
|
||
|
}
|
||
|
|
||
|
// 请求封装函数
|
||
|
function request(url, method, data, loading,nowToken) {
|
||
|
|
||
|
if (loading) {
|
||
|
wx.showLoading({
|
||
|
title: typeof loading === 'string' ? loading : "正在处理...",
|
||
|
});
|
||
|
}
|
||
|
|
||
|
const _header = {};
|
||
|
let _token = getToken();
|
||
|
if(nowToken){
|
||
|
_token = nowToken
|
||
|
}
|
||
|
console.log(_token);
|
||
|
if (_token) {
|
||
|
_header['Authorization'] = `Bearer ${_token}`; // 使用 Bearer Token 格式
|
||
|
}
|
||
|
|
||
|
return new Promise((resolve, reject) => {
|
||
|
wx.request({
|
||
|
url: ipAddress + url, // 请求的接口地址
|
||
|
method: method,
|
||
|
data: data,
|
||
|
header: _header,
|
||
|
success(res) {
|
||
|
wx.hideLoading();
|
||
|
if (res.statusCode === 200) {
|
||
|
resolve(res.data); // 请求成功返回数据
|
||
|
} else if (res.statusCode === 500) {
|
||
|
wx.showToast({
|
||
|
icon: "none",
|
||
|
title: "服务器内部错误",
|
||
|
});
|
||
|
} else if (res.statusCode === 404) {
|
||
|
wx.showToast({
|
||
|
icon: "none",
|
||
|
title: "未找到请求接口",
|
||
|
});
|
||
|
} else if (res.statusCode === 202 || res.statusCode === 401) {
|
||
|
if (res.data && res.data.message) {
|
||
|
wx.showToast({
|
||
|
icon: "none",
|
||
|
title: res.data.message,
|
||
|
});
|
||
|
}
|
||
|
if (res.data.code === 401) {
|
||
|
wx.showModal({
|
||
|
title: '登录信息失效,请重新登录!',
|
||
|
content: '',
|
||
|
showCancel:false,
|
||
|
confirmColor:"#26A6F6",
|
||
|
complete: (res) => {
|
||
|
wx.clearStorageSync();
|
||
|
wx.reLaunch({
|
||
|
url: "/pages/login/login",
|
||
|
});
|
||
|
if (res.cancel) {
|
||
|
|
||
|
}
|
||
|
|
||
|
if (res.confirm) {
|
||
|
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
} else {
|
||
|
wx.showToast({
|
||
|
icon: "none",
|
||
|
title: `未知错误,状态码:${res.statusCode}`,
|
||
|
});
|
||
|
}
|
||
|
},
|
||
|
fail(err) {
|
||
|
wx.hideLoading();
|
||
|
wx.showToast({
|
||
|
icon: "none",
|
||
|
title: "请求失败,请检查网络",
|
||
|
});
|
||
|
reject(err);
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// 简单封装 GET 和 POST 方法
|
||
|
function post(url, data, loading,token) {
|
||
|
return request(url, 'POST', data, loading,token);
|
||
|
}
|
||
|
|
||
|
// 简单封装 GET 和 POST 方法
|
||
|
function postWitthToken(url, data, loading,token) {
|
||
|
return request(url, 'POST', data, loading,token);
|
||
|
}
|
||
|
|
||
|
function get(url, data, loading) {
|
||
|
return request(url, 'GET', data, loading);
|
||
|
}
|
||
|
|
||
|
function getWithToken(url, data, loading,token) {
|
||
|
return request(url, 'GET', data, loading,token);
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
post,
|
||
|
get,
|
||
|
getWithToken,
|
||
|
postWitthToken,
|
||
|
ipAddress
|
||
|
};
|