2026-03-03 16:57:49 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 网络请求工具
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
// API基础地址(开发环境)
|
|
|
|
|
|
const BASE_URL = 'http://localhost:3000/api';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 发送HTTP请求
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function request(options) {
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2026-03-05 15:57:51 +08:00
|
|
|
|
const timeoutMs = options.timeout || 30000;
|
|
|
|
|
|
|
2026-03-03 16:57:49 +08:00
|
|
|
|
wx.request({
|
|
|
|
|
|
url: BASE_URL + options.url,
|
|
|
|
|
|
method: options.method || 'GET',
|
|
|
|
|
|
data: options.data || {},
|
2026-03-05 15:57:51 +08:00
|
|
|
|
timeout: timeoutMs,
|
2026-03-03 16:57:49 +08:00
|
|
|
|
header: {
|
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
...options.header
|
|
|
|
|
|
},
|
|
|
|
|
|
success(res) {
|
|
|
|
|
|
if (res.data.code === 0) {
|
|
|
|
|
|
resolve(res.data.data);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
reject(new Error(res.data.message || '请求失败'));
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
fail(err) {
|
|
|
|
|
|
reject(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* GET请求
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function get(url, data) {
|
|
|
|
|
|
return request({ url, method: 'GET', data });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* POST请求
|
|
|
|
|
|
*/
|
2026-03-05 15:57:51 +08:00
|
|
|
|
export function post(url, data, options = {}) {
|
|
|
|
|
|
return request({ url, method: 'POST', data, ...options });
|
2026-03-03 16:57:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default { request, get, post };
|