http.js
import axios from 'axios'
// 获取请求 header
const getHeader = (data = {}) => {
return Object.assign(
{
Accept: 'application/json',
'Content-Type': 'application/json;charset=utf-8'
},
data
)
}
// axios 配置
const axiosConfig = {
baseURL: '',
headers: getHeader(),
timeout: 60 * 1000,
// 是否开启cors
withCredentials: true
}
const instance = axios.create(axiosConfig)
// 添加请求拦截器
instance.interceptors.request.use(function (request) {
retrun request
}, function (err) {
return Promise.reject(err)
})
// 添加相应拦截器
instance.interceptors.response.use(function (res) {
return res
}, function (err) {
return Promise.reject(err)
})
const request = (type = 'POST') => {
return async (url, options) => {
const config = Object.assgin({}, instance.defaults, options)
config.url = url
config.method = type
try {
const res = await instance.request(config)
if (res.status === 200 && res.data.ResultCode === 1) {
return res.data
} else {
reject(res)
}
}
}
}
const http = {
post: request('POST')
}
export default http