From 275a056ab6edc43761031614ceee184c23d70880 Mon Sep 17 00:00:00 2001 From: X Date: Thu, 10 Dec 2020 16:09:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=9F=BA=E7=A1=80js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/main.js | 3 +- src/router/index.js | 7 +++-- src/store/getters.js | 4 +++ src/store/side-menu.js | 2 +- src/utils/clipboard.js | 4 +-- src/utils/cookies.js | 15 ++++++++++ src/utils/request.js | 68 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 src/store/getters.js create mode 100644 src/utils/cookies.js create mode 100644 src/utils/request.js diff --git a/package.json b/package.json index fe5667c..cd7959e 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "highlight.js": "^10.1.1", "js-beautify": "^1.11.0", "litepicker": "^1.5.7", - "lodash": "^4.17.19", + "lodash": "^4.17.20", "postcss-loader": "3", "raw-loader": "0.5.1", "sass-loader": "^9.0.2", diff --git a/src/main.js b/src/main.js index 37299c4..721bc30 100644 --- a/src/main.js +++ b/src/main.js @@ -12,7 +12,8 @@ import "./assets/sass/app.scss"; Vue.config.productionTip = false; new Vue({ + el: '#app', router, store, render: h => h(App) -}).$mount("#app"); +}); diff --git a/src/router/index.js b/src/router/index.js index 80cbf08..1449f59 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -280,13 +280,14 @@ const routes = [ component: () => import('@/views/Register') }, { - path: "/error-page", + path: "/404", name: "error-page", component: () => import('@/views/ErrorPage') }, { - path: "/*", - component: () => import('@/views/ErrorPage') + path: "*", + redirect: '/404', + hidden: true, } ]; diff --git a/src/store/getters.js b/src/store/getters.js new file mode 100644 index 0000000..55c383f --- /dev/null +++ b/src/store/getters.js @@ -0,0 +1,4 @@ +const getters = { + +} +export default getters diff --git a/src/store/side-menu.js b/src/store/side-menu.js index 494a40f..69038aa 100644 --- a/src/store/side-menu.js +++ b/src/store/side-menu.js @@ -8,7 +8,7 @@ const state = () => { }, { icon: "BoxIcon", - pageName: "side-menu-inbox", + pageName: "profile-overview-3", title: "个人资料" }, { diff --git a/src/utils/clipboard.js b/src/utils/clipboard.js index e2c7a56..fba2578 100644 --- a/src/utils/clipboard.js +++ b/src/utils/clipboard.js @@ -3,7 +3,7 @@ import Clipboard from 'clipboard' function clipboardSuccess() { Vue.prototype.$message({ - message: 'Copy successfully', + message: '拷贝成功', type: 'success', duration: 1500 }) @@ -11,7 +11,7 @@ function clipboardSuccess() { function clipboardError() { Vue.prototype.$message({ - message: 'Copy failed', + message: '拷贝失败', type: 'error' }) } diff --git a/src/utils/cookies.js b/src/utils/cookies.js new file mode 100644 index 0000000..95f238c --- /dev/null +++ b/src/utils/cookies.js @@ -0,0 +1,15 @@ +import Cookies from 'js-cookie' + +const TokenKey = 'Admin-Token' + +export function getToken() { + return Cookies.get(TokenKey) +} + +export function setToken(token) { + return Cookies.set(TokenKey, token) +} + +export function removeToken() { + return Cookies.remove(TokenKey) +} diff --git a/src/utils/request.js b/src/utils/request.js new file mode 100644 index 0000000..071a706 --- /dev/null +++ b/src/utils/request.js @@ -0,0 +1,68 @@ +import axios from '@/utils/request' + + +const service = axios.create({ + baseURL: process.env.VUE_APP_BASE_URL, + timeout: 5000 +}) + + +service.interceptors.request.use( + config => { + if (store.getters.token) { + config.headers['X-Token'] = getToken() + } + return config + }, + error => { + return Promise.reject(error) + } +) + + +// response interceptor +service.interceptors.response.use( + response => { + const res = response.data + console.log(res.msg, 'msg') + // if the custom code is not 20000, it is judged as an error. + if (res.code !== 0) { + Message({ + message: res.message || 'Error', + type: 'error', + duration: 5 * 1000 + }) + if (res.code === 50008 || res.code === 50012 || res.code === 50014) { + console.log(res.code, 'res code3') + // to re-login + MessageBox.confirm( + 'You have been logged out, you can cancel to stay on this page, or log in again', + 'Confirm logout', + { + confirmButtonText: 'Re-Login', + cancelButtonText: 'Cancel', + type: 'warning' + } + ).then(() => { + store.dispatch('user/resetToken').then(() => { + location.reload() + }) + }) + } + return Promise.reject(new Error(res.message || 'Error')) + } else { + return res + } + }, + error => { + console.log('err' + error) // for debug + Message({ + message: error.message, + type: 'error', + duration: 5 * 1000 + }) + return Promise.reject(error) + } +) + +export default service