Files
AIProd/plugins/axios.js
2025-10-10 10:32:37 +08:00

27 lines
619 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//设置请求和拦截响应器
export default function({
$axios,
app
}) {
// 请求拦截器
$axios.onRequest(config => {
// 如果用 token从 cookie 拿
const token = app.$cookies.get('token')
if (token) {
config.headers.Authorization = `Bearer ${token}` //鉴权方式这里使用的bearer
}
return config
}, error => Promise.reject(error))
// 响应拦截器
$axios.onResponse(response => {
if(response.code!=0){
return response
}
return response.data // 直接返回 data调用时不用再写 res.data
}, error => Promise.reject(error))
}