1.import _ from 'lodash'
2.function clearEmptyParam(config) {
['data', 'params'].forEach(item => {
if (config[item]) {
const keys = Object.keys(config[item])
if (keys.length) {
keys.forEach(key => {
const rawType = toRawType(config[item])
if (['', undefined, null].includes(config[item][key]) &&
['Object'].includes(rawType)) {
// 移除属性之前,进行深拷贝断开引用,避免影响页面
config[item] = _.cloneDeep(config[item])
delete config[item][key]
}
})
}
}
})
}
/**
* @description 获取原始类型
* @param {*} value
* @returns {String} 类型字符串,如'String', 'Object', 'Null', 'Boolean', 'Number', 'Array'
*/
export function toRawType(value) {
return Object.prototype.toString.call(value).slice(8, -1)
}
3.service.interceptors.request.use(
config => {
if (store.getters.token) {
config.headers['X-Token'] = getToken()
}
clearEmptyParam(config)
return config
},