forked from mt-fe-group/mt-yd-ui
105 lines
2.8 KiB
JavaScript
105 lines
2.8 KiB
JavaScript
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'
|
||
import merge from 'lodash/merge'
|
||
|
||
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['token'] = Cookies.get('token') || ''
|
||
// 默认参数
|
||
var defaults = {}
|
||
// 防止缓存,GET请求默认带_t参数
|
||
if (config.method === 'get') {
|
||
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 || response.data.code === 10001) {
|
||
clearLoginInfo()
|
||
router.replace({ name: 'login' })
|
||
return Promise.reject(response.data.msg)
|
||
}
|
||
return response
|
||
}, error => {
|
||
console.error(error)
|
||
return Promise.reject(error)
|
||
})
|
||
|
||
/**
|
||
* 请求地址处理
|
||
* @param {*} actionName action方法名称
|
||
*/
|
||
http.adornUrl = (actionName) => {
|
||
// 非生产环境 && 开启代理, 接口前缀统一使用[/proxyApi/]前缀做代理拦截!
|
||
return (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi/' : window.SITE_CONFIG.projURL) + actionName
|
||
}
|
||
|
||
/**
|
||
* get请求参数处理
|
||
* @param {*} params 参数对象
|
||
* @param {*} openDefultParams 是否开启默认参数?
|
||
*/
|
||
http.adornParams = (params = {}, openDefaultParams = true) => {
|
||
var defaults = {
|
||
't': new Date().getTime()
|
||
}
|
||
return openDefaultParams ? merge(defaults, params) : params
|
||
}
|
||
|
||
/**
|
||
* post请求数据处理
|
||
* @param {*} data 数据对象
|
||
* @param {*} openDefultdata 是否开启默认数据?
|
||
* @param {*} contentType 数据格式
|
||
* json: 'application/json; charset=utf-8'
|
||
* form: 'application/x-www-form-urlencoded; charset=utf-8'
|
||
*/
|
||
http.adornData = (data = {}, openDefaultdata = true, contentType = 'json') => {
|
||
var defaults = {
|
||
't': new Date().getTime()
|
||
}
|
||
data = openDefaultdata ? merge(defaults, data) : data
|
||
return contentType === 'raw' ? data : contentType === 'json' ? JSON.stringify(data) : qs.stringify(data)
|
||
// return contentType === 'json' ? JSON.stringify(data, (_, v) => typeof v === 'bigint' ? v.toString() : v) : qs.stringify(data)
|
||
}
|
||
|
||
|
||
export default http
|