vuex模块化及axios代理重新整理
This commit is contained in:
27
src/common/http.js
Normal file
27
src/common/http.js
Normal 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
|
||||
Reference in New Issue
Block a user