import axios from 'axios'
import Cookies from 'js-cookie'
import router from '@/router'
import qs from 'qs'
import {
clearLoginInfo
} from '@/utils'
import isPlainObject from 'lodash/isPlainObject'
const http = axios.create({
baseURL: window.SITE_CONFIG['apiURL'],
timeout: 1000 * 180,
withCredentials: true
})
http.interceptors.request.use(config => {
config.headers['Accept-Language'] = Cookies.get('language') || 'zh-CN'
config.headers['SELLER-TOKEN'] = Cookies.get('SELLER-TOKEN') || ''
var defaults = {}
if (config.method === 'get') {
if (config.params) {
for (var key in config.params) {
if (config.params[key] === '') {
delete config.params[key]
}
}
}
config.params = {
...config.params,
...{
'_t': new Date().getTime()
}
}
}
if (isPlainObject(config.params)) {
config.params = {
...defaults,
...config.params
}
}
if (isPlainObject(config.data)) {
config.data = {
...defaults,
...config.data
}
if (/^application\/x-www-form-urlencoded/.test(config.headers['content-type'])) {
config.data = qs.stringify(config.data)
}
}
return config
}, error => {
return Promise.reject(error)
})
http.interceptors.response.use(response => {
if (response.data.code === 401) {
clearLoginInfo()
router.replace({
name: 'login'
})
return Promise.reject(response.data.msg)
}
return response
}, error => {
console.error(error)
return Promise.reject(error)
})
export default http