联调后端成功
This commit is contained in:
parent
dd7801b273
commit
d2a8851199
|
|
@ -24,17 +24,12 @@ export const Request = () => {
|
||||||
// 直接使用 state.token(Pinia getter 可以直接访问)
|
// 直接使用 state.token(Pinia getter 可以直接访问)
|
||||||
const token = userStore.token
|
const token = userStore.token
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
// 使用标准的 Authorization header,添加 Bearer 前缀
|
// 使用标准的 Authorization header,添加 Bearer 前缀
|
||||||
config.header.Authorization = `Bearer ${token}`
|
config.header.Authorization = `Bearer ${token}`
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
console.warn('请求拦截器:token 为空,无法添加认证头')
|
console.warn('请求拦截器:token 为空,无法添加认证头')
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
console.log('请求拦截器:未启用 auth,跳过 token 添加')
|
|
||||||
}
|
}
|
||||||
return config
|
return config
|
||||||
}, config => { // 可使用async await 做异步操作
|
}, config => { // 可使用async await 做异步操作
|
||||||
|
|
@ -61,7 +56,7 @@ export const Request = () => {
|
||||||
if (data.code !== 200) {
|
if (data.code !== 200) {
|
||||||
// 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
|
// 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
|
||||||
if (custom?.toast !== false) {
|
if (custom?.toast !== false) {
|
||||||
uni.$uv.toast(data.message || '请求失败')
|
uni.$uv.toast(data.message || data.msg || '请求失败')
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果需要catch返回,则进行reject
|
// 如果需要catch返回,则进行reject
|
||||||
|
|
@ -72,7 +67,16 @@ export const Request = () => {
|
||||||
return new Promise(() => { })
|
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) => {
|
}, (response) => {
|
||||||
// 对响应错误做点什么 (statusCode !== 200)
|
// 对响应错误做点什么 (statusCode !== 200)
|
||||||
// 网络错误或服务器错误处理
|
// 网络错误或服务器错误处理
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user