fix: loading error

This commit is contained in:
LoadChange 2020-02-05 12:26:31 +08:00
parent 8ba9313493
commit df9b939071
10 changed files with 43 additions and 72 deletions

View File

@ -13,6 +13,7 @@
import projectConfig from './config'; import projectConfig from './config';
import $ from 'jquery'; import $ from 'jquery';
import { Message } from '@alifd/next';
const global = window; const global = window;
@ -445,7 +446,7 @@ const request = (function(_global) {
} catch (e) {} } catch (e) {}
// 设置自动loading效果 // 设置自动loading效果
if (serviceObj.autoLoading) { if (serviceObj.autoLoading) {
nacosUtils.openLoading(); // nacosUtils.openLoading();
const prevComplete = config.complete; const prevComplete = config.complete;
config.complete = function() { config.complete = function() {
nacosUtils.closeLoading(); nacosUtils.closeLoading();
@ -505,7 +506,13 @@ const request = (function(_global) {
error => { error => {
// 处理403 forbidden // 处理403 forbidden
const { status, responseJSON = {} } = error || {}; const { status, responseJSON = {} } = error || {};
if ([401, 403].includes(status) && responseJSON.message === 'token invalid!') { if (responseJSON.message) {
Message.error(responseJSON.message);
}
if (
[401, 403].includes(status) &&
['unknown user!', 'token invalid'].includes(responseJSON.message)
) {
// 跳转至login页 // 跳转至login页
// TODO: react-router 重写改造成本比较高这里先hack // TODO: react-router 重写改造成本比较高这里先hack
const url = window.location.href; const url = window.location.href;
@ -517,6 +524,7 @@ const request = (function(_global) {
const base_url = url.split('#')[0]; const base_url = url.split('#')[0];
window.location = `${base_url}#/login`; window.location = `${base_url}#/login`;
} }
return error;
} }
); );
} }
@ -536,9 +544,9 @@ export {
nacosEvent, nacosEvent,
nacosUtils, nacosUtils,
aliwareGetCookieByKeyName, aliwareGetCookieByKeyName,
removeParams,
getParams, getParams,
setParam, setParam,
setParams, setParams,
removeParams,
request, request,
}; };

View File

@ -286,9 +286,6 @@ class ConfigurationManagement extends React.Component {
}&config_tags=${this.state.config_tags || ''}&pageNo=${pageNo}&pageSize=${ }&config_tags=${this.state.config_tags || ''}&pageNo=${pageNo}&pageSize=${
this.state.pageSize this.state.pageSize
}`, }`,
beforeSend() {
self.openLoading();
},
success(data) { success(data) {
if (data != null) { if (data != null) {
self.setState({ self.setState({
@ -314,9 +311,6 @@ class ConfigurationManagement extends React.Component {
currentPage: 0, currentPage: 0,
}); });
}, },
complete() {
self.closeLoading();
},
}); });
} }

View File

@ -335,15 +335,15 @@ class NewConfig extends React.Component {
} }
self.successDialog.current.getInstance().openDialog(_payload); self.successDialog.current.getInstance().openDialog(_payload);
}, },
complete() { complete: () => {
self.closeLoading(); this.closeLoading();
}, },
error(res) { error: res => {
this.closeLoading();
Dialog.alert({ Dialog.alert({
language: aliwareIntl.currentLanguageCode || 'zh-cn', language: aliwareIntl.currentLanguageCode || 'zh-cn',
content: locale.publishFailed, content: locale.publishFailed,
}); });
self.closeLoading();
}, },
}); });
}; };

View File

@ -93,7 +93,6 @@ class ServiceList extends React.Component {
]; ];
request({ request({
url: `v1/ns/catalog/services?${parameter.join('&')}`, url: `v1/ns/catalog/services?${parameter.join('&')}`,
beforeSend: () => this.openLoading(),
success: ({ count = 0, serviceList = [] } = {}) => { success: ({ count = 0, serviceList = [] } = {}) => {
this.setState({ this.setState({
dataSource: serviceList, dataSource: serviceList,
@ -106,7 +105,6 @@ class ServiceList extends React.Component {
total: 0, total: 0,
currentPage: 0, currentPage: 0,
}), }),
complete: () => this.closeLoading(),
}); });
} }

View File

@ -1,15 +0,0 @@
function getValue(key) {
if (!document.cookie) return null;
const list = document.cookie.split(';') || [];
for (const item of list) {
const [k = '', v = ''] = item.split('=');
if (k.trim() === key) return v;
}
return null;
}
function setValue(key, value) {
document.cookie = `${key}=${value}`;
}
export default { getValue, setValue };

View File

@ -1,24 +0,0 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
module.exports = {
set(key, value) {
window.localStorage.setItem(key, value);
},
get(key) {
return window.localStorage.getItem(key);
},
remove(key) {
window.localStorage.removeItem(key);
},
};

View File

@ -57,7 +57,11 @@ export const isJsonString = str => {
}; };
export const generateUrl = (url, params) => { export const generateUrl = (url, params) => {
return [url, '?', Object.keys(params).map(key => [key, params[key].join('=')].join('&'))].join( return [
'' url,
); '?',
Object.keys(params)
.map(key => [key, params[key]].join('='))
.join('&'),
].join('');
}; };

View File

@ -43,17 +43,23 @@ const request = () => {
error => { error => {
if (error.response) { if (error.response) {
const { data = {}, status } = error.response; const { data = {}, status } = error.response;
if (status === 403 && data.message === 'token invalid!') { let message = `HTTP ERROR: ${status}`;
if (typeof data === 'string') {
message = data;
} else if (typeof data === 'object') {
message = data.message;
}
Message.error(message);
if ([401, 403].includes(status) && ['unknown user!', 'token invalid'].includes(message)) {
localStorage.removeItem('token'); localStorage.removeItem('token');
const [baseUrl] = location.href.split('#'); const [baseUrl] = location.href.split('#');
location.href = `${baseUrl}#/login`; location.href = `${baseUrl}#/login`;
return Promise.reject(error);
} }
Message.error(data && typeof data === 'string' ? data : `HTTP ERROR: ${status}`); return Promise.reject(error.response);
return error.response;
} }
Message.error(API_GENERAL_ERROR_MESSAGE); Message.error(API_GENERAL_ERROR_MESSAGE);
return error; return Promise.reject(error);
} }
); );

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long