mirror of
https://github.com/iczer/vue-antd-admin.git
synced 2025-04-06 03:57:44 +08:00
128 lines
30 KiB
HTML
128 lines
30 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zn-CN">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<title>拦截器配置 | Vue Antd Admin</title>
|
|
<meta name="generator" content="VuePress 1.5.2">
|
|
<link rel="icon" href="/favicon.ico">
|
|
<meta name="description" content="Vue Antd Admin">
|
|
<link rel="preload" href="/assets/css/0.styles.6e02fda8.css" as="style"><link rel="preload" href="/assets/js/app.9d3d26f1.js" as="script"><link rel="preload" href="/assets/js/2.8c76fef9.js" as="script"><link rel="preload" href="/assets/js/21.94de0f4c.js" as="script"><link rel="prefetch" href="/assets/js/10.089b316f.js"><link rel="prefetch" href="/assets/js/11.45b1f56d.js"><link rel="prefetch" href="/assets/js/12.01a276f6.js"><link rel="prefetch" href="/assets/js/13.1a84d693.js"><link rel="prefetch" href="/assets/js/14.bc952f61.js"><link rel="prefetch" href="/assets/js/15.e2444773.js"><link rel="prefetch" href="/assets/js/16.f4922829.js"><link rel="prefetch" href="/assets/js/17.2a1eabce.js"><link rel="prefetch" href="/assets/js/18.96936b0c.js"><link rel="prefetch" href="/assets/js/19.b8c9b558.js"><link rel="prefetch" href="/assets/js/20.5e521f7e.js"><link rel="prefetch" href="/assets/js/22.fb46e5b6.js"><link rel="prefetch" href="/assets/js/23.030dc832.js"><link rel="prefetch" href="/assets/js/24.d3dad329.js"><link rel="prefetch" href="/assets/js/25.3ea85210.js"><link rel="prefetch" href="/assets/js/26.dbfb0d65.js"><link rel="prefetch" href="/assets/js/27.d2edf1e7.js"><link rel="prefetch" href="/assets/js/28.5bf8a8a9.js"><link rel="prefetch" href="/assets/js/29.baa62900.js"><link rel="prefetch" href="/assets/js/3.d23e2c53.js"><link rel="prefetch" href="/assets/js/30.472c91a2.js"><link rel="prefetch" href="/assets/js/31.a79ce13e.js"><link rel="prefetch" href="/assets/js/32.ca4080fb.js"><link rel="prefetch" href="/assets/js/33.09a90ca2.js"><link rel="prefetch" href="/assets/js/4.1817d874.js"><link rel="prefetch" href="/assets/js/5.b598fcf7.js"><link rel="prefetch" href="/assets/js/6.a929b4e3.js"><link rel="prefetch" href="/assets/js/7.1b0a900f.js"><link rel="prefetch" href="/assets/js/8.fe7f978a.js"><link rel="prefetch" href="/assets/js/9.b6ba06c2.js">
|
|
<link rel="stylesheet" href="/assets/css/0.styles.6e02fda8.css">
|
|
</head>
|
|
<body>
|
|
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="Vue Antd Admin" class="logo"> <span class="site-name can-hide">Vue Antd Admin</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">
|
|
指南
|
|
</a></div><div class="nav-item"><a href="/develop/layout.html" class="nav-link">
|
|
配置
|
|
</a></div><div class="nav-item"><a href="/advance/theme.html" class="nav-link">
|
|
主题
|
|
</a></div> <a href="https://github.com/iczer/vue-antd-admin" target="_blank" rel="noopener noreferrer" class="repo-link">
|
|
GitHub
|
|
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">
|
|
指南
|
|
</a></div><div class="nav-item"><a href="/develop/layout.html" class="nav-link">
|
|
配置
|
|
</a></div><div class="nav-item"><a href="/advance/theme.html" class="nav-link">
|
|
主题
|
|
</a></div> <a href="https://github.com/iczer/vue-antd-admin" target="_blank" rel="noopener noreferrer" class="repo-link">
|
|
GitHub
|
|
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>开始</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/start/use.html" class="sidebar-link">使用</a></li><li><a href="/start/faq.html" class="sidebar-link">常见问题</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>开发</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/develop/layout.html" class="sidebar-link">布局</a></li><li><a href="/develop/router.html" class="sidebar-link">路由和菜单</a></li><li><a href="/develop/page.html" class="sidebar-link">页面</a></li><li><a href="/develop/theme.html" class="sidebar-link">主题定制</a></li><li><a href="/develop/service.html" class="sidebar-link">服务端交互</a></li><li><a href="/develop/mock.html" class="sidebar-link">Mock</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>进阶</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/advance/i18n.html" class="sidebar-link">国际化</a></li><li><a href="/advance/async.html" class="sidebar-link">异步路由和菜单</a></li><li><a href="/advance/authority.html" class="sidebar-link">权限管理</a></li><li><a href="/advance/login.html" class="sidebar-link">登录认证</a></li><li><a href="/advance/guard.html" class="sidebar-link">路由守卫</a></li><li><a href="/advance/interceptors.html" aria-current="page" class="active sidebar-link">拦截器配置</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/advance/interceptors.html#请求拦截器" class="sidebar-link">请求拦截器</a></li><li class="sidebar-sub-header"><a href="/advance/interceptors.html#响应拦截器" class="sidebar-link">响应拦截器</a></li><li class="sidebar-sub-header"><a href="/advance/interceptors.html#导出拦截器" class="sidebar-link">导出拦截器</a></li></ul></li><li><a href="/advance/api.html" class="sidebar-link">全局API</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>其它</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/other/upgrade.html" class="sidebar-link">更新日志</a></li><li><a href="/other/community.html" class="sidebar-link">社区</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="拦截器配置"><a href="#拦截器配置" class="header-anchor">#</a> 拦截器配置</h1> <p>Vue Antd Admin 基于 aixos 封装了 http 通信功能,我们可以为 http 请求响应配置一些拦截器。拦截器统一配置在 /utils/axios-interceptors.js 文件中。</p> <h2 id="请求拦截器"><a href="#请求拦截器" class="header-anchor">#</a> 请求拦截器</h2> <p>你可以为每个请求拦截器配置 <code>onFulfilled</code> 或 <code>onRejected</code> 两个钩子函数。</p> <h3 id="onfulfilled"><a href="#onfulfilled" class="header-anchor">#</a> onFulfilled</h3> <p>我们会为 onFulfilled 钩子函数注入 config 和 options 两个参数:</p> <ul><li><code>config: AxiosRequestConfig</code>: axios 请求配置,详情参考 <a href="http://www.axios-js.com/zh-cn/docs/#%E8%AF%B7%E6%B1%82%E9%85%8D%E7%BD%AE" target="_blank" rel="noopener noreferrer">axios 请求配置<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li> <li><code>options: Object</code>: 应用配置,包含: {router, i18n, store, message},可根据需要扩展。</li></ul> <h3 id="onrejected"><a href="#onrejected" class="header-anchor">#</a> onRejected</h3> <p>我们会为 onFulfilled 钩子函数注入 error 和 options 两个参数:</p> <ul><li><code>error: Error</code>: axios 请求错误对象</li> <li><code>options: Object</code>: 应用配置,包含: {router, i18n, store, message},可根据需要扩展。</li></ul> <p>如下,为一个完整的请求拦截器配置:</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> tokenCheck <span class="token operator">=</span> <span class="token punctuation">{</span>
|
|
<span class="token comment">// 发送请求之前做些什么</span>
|
|
<span class="token function">onFulfilled</span><span class="token punctuation">(</span><span class="token parameter">config<span class="token punctuation">,</span> options</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">const</span> <span class="token punctuation">{</span>message<span class="token punctuation">}</span> <span class="token operator">=</span> options
|
|
<span class="token keyword">const</span> <span class="token punctuation">{</span>url<span class="token punctuation">,</span> xsrfCookieName<span class="token punctuation">}</span> <span class="token operator">=</span> config
|
|
<span class="token keyword">if</span> <span class="token punctuation">(</span>url<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">'login'</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token operator">-</span><span class="token number">1</span> <span class="token operator">&&</span> xsrfCookieName <span class="token operator">&&</span> <span class="token operator">!</span>Cookie<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>xsrfCookieName<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
message<span class="token punctuation">.</span><span class="token function">warning</span><span class="token punctuation">(</span><span class="token string">'认证 token 已过期,请重新登录'</span><span class="token punctuation">)</span>
|
|
<span class="token punctuation">}</span>
|
|
<span class="token keyword">return</span> config
|
|
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|
<span class="token comment">// 请求出错时做点什么</span>
|
|
<span class="token function">onRejected</span><span class="token punctuation">(</span><span class="token parameter">error<span class="token punctuation">,</span> options</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">const</span> <span class="token punctuation">{</span>message<span class="token punctuation">}</span> <span class="token operator">=</span> options
|
|
message<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span>error<span class="token punctuation">.</span>message<span class="token punctuation">)</span>
|
|
<span class="token keyword">return</span> Promise<span class="token punctuation">.</span><span class="token function">reject</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span>
|
|
<span class="token punctuation">}</span>
|
|
<span class="token punctuation">}</span>
|
|
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><h2 id="响应拦截器"><a href="#响应拦截器" class="header-anchor">#</a> 响应拦截器</h2> <p>响应拦截器也同样可以配置 <code>onFulfilled</code> 或 <code>onRejected</code> 两个钩子函数。</p> <h3 id="onfulfilled-2"><a href="#onfulfilled-2" class="header-anchor">#</a> onFulfilled</h3> <p>我们会为 onFulfilled 钩子函数注入 response 和 options 两个参数:</p> <ul><li><code>response: AxiosResponse</code>: axios 响应对象,详情参考 <a href="http://www.axios-js.com/zh-cn/docs/#%E5%93%8D%E5%BA%94%E7%BB%93%E6%9E%84" target="_blank" rel="noopener noreferrer">axios 响应对象<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li> <li><code>options: Object</code>: 应用配置,包含: {router, i18n, store, message},可根据需要扩展。</li></ul> <h3 id="onrejected-2"><a href="#onrejected-2" class="header-anchor">#</a> onRejected</h3> <p>我们会为 onRejected 钩子函数注入 error 和 options 两个参数:</p> <ul><li><code>error: Error</code>: axios 请求错误对象</li> <li><code>options: Object</code>: 应用配置,包含: {router, i18n, store, message},可根据需要扩展。</li></ul> <p>如下,为一个完整的响应拦截器配置:</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> resp401 <span class="token operator">=</span> <span class="token punctuation">{</span>
|
|
<span class="token comment">// 响应数据之前做点什么</span>
|
|
<span class="token function">onFulfilled</span><span class="token punctuation">(</span><span class="token parameter">response<span class="token punctuation">,</span> options</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">const</span> <span class="token punctuation">{</span>message<span class="token punctuation">}</span> <span class="token operator">=</span> options
|
|
<span class="token keyword">if</span> <span class="token punctuation">(</span>response<span class="token punctuation">.</span>status <span class="token operator">===</span> <span class="token number">401</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
message<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">'无此接口权限'</span><span class="token punctuation">)</span>
|
|
<span class="token punctuation">}</span>
|
|
<span class="token keyword">return</span> response
|
|
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|
<span class="token comment">// 响应出错时做点什么</span>
|
|
<span class="token function">onRejected</span><span class="token punctuation">(</span><span class="token parameter">error<span class="token punctuation">,</span> options</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">const</span> <span class="token punctuation">{</span>message<span class="token punctuation">}</span> <span class="token operator">=</span> options
|
|
<span class="token keyword">if</span> <span class="token punctuation">(</span>response<span class="token punctuation">.</span>status <span class="token operator">===</span> <span class="token number">401</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
message<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">'无此接口权限'</span><span class="token punctuation">)</span>
|
|
<span class="token punctuation">}</span>
|
|
<span class="token keyword">return</span> Promise<span class="token punctuation">.</span><span class="token function">reject</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span>
|
|
<span class="token punctuation">}</span>
|
|
<span class="token punctuation">}</span>
|
|
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h2 id="导出拦截器"><a href="#导出拦截器" class="header-anchor">#</a> 导出拦截器</h2> <p>定义好拦截器后,只需在 axios-interceptors.js 文件中导出即可。分为两类,<code>请求拦截器</code>和<code>响应拦截器</code>。如下:</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
|
|
request<span class="token operator">:</span> <span class="token punctuation">[</span>tokenCheck<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token comment">// 请求拦截</span>
|
|
response<span class="token operator">:</span> <span class="token punctuation">[</span>resp401<span class="token punctuation">]</span> <span class="token comment">// 响应拦截</span>
|
|
<span class="token punctuation">}</span>
|
|
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><details class="custom-block details"><summary>点击查看完整的拦截器配置示例</summary> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> Cookie <span class="token keyword">from</span> <span class="token string">'js-cookie'</span>
|
|
<span class="token comment">// 401拦截</span>
|
|
<span class="token keyword">const</span> resp401 <span class="token operator">=</span> <span class="token punctuation">{</span>
|
|
<span class="token function">onFulfilled</span><span class="token punctuation">(</span><span class="token parameter">response<span class="token punctuation">,</span> options</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">const</span> <span class="token punctuation">{</span>message<span class="token punctuation">}</span> <span class="token operator">=</span> options
|
|
<span class="token keyword">if</span> <span class="token punctuation">(</span>response<span class="token punctuation">.</span>status <span class="token operator">===</span> <span class="token number">401</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
message<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">'无此接口权限'</span><span class="token punctuation">)</span>
|
|
<span class="token punctuation">}</span>
|
|
<span class="token keyword">return</span> response
|
|
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|
<span class="token function">onRejected</span><span class="token punctuation">(</span><span class="token parameter">error<span class="token punctuation">,</span> options</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">const</span> <span class="token punctuation">{</span>message<span class="token punctuation">}</span> <span class="token operator">=</span> options
|
|
message<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span>error<span class="token punctuation">.</span>message<span class="token punctuation">)</span>
|
|
<span class="token keyword">return</span> Promise<span class="token punctuation">.</span><span class="token function">reject</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span>
|
|
<span class="token punctuation">}</span>
|
|
<span class="token punctuation">}</span>
|
|
|
|
<span class="token keyword">const</span> resp403 <span class="token operator">=</span> <span class="token punctuation">{</span>
|
|
<span class="token function">onFulfilled</span><span class="token punctuation">(</span><span class="token parameter">response<span class="token punctuation">,</span> options</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">const</span> <span class="token punctuation">{</span>message<span class="token punctuation">}</span> <span class="token operator">=</span> options
|
|
<span class="token keyword">if</span> <span class="token punctuation">(</span>response<span class="token punctuation">.</span>status <span class="token operator">===</span> <span class="token number">403</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
message<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">请求被拒绝</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span>
|
|
<span class="token punctuation">}</span>
|
|
<span class="token keyword">return</span> response
|
|
<span class="token punctuation">}</span>
|
|
<span class="token punctuation">}</span>
|
|
|
|
<span class="token keyword">const</span> reqCommon <span class="token operator">=</span> <span class="token punctuation">{</span>
|
|
<span class="token function">onFulfilled</span><span class="token punctuation">(</span><span class="token parameter">config<span class="token punctuation">,</span> options</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">const</span> <span class="token punctuation">{</span>message<span class="token punctuation">}</span> <span class="token operator">=</span> options
|
|
<span class="token keyword">const</span> <span class="token punctuation">{</span>url<span class="token punctuation">,</span> xsrfCookieName<span class="token punctuation">}</span> <span class="token operator">=</span> config
|
|
<span class="token keyword">if</span> <span class="token punctuation">(</span>url<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">'login'</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token operator">-</span><span class="token number">1</span> <span class="token operator">&&</span> xsrfCookieName <span class="token operator">&&</span> <span class="token operator">!</span>Cookie<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>xsrfCookieName<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
message<span class="token punctuation">.</span><span class="token function">warning</span><span class="token punctuation">(</span><span class="token string">'认证 token 已过期,请重新登录'</span><span class="token punctuation">)</span>
|
|
<span class="token punctuation">}</span>
|
|
<span class="token keyword">return</span> config
|
|
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|
<span class="token function">onRejected</span><span class="token punctuation">(</span><span class="token parameter">error<span class="token punctuation">,</span> options</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|
<span class="token keyword">const</span> <span class="token punctuation">{</span>message<span class="token punctuation">}</span> <span class="token operator">=</span> options
|
|
message<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span>error<span class="token punctuation">.</span>message<span class="token punctuation">)</span>
|
|
<span class="token keyword">return</span> Promise<span class="token punctuation">.</span><span class="token function">reject</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span>
|
|
<span class="token punctuation">}</span>
|
|
<span class="token punctuation">}</span>
|
|
|
|
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
|
|
request<span class="token operator">:</span> <span class="token punctuation">[</span>reqCommon<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token comment">// 请求拦截</span>
|
|
response<span class="token operator">:</span> <span class="token punctuation">[</span>resp401<span class="token punctuation">,</span> resp403<span class="token punctuation">]</span> <span class="token comment">// 响应拦截</span>
|
|
<span class="token punctuation">}</span>
|
|
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br></div></div></details></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/iczer/vue-antd-admin/edit/master/docs/advance/interceptors.md" target="_blank" rel="noopener noreferrer">在 Github 上帮助我们编辑此页</a> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></div> <div class="last-updated"><span class="prefix">Last Updated:</span> <span class="time">8/6/2021, 2:31:23 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
|
|
←
|
|
<a href="/advance/guard.html" class="prev">
|
|
路由守卫
|
|
</a></span> <span class="next"><a href="/advance/api.html">
|
|
全局API
|
|
</a>
|
|
→
|
|
</span></p></div> </main></div><div class="global-ui"><!----></div></div>
|
|
<script src="/assets/js/app.9d3d26f1.js" defer></script><script src="/assets/js/2.8c76fef9.js" defer></script><script src="/assets/js/21.94de0f4c.js" defer></script>
|
|
</body>
|
|
</html>
|