From c22a593396f29c4bc6fc0c27c793544888821755 Mon Sep 17 00:00:00 2001 From: LoadChange Date: Sun, 15 Mar 2020 20:51:01 +0800 Subject: [PATCH] fix: closes #2443 --- .../resources/static/console-fe/src/globalLib.js | 13 +++++++++++-- .../static/console-fe/src/utils/request.js | 10 +++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/console/src/main/resources/static/console-fe/src/globalLib.js b/console/src/main/resources/static/console-fe/src/globalLib.js index 852ce84a1..381c6a67a 100644 --- a/console/src/main/resources/static/console-fe/src/globalLib.js +++ b/console/src/main/resources/static/console-fe/src/globalLib.js @@ -15,6 +15,14 @@ import projectConfig from './config'; import $ from 'jquery'; import { Message } from '@alifd/next'; +function goLogin() { + const url = window.location.href; + localStorage.removeItem('token'); + const base_url = url.split('#')[0]; + console.log('base_url', base_url); + window.location = `${base_url}#/login`; +} + const global = window; /** @@ -484,9 +492,10 @@ const request = (function(_global) { config = handleMiddleWare.apply(this, [config, ...args, middlewareBackList]); let token = {}; try { - token = JSON.parse(localStorage.token || '{}'); + token = JSON.parse(localStorage.token); } catch (e) { - console.log(e); + console.log('Token Erro', localStorage.token, e); + goLogin(); } const { accessToken = '' } = token; const [url, paramsStr = ''] = config.url.split('?'); diff --git a/console/src/main/resources/static/console-fe/src/utils/request.js b/console/src/main/resources/static/console-fe/src/utils/request.js index c3c16db89..a03d109e4 100644 --- a/console/src/main/resources/static/console-fe/src/utils/request.js +++ b/console/src/main/resources/static/console-fe/src/utils/request.js @@ -7,6 +7,13 @@ import { isPlainObject } from './nacosutil'; const API_GENERAL_ERROR_MESSAGE = 'Request error, please try again later!'; +function goLogin() { + const url = window.location.href; + localStorage.removeItem('token'); + const base_url = url.split('#')[0]; + window.location.href = `${base_url}#/login`; +} + const request = () => { const instance = axios.create(); @@ -19,9 +26,10 @@ const request = () => { if (!url.includes('auth/users/login')) { let token = {}; try { - token = JSON.parse(localStorage.token || '{}'); + token = JSON.parse(localStorage.token); } catch (e) { console.log(e); + goLogin(); } const { accessToken = '' } = token; config.params.accessToken = accessToken;