add
This commit is contained in:
28
frontend/src/api/http.js
Normal file
28
frontend/src/api/http.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import axios from 'axios'
|
||||
import { useAuthStore } from '../store/auth'
|
||||
|
||||
const http = axios.create({
|
||||
baseURL: 'http://localhost:8080',
|
||||
timeout: 15000
|
||||
})
|
||||
|
||||
http.interceptors.request.use((config) => {
|
||||
const store = useAuthStore()
|
||||
if (store.token) {
|
||||
config.headers.Authorization = `Bearer ${store.token}`
|
||||
}
|
||||
return config
|
||||
})
|
||||
|
||||
http.interceptors.response.use(
|
||||
(response) => {
|
||||
const res = response.data
|
||||
if (res.code !== 0) {
|
||||
return Promise.reject(new Error(res.message || '请求失败'))
|
||||
}
|
||||
return res.data
|
||||
},
|
||||
(error) => Promise.reject(error)
|
||||
)
|
||||
|
||||
export default http
|
||||
Reference in New Issue
Block a user