wx-shop/api/request.js
2025-12-09 22:57:49 +08:00

48 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const baseUrl = 'http://scrm.1024tool.vip/api/';
function request(url, method = 'GET', data = {}) {
const header = {
'content-type': 'application/json',
// 有其他content-type需求加点逻辑判断处理即可
};
// 获取token有就丢进请求头
const tokenString = wx.getStorageSync('access_token');
if (tokenString) {
header.Authorization = `${tokenString}`;
}
return new Promise((resolve, reject) => {
wx.request({
url: baseUrl + url,
method,
data,
dataType: 'json', // 微信官方文档中介绍会对数据进行一次JSON.parse
header,
success(res) {
if (res.data.code) {
if (res.data.code == 10103 || res.data.code == 10101) {
wx.removeStorageSync('access_token');
reject({ code: res.data.code, message: 'token expired' });
return;
}
wx.showToast({
title: res.data.message,
icon: 'none'
});
reject(res.data);
} else {
resolve(res.data);
}
},
fail(err) {
console.log(err)
// 断网、服务器挂了都会fail回调直接reject即可
reject(err);
},
});
});
}
// 导出请求和服务地址
export default request;