feat: 星域故事汇小游戏初始版本
This commit is contained in:
55
client/js/utils/http.js
Normal file
55
client/js/utils/http.js
Normal file
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* 网络请求工具
|
||||
*/
|
||||
|
||||
// API基础地址(开发环境)
|
||||
const BASE_URL = 'http://localhost:3000/api';
|
||||
|
||||
/**
|
||||
* 发送HTTP请求
|
||||
*/
|
||||
export function request(options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const timeout = setTimeout(() => {
|
||||
reject(new Error('请求超时'));
|
||||
}, 5000);
|
||||
|
||||
wx.request({
|
||||
url: BASE_URL + options.url,
|
||||
method: options.method || 'GET',
|
||||
data: options.data || {},
|
||||
header: {
|
||||
'Content-Type': 'application/json',
|
||||
...options.header
|
||||
},
|
||||
success(res) {
|
||||
clearTimeout(timeout);
|
||||
if (res.data.code === 0) {
|
||||
resolve(res.data.data);
|
||||
} else {
|
||||
reject(new Error(res.data.message || '请求失败'));
|
||||
}
|
||||
},
|
||||
fail(err) {
|
||||
clearTimeout(timeout);
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* GET请求
|
||||
*/
|
||||
export function get(url, data) {
|
||||
return request({ url, method: 'GET', data });
|
||||
}
|
||||
|
||||
/**
|
||||
* POST请求
|
||||
*/
|
||||
export function post(url, data) {
|
||||
return request({ url, method: 'POST', data });
|
||||
}
|
||||
|
||||
export default { request, get, post };
|
||||
Reference in New Issue
Block a user