mirror of
https://github.com/fantastic-admin/basic.git
synced 2026-10-07 16:48:39 +08:00
优化请求拦截器
This commit is contained in:
+10
-26
@@ -1,5 +1,5 @@
|
||||
import axios from 'axios'
|
||||
// import Qs from 'qs'
|
||||
// import qs from 'qs'
|
||||
import router from '@/router/index'
|
||||
import store from '@/store/index'
|
||||
import { ElMessage } from 'element-plus'
|
||||
@@ -23,32 +23,16 @@ api.interceptors.request.use(
|
||||
request => {
|
||||
/**
|
||||
* 全局拦截请求发送前提交的参数
|
||||
* 以下代码为示例,在登录状态下,分别对 post 和 get 请求加上 token 参数
|
||||
* 以下代码为示例,在请求头里带上 token 信息
|
||||
*/
|
||||
if (request.method == 'post') {
|
||||
if (request.data instanceof FormData) {
|
||||
if (store.getters['user/isLogin']) {
|
||||
// 如果是 FormData 类型(上传图片)
|
||||
request.data.append('token', store.state.user.token)
|
||||
}
|
||||
} else {
|
||||
// 带上 token
|
||||
if (request.data == undefined) {
|
||||
request.data = {}
|
||||
}
|
||||
if (store.getters['user/isLogin']) {
|
||||
request.data.token = store.state.user.token
|
||||
}
|
||||
// request.data = Qs.stringify(request.data)
|
||||
}
|
||||
} else {
|
||||
// 带上 token
|
||||
if (request.params == undefined) {
|
||||
request.params = {}
|
||||
}
|
||||
if (store.getters['user/isLogin']) {
|
||||
request.params.token = store.state.user.token
|
||||
}
|
||||
if (store.getters['user/isLogin']) {
|
||||
request.headers['Token'] = store.state.user.token
|
||||
}
|
||||
// 是否将 POST 请求参数进行字符串化处理
|
||||
if (request.method === 'post') {
|
||||
// request.data = qs.stringify(request.data, {
|
||||
// arrayFormat: 'brackets'
|
||||
// })
|
||||
}
|
||||
return request
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user