2022-06-10 16:14:13 +08:00
|
|
|
import axios from 'axios'
|
|
|
|
|
|
|
|
|
|
const instance = axios.create({
|
|
|
|
|
timeout: 600000,
|
|
|
|
|
withCredentials: true,
|
|
|
|
|
})
|
|
|
|
|
const getToken = () => {
|
|
|
|
|
let vuex = uni.getStorageSync("vuex")
|
2022-07-19 18:06:22 +08:00
|
|
|
return !!vuex ? JSON.parse(vuex).user.token : null
|
2022-06-10 16:14:13 +08:00
|
|
|
}
|
|
|
|
|
const source = axios.CancelToken.source();
|
2023-01-03 15:06:38 +08:00
|
|
|
let throttleMap = {}
|
2022-06-10 16:14:13 +08:00
|
|
|
instance.interceptors.request.use(config => {
|
2023-01-03 15:06:38 +08:00
|
|
|
if (config.throttle) {// 节流处理
|
|
|
|
|
let timer = throttleMap[config.url]
|
|
|
|
|
if (!!timer) {
|
|
|
|
|
config.cancelToken = source.token
|
|
|
|
|
source.cancel("节流控制,取消请求:" + config.url)
|
|
|
|
|
}
|
|
|
|
|
throttleMap[config.url] = setTimeout(() => {
|
|
|
|
|
throttleMap[config.url] = null
|
|
|
|
|
}, config.throttle)
|
|
|
|
|
}
|
2022-06-10 16:14:13 +08:00
|
|
|
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
|