let baseURL = 'https://xxxxxx' // 服务器地址
if (window.location.hostname == 'localhost') {
baseURL = ''
}
// 创建一个 axios 实例
const service = axios.create({
baseURL: baseURL, // 所有的请求地址前缀部分
timeout: 60000, // 请求超时时间毫秒
withCredentials: true // 异步请求携带cookie
})
// 添加请求拦截器
service.interceptors.request.use(
function (config) {
const token = sessionStorage.getItem('token') // 设置token值
if (token) {
config.headers['token'] = token
}
return config
},
function (error) {
// 对请求错误做些什么
console.log(error)
return Promise.reject(error)
}
)
// 添加响应拦截器
service.interceptors.response.use(
function (response) {
console.log(response)
// 2xx 范围内的状态码都会触发该函数。
// 对响应数据做点什么
// dataAxios 是 axios 返回数据中的 data
const dataAxios = response.data
// 这个状态码是和后端约定的
const code = dataAxios.reset
return dataAxios
},
function (error) {
// 超出 2xx 范围的状态码都会触发该函数。
// 对响应错误做点什么
console.log(error)
return Promise.reject(error)
}
)
export default service