mirror of
https://gitee.com/novel_dev_team/novel-front-web
synced 2026-07-20 17:54:10 +08:00
fix: 通过请求拦截来添加 Authorization Header
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import axios from 'axios'
|
||||
import router from '@/router'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import {getToken,removeToken, setToken} from '@/utils/auth'
|
||||
import {getToken,removeToken,removeNickName, setToken} from '@/utils/auth'
|
||||
|
||||
|
||||
|
||||
@@ -9,9 +9,16 @@ axios.defaults.baseURL = process.env.VUE_APP_BASE_API_URL
|
||||
axios.defaults.timeout = 10000
|
||||
axios.defaults.withCredentials = true
|
||||
axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'
|
||||
axios.defaults.headers['Authorization'] = getToken() || ''
|
||||
axios.defaults.headers.post['Content-Type'] = 'application/json'
|
||||
|
||||
axios.interceptors.request.use(config => {
|
||||
config.headers['Authorization'] = getToken()
|
||||
return config
|
||||
}, error => {
|
||||
console.log(error)
|
||||
Promise.reject(error)
|
||||
})
|
||||
|
||||
axios.interceptors.response.use(res => {
|
||||
if (typeof res.data !== 'object') {
|
||||
ElMessage.error('服务端异常!')
|
||||
@@ -25,6 +32,7 @@ axios.interceptors.response.use(res => {
|
||||
if (res.data.code == 'A0230') {
|
||||
// 移除 token
|
||||
removeToken();
|
||||
removeNickName();
|
||||
router.push({ path: '/login' })
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user