提交 86a69d99 作者: 翁进城

request增加配置参数

上级 d487663a
......@@ -5,10 +5,10 @@ import config from './config';
* @param url 请求url
* @param method 请求方法类型
* @param data 请求的参数
* @param options 额外参数
* @returns Promise<Response>
*/
export default function request(url: string, method: String = 'get', data?: any): Promise<Response<any>> {
let options = {};
export default function request(url: string, method: String = 'get', data?: any, options = {}): Promise<Response<any>> {
switch (method.toLowerCase()) {
case 'get':
......@@ -24,6 +24,7 @@ export default function request(url: string, method: String = 'get', data?: any)
case 'post':
options = {
...options,
method: 'POST',
headers: {
'Content-Type': 'application/json'
......@@ -48,6 +49,19 @@ export default function request(url: string, method: String = 'get', data?: any)
})
.then((data) => {
return data;
})
.catch(error => {
if (error.name === 'AbortError') {
console.log('请求已中断');
console.log(error);
} else {
console.error('请求出错', error);
}
return {
code: '-1',
message: '请求失败',
result: null
}
});
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论