- 优化 bug accessToken 存在到 headers 为空情况

pull/1/head
sin 2019-03-05 21:19:31 +08:00
parent 77290689d1
commit d0ca292bae
1 changed files with 20 additions and 1 deletions

View File

@ -39,6 +39,20 @@ function checkStatus(response) {
throw error; 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) => { const cachedSave = (response, hashcode) => {
/** /**
* Clone a response data and store it in sessionStorage * Clone a response data and store it in sessionStorage
@ -109,7 +123,11 @@ export default function request(url, option) {
// 将登陆的 accessToken 放到 header // 将登陆的 accessToken 放到 header
const loginToken = getLoginToken(); const loginToken = getLoginToken();
if (loginToken && loginToken.accessToken) { 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; const expirys = options.expirys && 60;
@ -138,6 +156,7 @@ export default function request(url, option) {
} }
return response.json(); return response.json();
}) })
.then(checkCode)
.catch(e => { .catch(e => {
const status = e.name; const status = e.name;
if (status === 401) { if (status === 401) {