53 lines
1.0 KiB
JavaScript
53 lines
1.0 KiB
JavaScript
/**
|
||
* 网络请求工具
|
||
*/
|
||
|
||
// API基础地址(开发环境)
|
||
const BASE_URL = 'http://localhost:3000/api';
|
||
|
||
/**
|
||
* 发送HTTP请求
|
||
*/
|
||
export function request(options) {
|
||
return new Promise((resolve, reject) => {
|
||
const timeoutMs = options.timeout || 30000;
|
||
|
||
wx.request({
|
||
url: BASE_URL + options.url,
|
||
method: options.method || 'GET',
|
||
data: options.data || {},
|
||
timeout: timeoutMs,
|
||
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请求
|
||
*/
|
||
export function post(url, data, options = {}) {
|
||
return request({ url, method: 'POST', data, ...options });
|
||
}
|
||
|
||
export default { request, get, post };
|