Commit 411171c8 authored by guang.wu's avatar guang.wu

feat: 兼容返回值类型blob

parent 3b4b4681
...@@ -31,7 +31,7 @@ const onAccessTokenFetched = () => { ...@@ -31,7 +31,7 @@ const onAccessTokenFetched = () => {
subscriber = []; subscriber = [];
}; };
const refreshtoken = async () => { const refreshToken = async () => {
return instance.get(`${instance._extend.refresTokenHost}/oauth/refreshtoken?refreshtoken=${localStorage.get('refreshToken')}`); return instance.get(`${instance._extend.refresTokenHost}/oauth/refreshtoken?refreshtoken=${localStorage.get('refreshToken')}`);
}; };
...@@ -43,7 +43,7 @@ const refreshRequest = async options => { ...@@ -43,7 +43,7 @@ const refreshRequest = async options => {
if (isRefreshing) { if (isRefreshing) {
isRefreshing = false; isRefreshing = false;
await wait(500); await wait(500);
const data = await refreshtoken(); const data = await refreshToken();
localStorage.set('token', data.accessToken); localStorage.set('token', data.accessToken);
onAccessTokenFetched(); onAccessTokenFetched();
isRefreshing = true; isRefreshing = true;
...@@ -68,7 +68,7 @@ function afterRequest(model, isLoading = true) { ...@@ -68,7 +68,7 @@ function afterRequest(model, isLoading = true) {
if (!isLoading) return; if (!isLoading) return;
if (!model) model = 'common'; if (!model) model = 'common';
clearTimeout(loadingTimeOut); clearTimeout(loadingTimeOut);
var index = httpQueue.indexOf(model); const index = httpQueue.indexOf(model);
if (index > -1) { if (index > -1) {
httpQueue.splice(index, 1); httpQueue.splice(index, 1);
} }
...@@ -81,8 +81,8 @@ function afterRequest(model, isLoading = true) { ...@@ -81,8 +81,8 @@ function afterRequest(model, isLoading = true) {
} }
function clearRequest() { function clearRequest() {
let modelArr = Array.from(new Set(httpQueue)); let modelArr = Array.from(new Set(httpQueue));
for (let i = 0; i < modelArr.length; i++) { for (const model of modelArr) {
Bus.$emit(`${modelArr[i]}HideLoading`); Bus.$emit(`${model}HideLoading`);
} }
httpQueue = []; httpQueue = [];
} }
...@@ -119,18 +119,12 @@ instance.interceptors.request.use( ...@@ -119,18 +119,12 @@ instance.interceptors.request.use(
} }
} }
// todo: 某些接口不支持operator参数的话,需要排除 //普通的get请求
if (config.method === 'post' || config.method === 'get') { // TODO: 某些接口不支持operator参数的话,需要排除
if (typeof config.data === 'object') { if (config.method === 'get' && config.url.indexOf('offline_record') !== -1) {
} else if (typeof config.data === 'string' && config.method === 'post') {
} else if (config.method === 'get') {
// //普通的get请求
if (config.url.indexOf('offline_record') !== -1) {
config.data = true; config.data = true;
config.headers.common['content-type'] = 'application/x-www-form-urlencoded'; config.headers.common['content-type'] = 'application/x-www-form-urlencoded';
} }
}
}
return config; return config;
}, },
error => { error => {
...@@ -144,7 +138,7 @@ instance.interceptors.response.use( ...@@ -144,7 +138,7 @@ instance.interceptors.response.use(
if (response.headers['content-type'] === 'application/octet-stream' || (response.headers['content-type'] === 'application/vnd.ms-excel;charset=UTF-8' && response.data.byteLength !== 'undefied')) { if (response.headers['content-type'] === 'application/octet-stream' || (response.headers['content-type'] === 'application/vnd.ms-excel;charset=UTF-8' && response.data.byteLength !== 'undefied')) {
return response.data; return response.data;
} }
if (response.config.responseType === 'arraybuffer') { if (['blob', 'arraybuffer'].includes(response.config.responseType)) {
return response.data; return response.data;
} }
...@@ -152,23 +146,24 @@ instance.interceptors.response.use( ...@@ -152,23 +146,24 @@ instance.interceptors.response.use(
// 200 - 300 之间的都是正常响应 // 200 - 300 之间的都是正常响应
if (response.status > 200 || response.status < 300) { if (response.status > 200 || response.status < 300) {
const codeArr = [0, '0', 2000]; const codeArr = [0, '0', 2000];
if (response.data && ((response.data.code === '0000' && response.data.businessCode === '0000') || codeArr.includes(response.data.code))) { const code = response.data && response.data.code;
if (response.data && ((code === '0000' && response.data.businessCode === '0000') || codeArr.includes(code))) {
if (instance._extend.dataParseMode === 'NOT_PARSE') { if (instance._extend.dataParseMode === 'NOT_PARSE') {
return response.data; return response.data;
} }
if (instance._extend.dataParseMode === 'PARSE_BODY' && response.data.code === 0) { if (instance._extend.dataParseMode === 'PARSE_BODY' && code === 0) {
return response.data.body; return response.data.body;
} }
return response.data.data || response.data; return response.data.data || response.data;
} }
if (response.data.code === 4033) { if (+code === 4033) {
// token过期; // token过期;
return refreshRequest(response.config); return refreshRequest(response.config);
} }
if (response.data.code === 4010 || response.data.code === 4011) { if (+code === 4010 || +code === 4011) {
var href = window.location.href; let href = window.location.href;
href = href.replace(/\?token=[a-z0-9\-A-Z]+/g, ''); href = href.replace(/\?token=[a-z0-9\-A-Z]+/g, '');
window.location.href = instance._extend.loginUrl + '?url=' + window.btoa(href); window.location.href = instance._extend.loginUrl + '?url=' + window.btoa(href);
return; return;
...@@ -195,7 +190,7 @@ instance.interceptors.response.use( ...@@ -195,7 +190,7 @@ instance.interceptors.response.use(
switch (error.response.status) { switch (error.response.status) {
case 401: case 401:
// todo: 系统中有3处处理401的地方 // todo: 系统中有3处处理401的地方
var href = window.location.href; let href = window.location.href;
href = href.replace(/\?token=[a-z0-9\-A-Z]+/g, ''); href = href.replace(/\?token=[a-z0-9\-A-Z]+/g, '');
window.location.href = instance._extend.loginUrl + '?url=' + window.btoa(href); window.location.href = instance._extend.loginUrl + '?url=' + window.btoa(href);
return; return;
...@@ -299,7 +294,7 @@ const http = { ...@@ -299,7 +294,7 @@ const http = {
}); });
}, },
cancel() { cancel() {
cancelArr.map(cancel => cancel()); cancelArr.forEach(cancel => cancel());
}, },
}; };
......
{ {
"name": "@qg/sys-sdk", "name": "@qg/sys-sdk",
"version": "1.0.18", "version": "1.0.19",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment