add fetchInterceptor.js
This commit is contained in:
parent
3706d4bd7a
commit
b761878e54
@ -7,6 +7,7 @@ import App from './App.vue'
|
||||
import router from './router'
|
||||
import Antd from 'ant-design-vue'
|
||||
import 'ant-design-vue/dist/reset.css'
|
||||
import './utils/fetchInterceptor.js'
|
||||
|
||||
const app = createApp(App)
|
||||
|
||||
|
||||
40
src/utils/fetchInterceptor.js
Normal file
40
src/utils/fetchInterceptor.js
Normal file
@ -0,0 +1,40 @@
|
||||
const originalFetch = window.fetch
|
||||
|
||||
window.fetch = async (input, init = {}) => {
|
||||
console.log('拦截请求:', input, init)
|
||||
|
||||
// 确保 init 对象初始化
|
||||
init = init || {}
|
||||
init.headers = {
|
||||
...init.headers, // 合并已有的 headers
|
||||
Authorization: 'Bearer YOUR_TOKEN' // 添加默认授权头
|
||||
}
|
||||
|
||||
if (init && !init.headers) {
|
||||
init.headers = {}
|
||||
}
|
||||
|
||||
init.headers = {
|
||||
...init.headers,
|
||||
'x-token': '123456'
|
||||
}
|
||||
|
||||
const response = await originalFetch(input, init)
|
||||
|
||||
const clonedResponse = response.clone()
|
||||
|
||||
const data = await clonedResponse.json()
|
||||
|
||||
console.log('拦截响应:', data)
|
||||
|
||||
const modifiedData = {
|
||||
...data,
|
||||
intercepted: 'true'
|
||||
}
|
||||
|
||||
return new Response(JSON.stringify(modifiedData), {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
headers: response.headers
|
||||
})
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user