From fdd70d1829c17b245a63fe99776e0554482a1440 Mon Sep 17 00:00:00 2001 From: pnoker Date: Fri, 6 Mar 2020 00:43:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dc3/docker-compose.yml | 1 + dc3/nginx/conf.d/default.conf | 8 +++++-- src/config/axios.js | 10 +++----- src/config/permission.js | 35 ---------------------------- src/main.js | 1 - src/router/router.js | 28 ++++++++++++++++++++++ src/store/modules/user.js | 26 ++++++++++++--------- src/util/auth.js | 2 +- src/util/util.js | 11 ++++++++- src/views/device/Device.vue | 9 +++---- src/views/driver/Driver.vue | 2 ++ src/views/driver/DriverAttribute.vue | 6 +++-- src/views/driver/DriverInfo.vue | 9 +++---- src/views/group/Group.vue | 3 +++ src/views/layout/Layout.vue | 8 ++++++- src/views/point/Point.vue | 6 +++-- src/views/point/PointAttribute.vue | 6 +++-- src/views/point/PointInfo.vue | 12 +++++----- src/views/point/PointValue.vue | 7 +++--- src/views/profile/Profile.vue | 6 +++-- vue.config.js | 8 ------- 21 files changed, 111 insertions(+), 93 deletions(-) delete mode 100644 src/config/permission.js diff --git a/dc3/docker-compose.yml b/dc3/docker-compose.yml index 8648bc1..d5b0bf0 100644 --- a/dc3/docker-compose.yml +++ b/dc3/docker-compose.yml @@ -20,3 +20,4 @@ services: networks: dc3net: driver: 'bridge' +... diff --git a/dc3/nginx/conf.d/default.conf b/dc3/nginx/conf.d/default.conf index d67e112..0c5ba85 100644 --- a/dc3/nginx/conf.d/default.conf +++ b/dc3/nginx/conf.d/default.conf @@ -1,6 +1,6 @@ server { listen 80; - server_name dc3.com localhost; + server_name dc3-web localhost; location / { root /usr/share/nginx/html; @@ -17,7 +17,7 @@ server { server { listen 443 ssl; - server_name dc3.com localhost; + server_name dc3-web localhost; add_header X-Xss-Protection 1; add_header X-Frame-Options DENY; @@ -44,6 +44,10 @@ server { proxy_pass http://dc3-manager:8400/; } + location ^~/data_api/ { + proxy_pass http://dc3-data:8500/; + } + error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; diff --git a/src/config/axios.js b/src/config/axios.js index aade192..9339113 100644 --- a/src/config/axios.js +++ b/src/config/axios.js @@ -1,10 +1,10 @@ import axios from 'axios' import router from '@/router/router' import store from '@/store/store'; -import {Message} from 'element-ui' import NProgress from 'nprogress' import 'nprogress/nprogress.css' import {getToken} from '@/util/auth' +import {showError} from '@/util/util' //返回其他状态码 axios.defaults.validateStatus = function (status) { @@ -38,17 +38,13 @@ axios.interceptors.response.use(res => { if (status === 401) store.dispatch('FedLogOut').then(() => router.push({path: '/login'})); // 如果请求为 !ok 默认统一处理 if (!ok) { - Message({ - center: true, - type: 'error', - showClose: true, - message: message - }); + showError(message); return Promise.reject(new Error(message)); } return res.data; }, error => { NProgress.done(); + showError('未知错误'); return Promise.reject(new Error(error)); }); diff --git a/src/config/permission.js b/src/config/permission.js deleted file mode 100644 index c0f2c1f..0000000 --- a/src/config/permission.js +++ /dev/null @@ -1,35 +0,0 @@ -import router from '@/router/router' -import store from '@/store/store' -import NProgress from 'nprogress' -import 'nprogress/nprogress.css' -import {getToken} from '@/util/auth' - -router.beforeEach((to, from, next) => { - NProgress.start(); - const meta = to.meta || {}; - if (getToken()) { - if (to.path === '/login') { //如果登录成功访问登录页跳转到主页 - next({path: '/'}) - } else { - //如果用户信息为空则获取用户信息,获取用户信息失败,跳转到登录页 - if (store.getters.token.length === 0) { - store.dispatch('FedLogOut').then(() => { - next({path: '/login'}) - }) - } else { - next() - } - } - } else { - //判断是否需要认证,没有登录访问去登录页 - if (meta.isAuth === false) { - next() - } else { - next('/login') - } - } -}); - -router.afterEach(() => { - NProgress.done(); -}); diff --git a/src/main.js b/src/main.js index ae610b1..55900c6 100644 --- a/src/main.js +++ b/src/main.js @@ -5,7 +5,6 @@ import App from './App' import router from './router/router' import store from './store/store' import './plugins/element/element.js' -import './config/permission'; Vue.use(VueAxios, axios); diff --git a/src/router/router.js b/src/router/router.js index 6d7c3ad..bc0ca5e 100644 --- a/src/router/router.js +++ b/src/router/router.js @@ -2,6 +2,10 @@ import Vue from 'vue' import VueRouter from 'vue-router' import CommonRouter from './common/index' import ViewsRouter from './views/index' +import NProgress from "nprogress"; +import {getToken} from "@/util/auth"; +import store from "@/store/store"; +import {checkTokenValid} from "@/api/user"; Vue.use(VueRouter); @@ -22,6 +26,30 @@ const router = new VueRouter({ routes: [] }); +router.beforeEach((to, from, next) => { + NProgress.start(); + const meta = to.meta || {}; + if (meta.isAuth !== true) { + next(); + } else { + checkTokenValid(getToken()).then(res => { + if (res.ok && store.getters.token.length > 0) { + next(); + } else { + throw new Error(res.message); + } + }).catch(() => { + store.dispatch('ClearToken').then(() => { + next({path: '/login'}) + }); + }); + } +}); + +router.afterEach(() => { + NProgress.done(); +}); + router.addRoutes([...CommonRouter, ...ViewsRouter]); export default router diff --git a/src/store/modules/user.js b/src/store/modules/user.js index 888e433..b4bb6b5 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -1,6 +1,6 @@ -import {getStore, setStore} from '@/util/store' -import {setToken} from '@/util/auth' -import {checkTokenValid, generateToken} from '@/api/user' +import {getStore, removeStore, setStore} from '@/util/store' +import {removeToken, setToken} from '@/util/auth' +import {generateToken} from '@/api/user' const user = { state: { @@ -20,14 +20,9 @@ const user = { }) }) }, - CheckTokenValid(body) { - return new Promise((resolve, reject) => { - checkTokenValid(body).then(() => { - resolve(); - }).catch(error => { - reject(error) - }) - }) + ClearToken({commit}) { + commit('REMOVE_TOKEN'); + commit('REMOVE_USER'); } }, mutations: { @@ -39,6 +34,15 @@ const user = { SET_USER: (state, userInfo) => { state.userInfo = userInfo; setStore({name: 'userInfo', content: state.userInfo}) + }, + REMOVE_TOKEN: (state) => { + removeToken(); + state.token = ''; + removeStore({name: 'token', type: 'session'}); + }, + REMOVE_USER: (state) => { + state.userInfo = ''; + removeStore({name: 'userInfo'}); } } }; diff --git a/src/util/auth.js b/src/util/auth.js index c39993f..22df6a5 100644 --- a/src/util/auth.js +++ b/src/util/auth.js @@ -13,4 +13,4 @@ export function setToken(token) { export function removeToken() { return Cookies.remove(TokenKey); -} \ No newline at end of file +} diff --git a/src/util/util.js b/src/util/util.js index 08fdc5a..8aefbde 100644 --- a/src/util/util.js +++ b/src/util/util.js @@ -375,4 +375,13 @@ export const openWindow = (url, title, w, h) => { if (window.focus) { newWindow.focus() } -}; \ No newline at end of file +}; + +export const showError = (message) => { + Message({ + center: true, + type: 'error', + showClose: true, + message: message + }); +}; diff --git a/src/views/device/Device.vue b/src/views/device/Device.vue index 174b88b..b8dbce6 100644 --- a/src/views/device/Device.vue +++ b/src/views/device/Device.vue @@ -189,6 +189,7 @@ const data = res.data; this.page.total = data.total; this.listData = data.records; + }).catch(() => { }).finally(() => { this.loading = false; }); @@ -196,15 +197,13 @@ group() { dictionaryApi.groupDictionary().then(res => { this.listOption.column[this.$refs.crud.findColumnIndex('groupId')].dicData = res.data; - }).catch((e) => { - console.log(e); + }).catch(() => { }); }, profile() { dictionaryApi.profileDictionary().then(res => { this.listOption.column[this.$refs.crud.findColumnIndex('profileId')].dicData = res.data; - }).catch((e) => { - console.log(e); + }).catch(() => { }); }, listAdd(row, done, loading) { @@ -212,6 +211,7 @@ loading(); this.list(this.page); successMessage(); + }).catch(() => { }).finally(() => { done(); }); @@ -233,6 +233,7 @@ loading(); this.list(this.page); successMessage(); + }).catch(() => { }).finally(() => { done(); }); diff --git a/src/views/driver/Driver.vue b/src/views/driver/Driver.vue index 790f9e1..2fb6733 100644 --- a/src/views/driver/Driver.vue +++ b/src/views/driver/Driver.vue @@ -125,6 +125,7 @@ const data = res.data; this.page.total = data.total; this.listData = data.records; + }).catch(() => { }).finally(() => { this.loading = false; }); @@ -146,6 +147,7 @@ loading(); this.list(this.page); successMessage(); + }).catch(() => { }).finally(() => { done(); }); diff --git a/src/views/driver/DriverAttribute.vue b/src/views/driver/DriverAttribute.vue index 12b7f75..b77d910 100644 --- a/src/views/driver/DriverAttribute.vue +++ b/src/views/driver/DriverAttribute.vue @@ -203,6 +203,7 @@ const data = res.data; this.page.total = data.total; this.listData = data.records; + }).catch(() => { }).finally(() => { this.loading = false; }); @@ -210,8 +211,7 @@ driver() { dictionaryApi.driverDictionary().then(res => { this.listOption.column[this.$refs.crud.findColumnIndex('driverId')].dicData = res.data; - }).catch((e) => { - console.log(e); + }).catch(() => { }); }, listAdd(row, done, loading) { @@ -219,6 +219,7 @@ loading(); this.list(this.page); successMessage(); + }).catch(() => { }).finally(() => { done(); }); @@ -240,6 +241,7 @@ loading(); this.list(this.page); successMessage(); + }).catch(() => { }).finally(() => { done(); }); diff --git a/src/views/driver/DriverInfo.vue b/src/views/driver/DriverInfo.vue index 7c228c5..ec667c3 100644 --- a/src/views/driver/DriverInfo.vue +++ b/src/views/driver/DriverInfo.vue @@ -144,6 +144,7 @@ const data = res.data; this.page.total = data.total; this.listData = data.records; + }).catch(() => { }).finally(() => { this.loading = false; }); @@ -151,15 +152,13 @@ profile() { dictionaryApi.profileDictionary().then(res => { this.listOption.column[this.$refs.crud.findColumnIndex('profileId')].dicData = res.data; - }).catch((e) => { - console.log(e); + }).catch(() => { }); }, driverAttribute() { dictionaryApi.driverAttributeDictionary().then(res => { this.listOption.column[this.$refs.crud.findColumnIndex('driverAttributeId')].dicData = res.data; - }).catch((e) => { - console.log(e); + }).catch(() => { }); }, listAdd(row, done, loading) { @@ -167,6 +166,7 @@ loading(); this.list(this.page); successMessage(); + }).catch(() => { }).finally(() => { done(); }); @@ -188,6 +188,7 @@ loading(); this.list(this.page); successMessage(); + }).catch(() => { }).finally(() => { done(); }); diff --git a/src/views/group/Group.vue b/src/views/group/Group.vue index 72fa87f..942ee2d 100644 --- a/src/views/group/Group.vue +++ b/src/views/group/Group.vue @@ -115,6 +115,7 @@ const data = res.data; this.page.total = data.total; this.listData = data.records; + }).catch(() => { }).finally(() => { this.loading = false; }); @@ -124,6 +125,7 @@ loading(); this.list(this.page); successMessage(); + }).catch(() => { }).finally(() => { done(); }); @@ -145,6 +147,7 @@ loading(); this.list(this.page); successMessage(); + }).catch(() => { }).finally(() => { done(); }); diff --git a/src/views/layout/Layout.vue b/src/views/layout/Layout.vue index 1ade20b..e9227ec 100644 --- a/src/views/layout/Layout.vue +++ b/src/views/layout/Layout.vue @@ -69,7 +69,13 @@ this.$message('click on message'); }, handleCommand(command) { - this.$message('click on item ' + command); + if (command === 'logout') { + this.$store.dispatch('ClearToken').then(() => { + this.$router.push('/login'); + }); + } else { + this.$message('click on item ' + command); + } } } } diff --git a/src/views/point/Point.vue b/src/views/point/Point.vue index 5d06eb0..3375981 100644 --- a/src/views/point/Point.vue +++ b/src/views/point/Point.vue @@ -308,6 +308,7 @@ const data = res.data; this.page.total = data.total; this.listData = data.records; + }).catch(() => { }).finally(() => { this.loading = false; }); @@ -315,8 +316,7 @@ profile() { dictionaryApi.profileDictionary().then(res => { this.listOption.column[this.$refs.crud.findColumnIndex('profileId')].dicData = res.data; - }).catch((e) => { - console.log(e); + }).catch(() => { }); }, listAdd(row, done, loading) { @@ -324,6 +324,7 @@ loading(); this.list(this.page); successMessage(); + }).catch(() => { }).finally(() => { done(); }); @@ -345,6 +346,7 @@ loading(); this.list(this.page); successMessage(); + }).catch(() => { }).finally(() => { done(); }); diff --git a/src/views/point/PointAttribute.vue b/src/views/point/PointAttribute.vue index c6403a9..66a4261 100644 --- a/src/views/point/PointAttribute.vue +++ b/src/views/point/PointAttribute.vue @@ -203,6 +203,7 @@ const data = res.data; this.page.total = data.total; this.listData = data.records; + }).catch(() => { }).finally(() => { this.loading = false; }); @@ -210,8 +211,7 @@ driver() { dictionaryApi.driverDictionary().then(res => { this.listOption.column[this.$refs.crud.findColumnIndex('driverId')].dicData = res.data; - }).catch((e) => { - console.log(e); + }).catch(() => { }); }, listAdd(row, done, loading) { @@ -219,6 +219,7 @@ loading(); this.list(this.page); successMessage(); + }).catch(() => { }).finally(() => { done(); }); @@ -240,6 +241,7 @@ loading(); this.list(this.page); successMessage(); + }).catch(() => { }).finally(() => { done(); }); diff --git a/src/views/point/PointInfo.vue b/src/views/point/PointInfo.vue index e1b4b0b..731305e 100644 --- a/src/views/point/PointInfo.vue +++ b/src/views/point/PointInfo.vue @@ -161,6 +161,7 @@ const data = res.data; this.page.total = data.total; this.listData = data.records; + }).catch(() => { }).finally(() => { this.loading = false; }); @@ -168,22 +169,19 @@ device() { dictionaryApi.deviceDictionary('group').then(res => { this.listOption.column[this.$refs.crud.findColumnIndex('deviceId')].dicData = res.data; - }).catch((e) => { - console.log(e); + }).catch(() => { }); }, point() { dictionaryApi.pointDictionary('profile').then(res => { this.listOption.column[this.$refs.crud.findColumnIndex('pointId')].dicData = res.data; - }).catch((e) => { - console.log(e); + }).catch(() => { }); }, pointAttribute() { dictionaryApi.pointAttributeDictionary().then(res => { this.listOption.column[this.$refs.crud.findColumnIndex('pointAttributeId')].dicData = res.data; - }).catch((e) => { - console.log(e); + }).catch(() => { }); }, listAdd(row, done, loading) { @@ -191,6 +189,7 @@ loading(); this.list(this.page); successMessage(); + }).catch(() => { }).finally(() => { done(); }); @@ -212,6 +211,7 @@ loading(); this.list(this.page); successMessage(); + }).catch(() => { }).finally(() => { done(); }); diff --git a/src/views/point/PointValue.vue b/src/views/point/PointValue.vue index 73cdfb7..98a20ef 100644 --- a/src/views/point/PointValue.vue +++ b/src/views/point/PointValue.vue @@ -136,6 +136,7 @@ const data = res.data; this.page.total = data.total; this.listData = data.records; + }).catch(() => { }).finally(() => { this.loading = false; }); @@ -143,15 +144,13 @@ device() { dictionaryApi.deviceDictionary('group').then(res => { this.listOption.column[this.$refs.crud.findColumnIndex('deviceId')].dicData = res.data; - }).catch((e) => { - console.log(e); + }).catch(() => { }); }, point() { dictionaryApi.pointDictionary('profile').then(res => { this.listOption.column[this.$refs.crud.findColumnIndex('pointId')].dicData = res.data; - }).catch((e) => { - console.log(e); + }).catch(() => { }); }, dateChange(date) { diff --git a/src/views/profile/Profile.vue b/src/views/profile/Profile.vue index 7e852c5..638f00f 100644 --- a/src/views/profile/Profile.vue +++ b/src/views/profile/Profile.vue @@ -162,6 +162,7 @@ const data = res.data; this.page.total = data.total; this.listData = data.records; + }).catch(() => { }).finally(() => { this.loading = false; }); @@ -169,8 +170,7 @@ driver() { dictionaryApi.driverDictionary().then(res => { this.listOption.column[this.$refs.crud.findColumnIndex('driverId')].dicData = res.data; - }).catch((e) => { - console.log(e); + }).catch(() => { }); }, listAdd(row, done, loading) { @@ -178,6 +178,7 @@ loading(); this.list(this.page); successMessage(); + }).catch(() => { }).finally(() => { done(); }); @@ -199,6 +200,7 @@ loading(); this.list(this.page); successMessage(); + }).catch(() => { }).finally(() => { done(); }); diff --git a/vue.config.js b/vue.config.js index 3f092de..28d410a 100644 --- a/vue.config.js +++ b/vue.config.js @@ -4,14 +4,6 @@ module.exports = { productionSourceMap: false, devServer: { proxy: { - '/api': { - target: 'http://localhost:8400', - changeOrigin: true, - ws: true, - pathRewrite: { - '^/api': '' - } - }, '/user_api': { target: 'http://dc3-auth:8300', changeOrigin: true,