axios 请求和响应拦截器

1. 创建实例

使用 axios.create() 使用自定义配置创建一个 axios 实例。

js 复制代码
const $http = axios.create({
	timeout: 1000,
	headers: {
		'Content-Type': 'application/json',
	}
})

2. 拦截器

在请求或响应被 then 或者 catch 处理前拦截他们,拦截分为请求拦截和响应拦截。

js 复制代码
// 添加请求拦截器
$http.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    return config;
  }, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  });

// 添加响应拦截器
$http.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    return response;
  }, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
  });

2.1 request 拦截器,全局添加市场信息

js 复制代码
$http.interceptors.request.use(config => {
  ......
  // 市场编码
  if (config.data && Object.prototype.toString.call(config.data) === '[object Object]' && typeof config.data !== 'string') {
    if (!config.data.removeMarketCode) {
      const market = store.state.common.market || {}
      config.data.marketId = market.key
      config.data.marketName = market.val
    } else {
      delete config.data.removeMarketCode
    }
  }

  return config
}, error => {
  // Do something with request error
  Promise.reject(error)
})
  • removeMarketCode 是否移除市场信息,默认不移除;

根据上述代码可以看到,市场信息都是默认添加在 config.data 对象中下的,如果我们需要调整传参对象的位置时,那需要更改上述逻辑,具体代码如下:

js 复制代码
if (!config.data.removeMarketCode) {
	// 指定 data 下某个路径
    const key = config.data?.target
    const market = store.state.common.market || {}
    const marketObj = { marketId: market.key, marketName: market.val }
    if (key) {
        config.data[key] = { ...config.data[key], ...marketObj }
        delete config.data.target
    } else {
        config.data = { ...config.data, ...marketObj }
    }
} else {
    delete config.data.removeMarketCode
}

使用如下:

js 复制代码
class BillManage {
 export (params) {
    return axios.post(URL.receivePayBill.export, { ...params, target: 'condition' })
  }
}
export default new BillManage()

// 实际传参
{ "columns": [...], "condition": { "isPay": false, "marketId": "000", "marketName": "白糖" } }

2.2 response 拦截器

通过响应拦截器我们用来处理响应异常的接口进行拦截提示,返回对象中会返回一个 succeed 字段来表示接口处理正常还是异常情况,当然也有可能直接返回文件流,因此这里使用的 'succeed' in res 的写法来表示 res 中是否存在 succeed 的 key 来代替 res.succeed。

js 复制代码
// response 拦截器
$http.interceptors.response.use(
  response => {
    ......
    const res = response.data
    if ('succeed' in res && res.succeed !== true) {
      notification.error({
        message: 'Error',
        description: res.errorMsg,
      })
    
      return Promise.reject(new Error(res.errorMsg || 'error'))
    } else {
      return response.data
    }
  },
  error => {
    return Promise.reject(error)
  }
)

导出文件里处理如下:

js 复制代码
class ReceivePayAccountManage {
  // 下载模板
  payOrderImportTmpl (params) {
    return axios.post(URL.receivePayOrder.payOrderImportTmpl, params, { responseType: 'blob' })
  }
}
export default new ReceivePayAccountManage()

handleDownload (methodName) {
  handleRepeatSubmit.call(this, null, 
  	() => config.methods[methodName].handler(), 
  	(data) => downloadPDF('应付单导入模板.xlsx', data)
  )
},

downloadPDF 方法传入文件名和文件流,下载文件信息处理,具体代码如下:

js 复制代码
export const downloadPDF = function (fileName, blob) {
  const downloadElement = document.createElement('a')
  const href = window.URL.createObjectURL(blob)
  downloadElement.href = href
  downloadElement.download = fileName
  document.body.appendChild(downloadElement)
  downloadElement.click()
  document.body.removeChild(downloadElement)
  window.URL.revokeObjectURL(href)
}
相关推荐
酒尘&5 小时前
JS数组不止Array!索引集合类全面解析
开发语言·前端·javascript·学习·js
学历真的很重要6 小时前
VsCode+Roo Code+Gemini 2.5 Pro+Gemini Balance AI辅助编程环境搭建(理论上通过多个Api Key负载均衡达到无限免费Gemini 2.5 Pro)
前端·人工智能·vscode·后端·语言模型·负载均衡·ai编程
用户47949283569157 小时前
"讲讲原型链" —— 面试官最爱问的 JavaScript 基础
前端·javascript·面试
用户47949283569157 小时前
2025 年 TC39 都在忙什么?Import Bytes、Iterator Chunking 来了
前端·javascript·面试
大怪v8 小时前
【Virtual World 04】我们的目标,无限宇宙!!
前端·javascript·代码规范
狂炫冰美式8 小时前
不谈技术,搞点文化 🧀 —— 从复活一句明代残诗破局产品迭代
前端·人工智能·后端
xw59 小时前
npm几个实用命令
前端·npm
!win !9 小时前
npm几个实用命令
前端·npm
代码狂想家9 小时前
使用openEuler从零构建用户管理系统Web应用平台
前端
dorisrv10 小时前
优雅的React表单状态管理
前端