From 3133e0a026e533ab9df8ac7f2f26631a28605c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B0=B4=E8=90=BD=28YangLei=29?= Date: Fri, 25 Jun 2021 17:21:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AF=B7=E6=B1=82=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 - src/components/table/index.vue | 9 +- src/main.js | 4 +- src/pages/frame/view/login/Login.vue | 6 +- src/pages/system/view/menu/MenuManagement.vue | 31 +++- src/router/interceptors.js | 147 +++++++++--------- src/utils/requestUtil.js | 27 +--- vue.config.js | 2 - 8 files changed, 120 insertions(+), 107 deletions(-) diff --git a/package.json b/package.json index e7e2571..3c6f911 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,6 @@ "date-fns": "^2.14.0", "enquire.js": "^2.1.6", "highlight.js": "^10.2.1", - "js-cookie": "^2.2.1", "mockjs": "^1.1.0", "nprogress": "^0.2.0", "tailwindcss": "npm:@tailwindcss/postcss7-compat@^2.2.2", diff --git a/src/components/table/index.vue b/src/components/table/index.vue index 7f6b4a9..0e9af81 100644 --- a/src/components/table/index.vue +++ b/src/components/table/index.vue @@ -5,7 +5,7 @@ -
+
重置 查询 @@ -44,7 +44,6 @@ diff --git a/src/router/interceptors.js b/src/router/interceptors.js index ec6f12f..0f1856f 100644 --- a/src/router/interceptors.js +++ b/src/router/interceptors.js @@ -1,82 +1,81 @@ -import Cookie from 'js-cookie' // 401拦截 const resp401 = { - /** - * 响应数据之前做点什么 - * @param response 响应对象 - * @param options 应用配置 包含: {router, i18n, store, message} - * @returns {*} - */ - onFulfilled(response, options) { - const {message} = options - if (response.code === 401) { - message.error('无此权限') - } - return response - }, - /** - * 响应出错时执行 - * @param error 错误对象 - * @param options 应用配置 包含: {router, i18n, store, message} - * @returns {Promise} - */ - onRejected(error, options) { - const {message} = options - const {response} = error - if (response.status === 401) { - message.error('无此权限') - } - return Promise.reject(error) - } -} + /** + * 响应数据之前做点什么 + * @param response 响应对象 + * @param options 应用配置 包含: {router, i18n, store, message} + * @returns {*} + */ + onFulfilled(response, options) { + const { message } = options; + if (response.code === 401) { + message.error('无此权限'); + } + return response; + }, + /** + * 响应出错时执行 + * @param error 错误对象 + * @param options 应用配置 包含: {router, i18n, store, message} + * @returns {Promise} + */ + onRejected(error, options) { + const { message } = options; + const { response } = error; + if (response.status === 401) { + message.error('无此权限'); + } + return Promise.reject(error); + }, +}; const resp403 = { - onFulfilled(response, options) { - const {message} = options - if (response.code === 403) { - message.error('请求被拒绝') - } - return response - }, - onRejected(error, options) { - const {message} = options - const {response} = error - if (response.status === 403) { - message.error('请求被拒绝') - } - return Promise.reject(error) - } -} + onFulfilled(response, options) { + const { message } = options; + if (response.code === 403) { + message.error('请求被拒绝'); + } + return response; + }, + onRejected(error, options) { + const { message } = options; + const { response } = error; + if (response.status === 403) { + message.error('请求被拒绝'); + } + return Promise.reject(error); + }, +}; const reqCommon = { - /** - * 发送请求之前做些什么 - * @param config axios config - * @param options 应用配置 包含: {router, i18n, store, message} - * @returns {*} - */ - onFulfilled(config, options) { - const {message} = options - const {url, xsrfCookieName} = config - if (url.indexOf('login') === -1 && xsrfCookieName && !Cookie.get(xsrfCookieName)) { - message.warning('认证 token 已过期,请重新登录') - } - return config - }, - /** - * 请求出错时做点什么 - * @param error 错误对象 - * @param options 应用配置 包含: {router, i18n, store, message} - * @returns {Promise} - */ - onRejected(error, options) { - const {message} = options - message.error(error.message) - return Promise.reject(error) - } -} + /** + * 发送请求之前做些什么 + * @param config axios config + * @param options 应用配置 包含: {router, i18n, store, message} + * @returns {*} + */ + onFulfilled(config, options) { + const { message } = options; + const { url, xsrfCookieName } = config; + if (url.indexOf('login') === -1 && xsrfCookieName) { + message.warning('认证 token 已过期,请重新登录'); + } + return config; + }, + /** + * 请求出错时做点什么 + * @param error 错误对象 + * @param options 应用配置 包含: {router, i18n, store, message} + * @returns {Promise} + */ + onRejected(error, options) { + const { message } = options; + message.error(error.message); + return Promise.reject(error); + }, +}; export default { - request: [reqCommon], // 请求拦截 - response: [resp401, resp403] // 响应拦截 -} + request: [reqCommon], // 请求拦截 + response: [resp401, resp403], // 响应拦截 +}; diff --git a/src/utils/requestUtil.js b/src/utils/requestUtil.js index e02f544..a210156 100644 --- a/src/utils/requestUtil.js +++ b/src/utils/requestUtil.js @@ -14,9 +14,10 @@ axios.defaults.xsrfCookieName = xsrfHeaderName; /** * @param {*} info 提示函数 */ -function loadResponseInterceptor() { +function loadResponseInterceptor({ router }) { axios.interceptors.request.use( function(config) { + console.log(getToken(), config); // 在发送请求之前做些什么 config.headers = { ...config.headers, @@ -40,10 +41,15 @@ function loadResponseInterceptor() { if (data.code === 'sys.success') { return data.data; } + + if (data.code === 'error.system.authc') { + router.push('/login'); + } notification.error({ message: data.code, description: h => h('pre', data.message), }); + return Promise.reject(data.message); }, function(error) { @@ -58,14 +64,6 @@ function loadResponseInterceptor() { ); } -// 认证类型 -const AUTH_TYPE = { - BEARER: 'Bearer', - BASIC: 'basic', - AUTH1: 'auth1', - AUTH2: 'auth2', -}; - // http method const METHOD = { GET: 'get', @@ -136,13 +134,4 @@ function checkAuthorization() { return !!getToken(); } -export { - METHOD, - AUTH_TYPE, - request, - parseUrlParams, - loadResponseInterceptor, - setToken, - checkAuthorization, - clearToken, -}; +export { METHOD, request, parseUrlParams, loadResponseInterceptor, setToken, checkAuthorization, clearToken }; diff --git a/vue.config.js b/vue.config.js index b89d494..663d87d 100644 --- a/vue.config.js +++ b/vue.config.js @@ -177,7 +177,6 @@ const assetsCDN = { nprogress: 'NProgress', clipboard: 'ClipboardJS', '@antv/data-set': 'DataSet', - 'js-cookie': 'Cookies', }, css: [], js: [ @@ -188,7 +187,6 @@ const assetsCDN = { '//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js', '//cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js', '//cdn.jsdelivr.net/npm/@antv/data-set@0.11.4/build/data-set.min.js', - '//cdn.jsdelivr.net/npm/js-cookie@2.2.1/src/js.cookie.min.js', ], }; -- GitLab