Commit 4e3ba6a0 authored by 郝聪敏's avatar 郝聪敏

fix: 优化cancel请求;添加loading控制header

parent 78245314
...@@ -51,7 +51,8 @@ const refreshRequest = async options => { ...@@ -51,7 +51,8 @@ const refreshRequest = async options => {
return promise; return promise;
}; };
function beforeRequest(model) { function beforeRequest(model, isLoading = true) {
if (!isLoading) return;
if (!model) model = 'common'; if (!model) model = 'common';
httpQueue.push(model); httpQueue.push(model);
loadingTimeOut = setTimeout(() => { loadingTimeOut = setTimeout(() => {
...@@ -63,7 +64,8 @@ function beforeRequest(model) { ...@@ -63,7 +64,8 @@ function beforeRequest(model) {
Bus.$emit(`${model}HideLoading`); Bus.$emit(`${model}HideLoading`);
}, 30 * 60 * 1000); }, 30 * 60 * 1000);
} }
function afterRequest(model) { function afterRequest(model, isLoading = true) {
if (!isLoading) return;
if (!model) model = 'common'; if (!model) model = 'common';
clearTimeout(loadingTimeOut); clearTimeout(loadingTimeOut);
var index = httpQueue.indexOf(model); var index = httpQueue.indexOf(model);
...@@ -87,7 +89,7 @@ function clearRequest() { ...@@ -87,7 +89,7 @@ function clearRequest() {
// http request 拦截器 // http request 拦截器
instance.interceptors.request.use( instance.interceptors.request.use(
config => { config => {
beforeRequest(config.headers._loading); beforeRequest(config.headers._loading, config.headers._isLoading);
const token = localStorage.get('token'); const token = localStorage.get('token');
const user = localStorage.get('user'); const user = localStorage.get('user');
if (user) { if (user) {
...@@ -133,7 +135,7 @@ instance.interceptors.request.use( ...@@ -133,7 +135,7 @@ instance.interceptors.request.use(
instance.interceptors.response.use( instance.interceptors.response.use(
response => { response => {
afterRequest(response.config.headers._loading); afterRequest(response.config.headers._loading, response.config.headers._isLoading);
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;
} }
...@@ -179,11 +181,9 @@ instance.interceptors.response.use( ...@@ -179,11 +181,9 @@ instance.interceptors.response.use(
return Promise.reject(response.data); return Promise.reject(response.data);
}, },
error => { error => {
instance._extend.notice.error({
title: '后端服务异常',
});
if (axios.isCancel(error) || error.message.indexOf('Network') > -1) { if (axios.isCancel(error) || error.message.indexOf('Network') > -1) {
clearRequest(); clearRequest();
return Promise.reject(error);
} else { } else {
afterRequest(error.response && error.response.config.headers._loading); afterRequest(error.response && error.response.config.headers._loading);
if (error.response) { if (error.response) {
...@@ -197,6 +197,10 @@ instance.interceptors.response.use( ...@@ -197,6 +197,10 @@ instance.interceptors.response.use(
} }
} }
} }
instance._extend.notice.error({
title: error.message || '后端服务异常',
});
return Promise.reject(error); return Promise.reject(error);
} }
); );
......
{ {
"name": "@qg/sys-sdk", "name": "@qg/sys-sdk",
"version": "1.0.11", "version": "1.0.14",
"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