import axios from 'axios'
import adapter from 'axios-miniprogram-adapter'

const instance = axios.create({
  timeout: 600000,
  withCredentials: true,
  adapter
})
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
