From d0ca292baeda604f1711fb3846b771273d85a946 Mon Sep 17 00:00:00 2001 From: sin <2943460818@qq.com> Date: Tue, 5 Mar 2019 21:19:31 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96=20bug=20accessToken=20?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E5=88=B0=20headers=20=E4=B8=BA=E7=A9=BA?= =?UTF-8?q?=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin-web/src/utils/request.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/admin-web/src/utils/request.js b/admin-web/src/utils/request.js index fdeb460e8..f1b7d5b8f 100644 --- a/admin-web/src/utils/request.js +++ b/admin-web/src/utils/request.js @@ -39,6 +39,20 @@ function checkStatus(response) { throw error; } +function checkCode(result) { + if (result.code === undefined || result.code === 0) { + return result; + } + + notification.warning({ + message: `请求错误 ${result.code}`, + description: result.message, + }); + const error = new Error(result.message); + error.result = result; + throw error; +} + const cachedSave = (response, hashcode) => { /** * Clone a response data and store it in sessionStorage @@ -109,7 +123,11 @@ export default function request(url, option) { // 将登陆的 accessToken 放到 header const loginToken = getLoginToken(); if (loginToken && loginToken.accessToken) { - newOptions.headers.Authorization = loginToken.accessToken; + const headers = { + ...newOptions.headers, + Authorization: `Bearer ${loginToken.accessToken}`, + }; + newOptions.headers = headers; } const expirys = options.expirys && 60; @@ -138,6 +156,7 @@ export default function request(url, option) { } return response.json(); }) + .then(checkCode) .catch(e => { const status = e.name; if (status === 401) {