响应拦截器处理返回值code=0的情况为成功
This commit is contained in:
parent
e1f3a39fe4
commit
8ac8eecb5c
@ -74,7 +74,28 @@ service.interceptors.request.use(config => {
|
||||
// 响应拦截器
|
||||
service.interceptors.response.use(res => {
|
||||
// 未设置状态码则默认成功状态
|
||||
const code = res.data.code || 0;
|
||||
const code = (() => {
|
||||
const rawCode = res.data.code;
|
||||
|
||||
// 如果是 null/undefined,返回默认值 200
|
||||
if (rawCode === null || rawCode === undefined) {
|
||||
return 200;
|
||||
}
|
||||
|
||||
// 如果是字符串,尝试转换为数字
|
||||
if (typeof rawCode === 'string') {
|
||||
const numericCode = Number(rawCode);
|
||||
return isNaN(numericCode) ? 200 : numericCode;
|
||||
}
|
||||
|
||||
// 如果已经是数字,直接返回
|
||||
if (typeof rawCode === 'number') {
|
||||
return rawCode;
|
||||
}
|
||||
|
||||
// 其他情况,返回默认值
|
||||
return 200;
|
||||
})();
|
||||
// 获取错误信息
|
||||
const msg = errorCode[code] || res.data.msg || errorCode['default']
|
||||
// 二进制数据则直接返回
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user