vuex模块化及axios代理重新整理

This commit is contained in:
aixianling
2022-06-10 16:14:13 +08:00
parent 7cbe10ff64
commit 1218536e64
9 changed files with 174 additions and 158 deletions

27
src/common/http.js Normal file
View File

@@ -0,0 +1,27 @@
import axios from 'axios'
const instance = axios.create({
timeout: 600000,
withCredentials: true,
})
const getToken = () => {
let vuex = uni.getStorageSync("vuex")
return !!vuex ? JSON.parse(vuex).token : null
}
const source = axios.CancelToken.source();
instance.interceptors.request.use(config => {
if (config.withoutToken) {
return config
} else if (getToken()) {
config.headers["Authorization"] = getToken()
} else {
config.cancelToken = source.token
source.cancel("用户未验证,取消请求:" + config.url)
}
return config
}, err => {
console.error(err)
return Promise.reject(err)
})
export default instance