Files
ai_game/client/js/utils/http.js

53 lines
1.0 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.

/**
* 网络请求工具
*/
// 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 };