2025-10-17 17:29:11 +08:00
|
|
|
|
const apiConfig = require('../config/api.js');
|
2025-09-19 17:52:28 +08:00
|
|
|
|
|
2025-10-17 17:29:11 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 封装的HTTP请求工具
|
|
|
|
|
|
*/
|
|
|
|
|
|
class Request {
|
|
|
|
|
|
constructor() {
|
|
|
|
|
|
this.baseURL = apiConfig.baseURL;
|
|
|
|
|
|
this.timeout = apiConfig.timeout;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 发送HTTP请求
|
|
|
|
|
|
* @param {Object} options 请求配置
|
|
|
|
|
|
* @returns {Promise} 请求结果
|
|
|
|
|
|
*/
|
|
|
|
|
|
request(options) {
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
// 获取存储的token
|
|
|
|
|
|
const token = wx.getStorageSync('token');
|
|
|
|
|
|
|
|
|
|
|
|
// 构建请求头
|
|
|
|
|
|
const header = {
|
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
...options.header
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 如果有token,添加到请求头
|
|
|
|
|
|
if (token) {
|
|
|
|
|
|
header['Authorization'] = `Bearer ${token}`;
|
2025-09-19 17:52:28 +08:00
|
|
|
|
}
|
2025-10-17 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
|
// 构建完整URL
|
|
|
|
|
|
const url = options.url.startsWith('http')
|
|
|
|
|
|
? options.url
|
|
|
|
|
|
: `${this.baseURL}${options.url}`;
|
|
|
|
|
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
|
|
wx.request({
|
|
|
|
|
|
url,
|
|
|
|
|
|
method: options.method || 'GET',
|
|
|
|
|
|
data: options.data || {},
|
|
|
|
|
|
header,
|
|
|
|
|
|
timeout: this.timeout,
|
|
|
|
|
|
success: (res) => {
|
|
|
|
|
|
console.log('API请求成功:', url, res);
|
|
|
|
|
|
|
|
|
|
|
|
// 处理HTTP状态码
|
|
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
|
|
// 处理业务状态码
|
|
|
|
|
|
if (res.data && res.data.code !== undefined) {
|
|
|
|
|
|
if (res.data.code === 200 || res.data.code === 0) {
|
|
|
|
|
|
resolve(res.data);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 业务错误
|
|
|
|
|
|
this.handleBusinessError(res.data);
|
|
|
|
|
|
reject(res.data);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 没有业务状态码,直接返回数据
|
|
|
|
|
|
resolve(res.data);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// HTTP错误
|
|
|
|
|
|
this.handleHttpError(res);
|
|
|
|
|
|
reject(res);
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
fail: (err) => {
|
|
|
|
|
|
console.error('API请求失败:', url, err);
|
|
|
|
|
|
this.handleNetworkError(err);
|
|
|
|
|
|
reject(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-09-19 17:52:28 +08:00
|
|
|
|
|
2025-10-17 17:29:11 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* GET请求
|
|
|
|
|
|
*/
|
|
|
|
|
|
get(url, data = {}, options = {}) {
|
|
|
|
|
|
return this.request({
|
|
|
|
|
|
url,
|
|
|
|
|
|
method: 'GET',
|
|
|
|
|
|
data,
|
|
|
|
|
|
...options
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-09-19 17:52:28 +08:00
|
|
|
|
|
2025-10-17 17:29:11 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* POST请求
|
|
|
|
|
|
*/
|
|
|
|
|
|
post(url, data = {}, options = {}) {
|
|
|
|
|
|
return this.request({
|
|
|
|
|
|
url,
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
data,
|
2025-09-19 17:52:28 +08:00
|
|
|
|
...options
|
2025-10-17 17:29:11 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-09-19 17:52:28 +08:00
|
|
|
|
|
2025-10-17 17:29:11 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* PUT请求
|
|
|
|
|
|
*/
|
|
|
|
|
|
put(url, data = {}, options = {}) {
|
|
|
|
|
|
return this.request({
|
|
|
|
|
|
url,
|
|
|
|
|
|
method: 'PUT',
|
|
|
|
|
|
data,
|
|
|
|
|
|
...options
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-09-19 17:52:28 +08:00
|
|
|
|
|
2025-10-17 17:29:11 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* DELETE请求
|
|
|
|
|
|
*/
|
|
|
|
|
|
delete(url, data = {}, options = {}) {
|
|
|
|
|
|
return this.request({
|
|
|
|
|
|
url,
|
|
|
|
|
|
method: 'DELETE',
|
|
|
|
|
|
data,
|
|
|
|
|
|
...options
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-09-19 17:52:28 +08:00
|
|
|
|
|
2025-10-17 17:29:11 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 处理业务错误
|
|
|
|
|
|
*/
|
|
|
|
|
|
handleBusinessError(data) {
|
|
|
|
|
|
const message = data.message || '请求失败';
|
|
|
|
|
|
|
|
|
|
|
|
// 特殊错误码处理
|
|
|
|
|
|
switch (data.code) {
|
|
|
|
|
|
case 401:
|
|
|
|
|
|
// 未授权,清除token并跳转登录
|
|
|
|
|
|
wx.removeStorageSync('token');
|
|
|
|
|
|
wx.removeStorageSync('userInfo');
|
|
|
|
|
|
wx.showToast({
|
|
|
|
|
|
title: '登录已过期,请重新登录',
|
|
|
|
|
|
icon: 'none'
|
|
|
|
|
|
});
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
wx.reLaunch({
|
|
|
|
|
|
url: '/pages/login/login'
|
|
|
|
|
|
});
|
|
|
|
|
|
}, 1500);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 403:
|
|
|
|
|
|
wx.showToast({
|
|
|
|
|
|
title: '权限不足',
|
|
|
|
|
|
icon: 'none'
|
|
|
|
|
|
});
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
wx.showToast({
|
|
|
|
|
|
title: message,
|
|
|
|
|
|
icon: 'none'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-19 17:52:28 +08:00
|
|
|
|
|
2025-10-17 17:29:11 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 处理HTTP错误
|
|
|
|
|
|
*/
|
|
|
|
|
|
handleHttpError(res) {
|
|
|
|
|
|
let message = '网络请求失败';
|
|
|
|
|
|
|
|
|
|
|
|
switch (res.statusCode) {
|
|
|
|
|
|
case 400:
|
|
|
|
|
|
message = '请求参数错误';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 401:
|
|
|
|
|
|
message = '未授权访问';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 403:
|
|
|
|
|
|
message = '禁止访问';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 404:
|
|
|
|
|
|
message = '请求地址不存在';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 500:
|
|
|
|
|
|
message = '服务器内部错误';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 502:
|
|
|
|
|
|
message = '网关错误';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 503:
|
|
|
|
|
|
message = '服务不可用';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 504:
|
|
|
|
|
|
message = '网关超时';
|
|
|
|
|
|
break;
|
2025-09-19 17:52:28 +08:00
|
|
|
|
}
|
2025-10-17 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
|
wx.showToast({
|
|
|
|
|
|
title: message,
|
|
|
|
|
|
icon: 'none'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-09-19 17:52:28 +08:00
|
|
|
|
|
2025-10-17 17:29:11 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 处理网络错误
|
|
|
|
|
|
*/
|
|
|
|
|
|
handleNetworkError(err) {
|
|
|
|
|
|
let message = '网络连接失败';
|
|
|
|
|
|
|
|
|
|
|
|
if (err.errMsg) {
|
|
|
|
|
|
if (err.errMsg.includes('timeout')) {
|
|
|
|
|
|
message = '请求超时';
|
|
|
|
|
|
} else if (err.errMsg.includes('fail')) {
|
|
|
|
|
|
message = '网络连接失败';
|
|
|
|
|
|
}
|
2025-09-19 17:52:28 +08:00
|
|
|
|
}
|
2025-10-17 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
|
wx.showToast({
|
|
|
|
|
|
title: message,
|
|
|
|
|
|
icon: 'none'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-09-19 17:52:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-17 17:29:11 +08:00
|
|
|
|
// 创建实例
|
|
|
|
|
|
const request = new Request();
|
2025-09-19 17:52:28 +08:00
|
|
|
|
|
2025-10-17 17:29:11 +08:00
|
|
|
|
module.exports = request;
|