联调后端成功

This commit is contained in:
WindowBird 2025-11-06 09:14:57 +08:00
parent dd7801b273
commit d2a8851199

View File

@ -24,17 +24,12 @@ export const Request = () => {
// 直接使用 state.tokenPinia 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
// 网络错误或服务器错误处理