diff --git a/utils/request/index.js b/utils/request/index.js index 3afa5f9..62b51f8 100644 --- a/utils/request/index.js +++ b/utils/request/index.js @@ -24,17 +24,12 @@ export const Request = () => { // 直接使用 state.token(Pinia getter 可以直接访问) const token = userStore.token - - if (token) { // 使用标准的 Authorization header,添加 Bearer 前缀 config.header.Authorization = `Bearer ${token}` - } else { console.warn('请求拦截器:token 为空,无法添加认证头') } - } else { - console.log('请求拦截器:未启用 auth,跳过 token 添加') } return config }, config => { // 可使用async await 做异步操作 @@ -61,7 +56,7 @@ export const Request = () => { if (data.code !== 200) { // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示 if (custom?.toast !== false) { - uni.$uv.toast(data.message || '请求失败') + uni.$uv.toast(data.message || data.msg || '请求失败') } // 如果需要catch返回,则进行reject @@ -72,7 +67,16 @@ export const Request = () => { return new Promise(() => { }) } } - return data.data === undefined ? {} : data.data + + // 如果响应数据有 data 字段,返回 data 字段的值 + // 如果没有 data 字段,返回去除 code 和 msg 后的数据对象 + if (data.data !== undefined) { + return data.data + } else { + // 创建一个新对象,排除 code 和 msg 字段 + const { code, msg, message, ...rest } = data + return Object.keys(rest).length > 0 ? rest : data + } }, (response) => { // 对响应错误做点什么 (statusCode !== 200) // 网络错误或服务器错误处理