Skip to content

Commit

Permalink
feat: 封装根据响应内容弹窗提示成功的请求方法
Browse files Browse the repository at this point in the history
  • Loading branch information
cadecode committed Apr 22, 2023
1 parent cc93c09 commit 27959f5
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/util/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function checkResError(response) {
// 401未登录,跳转到登录页
if (res.status === 401) {
errorMessage = res.error.message;
// await此结果将抛出错误
return MessageBox.confirm(`${errorMessage}, 是否重新登录`, '登录状态失效', {
type: 'warning',
confirmButtonText: '返回登录页',
Expand All @@ -81,7 +82,7 @@ function checkResError(response) {
throw new Error(errorMessage);
});
}
errorMessage = '错误: ' + JSON.stringify(res.error);
errorMessage = 'Error: ' + JSON.stringify(res.error);
// 此处res.error可兼容SpringBoot原生接口异常
Message.error({message: errorMessage, duration: 5 * 1000});
throw new Error(errorMessage);
Expand All @@ -97,29 +98,55 @@ function checkResToken(response) {
}
}

/**
* 发送请求
* customConfig.messageFn: res => res.data.flag,根据flag决定是否弹出提示
* @param config axios配置
* @param customConfig 自定义配置
*/
function request(config, customConfig) {
return service(config).then(res => {
if (customConfig && customConfig.messageFn) {
let flag;
try {
flag = customConfig.messageFn(res);
} catch (e) {
flag = false;
}
// 操作成功
if (flag) {
Message.success('操作成功');
} else {
Message.success('操作失败');
}
}
return res;
});
}

/**
* 查询字符串式的请求,a=1&b=2
* axios自动设置x-www-form-urlencoded
*/
function requestQuery(config) {
function requestQuery(config, customConfig) {
config.data = objectToQuery((config.data));
return service(config);
return request(config, customConfig);
}

/**
* FormData式请求,new FormData()
* axios自动设置x-www-form-urlencoded
*/
function requestFormData(config) {
function requestFormData(config, customConfig) {
config.data = Object.keys(config.data).reduce((p, n) => {
p.append(n, config.data[n]);
return p;
}, new FormData());
return service(config);
return request(config, customConfig);
}

export {
service as default,
request as default,
requestFormData,
requestQuery
};

0 comments on commit 27959f5

Please sign in to comment.