diff --git a/xiuosiot-frontend/src/api/user/index.js b/xiuosiot-frontend/src/api/user/index.js index 7c66bbe..bb4c0bd 100644 --- a/xiuosiot-frontend/src/api/user/index.js +++ b/xiuosiot-frontend/src/api/user/index.js @@ -18,11 +18,11 @@ export function getGifCode(data) { }) } -export function getInfo(token) { +export function getInfo() { return request({ - url: '/vue-admin-template/user/info', + url: '/user/getSession', method: 'get', - params: { token } + params: {} }) } diff --git a/xiuosiot-frontend/src/assets/images/slide1.png b/xiuosiot-frontend/src/assets/images/dashengda-slide1.png similarity index 100% rename from xiuosiot-frontend/src/assets/images/slide1.png rename to xiuosiot-frontend/src/assets/images/dashengda-slide1.png diff --git a/xiuosiot-frontend/src/assets/images/slide2.png b/xiuosiot-frontend/src/assets/images/dashengda-slide2.png similarity index 100% rename from xiuosiot-frontend/src/assets/images/slide2.png rename to xiuosiot-frontend/src/assets/images/dashengda-slide2.png diff --git a/xiuosiot-frontend/src/assets/images/hangxiao-slide1.png b/xiuosiot-frontend/src/assets/images/hangxiao-slide1.png new file mode 100644 index 0000000..d1fe9a7 Binary files /dev/null and b/xiuosiot-frontend/src/assets/images/hangxiao-slide1.png differ diff --git a/xiuosiot-frontend/src/assets/images/hangxiao-slide2.png b/xiuosiot-frontend/src/assets/images/hangxiao-slide2.png new file mode 100644 index 0000000..90176af Binary files /dev/null and b/xiuosiot-frontend/src/assets/images/hangxiao-slide2.png differ diff --git a/xiuosiot-frontend/src/layout/components/Navbar.vue b/xiuosiot-frontend/src/layout/components/Navbar.vue index 1580b5e..a592add 100644 --- a/xiuosiot-frontend/src/layout/components/Navbar.vue +++ b/xiuosiot-frontend/src/layout/components/Navbar.vue @@ -35,7 +35,7 @@ export default { }, async logout() { await this.$store.dispatch('user/logout') - this.$router.push(`/login?redirect=${this.$route.fullPath}`) + this.$router.push('/login') } } } diff --git a/xiuosiot-frontend/src/layout/components/Sidebar/index.vue b/xiuosiot-frontend/src/layout/components/Sidebar/index.vue index 408adfd..ddfc6b9 100644 --- a/xiuosiot-frontend/src/layout/components/Sidebar/index.vue +++ b/xiuosiot-frontend/src/layout/components/Sidebar/index.vue @@ -32,7 +32,7 @@ export default { 'sidebar' ]), routes() { - return this.$router.options.routes + return this.$store.state.user.routes }, activeMenu() { const route = this.$route diff --git a/xiuosiot-frontend/src/permission.js b/xiuosiot-frontend/src/permission.js index f59ba2e..2737c84 100644 --- a/xiuosiot-frontend/src/permission.js +++ b/xiuosiot-frontend/src/permission.js @@ -2,11 +2,13 @@ * @Author: 龚祖望 573413756@qq.com * @Date: 2022-05-16 09:16:41 * @LastEditors: 龚祖望 573413756@qq.com - * @LastEditTime: 2022-09-16 10:39:39 + * @LastEditTime: 2022-10-09 15:28:49 * @FilePath: \dashengda\src\permission.js * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE */ import router from './router' +import store from './store' +import { Message } from 'element-ui' import NProgress from 'nprogress' // progress bar import 'nprogress/nprogress.css' // progress bar style import { getToken } from '@/utils/auth' // get token from cookie @@ -31,8 +33,31 @@ router.beforeEach(async(to, from, next) => { next({ path: '/overview' }) NProgress.done() } else { - next() - NProgress.done() + const corpName = store.getters.corpName + if (corpName) { + next() + NProgress.done() + } else { + try { + const res = await store.dispatch('user/getInfo') + store.dispatch('user/generateRoutes', res).then(() => { + router.addRoutes(store.getters.asyncRouters) + next() + }).catch(async err => { + // remove token and go to login page to re-login + await store.dispatch('user/resetToken') + Message.error(err || 'Has Error') + next(`/login?redirect=${to.path}`) + NProgress.done() + }) + } catch (error) { + // remove token and go to login page to re-login + await store.dispatch('user/resetToken') + Message.error(error || 'Has Error') + next(`/login?redirect=${to.path}`) + NProgress.done() + } + } } } else { /* has no token*/ diff --git a/xiuosiot-frontend/src/router/index.js b/xiuosiot-frontend/src/router/index.js index d828f12..13fa61b 100644 --- a/xiuosiot-frontend/src/router/index.js +++ b/xiuosiot-frontend/src/router/index.js @@ -75,6 +75,26 @@ export const constantRoutes = [ component: () => import('@/views/solution/qianjiang'), hidden: true }, + { + path: '/configuration/development', + component: () => import('@/views/configuration/development'), + hidden: true + }, + { + path: '/configuration/transferStation', + component: () => import('@/views/configuration/transferStation'), + hidden: true + }, + { + path: '/configuration/AiCity', + component: () => import('@/views/configuration/AiCity'), + hidden: true + }, + { + path: '/configuration/bridgeMachine', + component: () => import('@/views/configuration/bridgeMachine'), + hidden: true + }, { path: '/login', component: () => import('@/views/login/index'), @@ -176,37 +196,43 @@ export const constantRoutes = [ } ] }, + // { + // path: '/configuration', + // component: Layout, + // name: 'Configuration', + // meta: { title: '组态管理', icon: 'configuration' }, + // children: [ + // { + // path: 'development', + // name: 'Development', + // component: () => import('@/views/configuration/development'), + // meta: { title: '组态开发' } + // }, + // { + // path: 'transferStation', + // name: 'TransferStation', + // component: () => import('@/views/configuration/transferStation'), + // meta: { title: '变电站' } + // }, + // { + // path: 'aicity', + // name: 'AiCity', + // component: () => import('@/views/configuration/AiCity'), + // meta: { title: '智慧城市' } + // }, + // { + // path: 'bridgeMachine', + // name: 'BridgeMachine', + // component: () => import('@/views/configuration/bridgeMachine'), + // meta: { title: '大方架桥机' } + // } + // ] + // }, { - path: '/configuration', + path: '/configureApp', component: Layout, - name: 'Configuration', - meta: { title: '组态管理', icon: 'configuration' }, - children: [ - { - path: 'development', - name: 'Development', - component: () => import('@/views/configuration/development'), - meta: { title: '组态开发' } - }, - { - path: 'transferStation', - name: 'TransferStation', - component: () => import('@/views/configuration/transferStation'), - meta: { title: '变电站' } - }, - { - path: 'aicity', - name: 'AiCity', - component: () => import('@/views/configuration/AiCity'), - meta: { title: '智慧城市' } - }, - { - path: 'bridgeMachine', - name: 'BridgeMachine', - component: () => import('@/views/configuration/bridgeMachine'), - meta: { title: '大方架桥机' } - } - ] + name: 'configureApp', + meta: { title: '组态应用', icon: 'configuration', disabled: true } }, { path: '/log', @@ -228,48 +254,36 @@ export const constantRoutes = [ } ] }, - { - path: '/data', - component: Layout, - name: 'Data', - meta: { title: '数据管理', icon: 'data' }, - children: [ - { - path: 'realTime', - name: 'RealTime', - component: () => import('@/views/data/realTime/index'), - meta: { title: '实时数据' } - }, - { - path: 'history', - name: 'History', - component: () => import('@/views/data/history/index'), - meta: { title: '历史数据' } - }, - { - path: 'value', - name: 'Value', - component: () => import('@/views/data/value/index'), - meta: { title: '数据价值' } - } - ] - }, - - // { - // path: 'external-link', - // component: Layout, - // children: [ - // { - // path: 'https://panjiachen.github.io/vue-element-admin-site/#/', - // meta: { title: 'External Link', icon: 'link' } - // } - // ] - // }, - - // 404 page must be placed at the end !!! { path: '*', redirect: '/404', hidden: true } ] +export const asyncRouter = [{ + path: '/data', + component: Layout, + name: 'Data', + meta: { title: '数据管理', icon: 'data', permission: ['dashengda'] }, + children: [ + { + path: 'realTime', + name: 'RealTime', + component: () => import('@/views/data/realTime/index'), + meta: { title: '实时数据', permission: ['dashengda'] } + }, + { + path: 'history', + name: 'History', + component: () => import('@/views/data/history/index'), + meta: { title: '历史数据', permission: ['dashengda'] } + }, + { + path: 'value', + name: 'Value', + component: () => import('@/views/data/value/index'), + meta: { title: '数据价值', permission: ['dashengda'] } + } + ] +}] + const createRouter = () => new Router({ mode: 'history', // require service support scrollBehavior: () => ({ y: 0 }), @@ -277,6 +291,7 @@ const createRouter = () => new Router({ }) const router = createRouter() +router.addRoutes(asyncRouter) // Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465 export function resetRouter() { diff --git a/xiuosiot-frontend/src/store/getters.js b/xiuosiot-frontend/src/store/getters.js index 771dee6..1f42a78 100644 --- a/xiuosiot-frontend/src/store/getters.js +++ b/xiuosiot-frontend/src/store/getters.js @@ -4,6 +4,9 @@ const getters = { token: state => state.user.token, avatar: state => state.user.avatar, name: state => state.user.name, - corpName: state => state.user.corpName + corpName: state => state.user.corpName, + abbrCorpName: state => state.user.abbrCorpName, + asyncRouters: state => state.user.asyncRouters, + routes: state => state.user.routes } export default getters diff --git a/xiuosiot-frontend/src/store/modules/user.js b/xiuosiot-frontend/src/store/modules/user.js index a892434..b9de07a 100644 --- a/xiuosiot-frontend/src/store/modules/user.js +++ b/xiuosiot-frontend/src/store/modules/user.js @@ -2,20 +2,50 @@ * @Author: 龚祖望 573413756@qq.com * @Date: 2022-05-16 09:16:41 * @LastEditors: 龚祖望 573413756@qq.com - * @LastEditTime: 2022-08-31 20:21:28 + * @LastEditTime: 2022-10-09 15:39:05 * @FilePath: \dashengda\src\store\modules\user.js * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE */ -import { login, logout } from '@/api/user' +import { login, logout, getInfo } from '@/api/user' import { getToken, setToken, removeToken } from '@/utils/auth' -import { resetRouter } from '@/router' +import { resetRouter, constantRoutes, asyncRouter } from '@/router' + +function filterAsyncRouter(routerMap, roles) { + const accessedRouters = routerMap.filter(route => { + if (hasPermission([roles.permissions], route)) { + if (route.children && route.children.length) { + route.children = filterAsyncRouter(route.children, roles) + } + return true + } + return false + }) + return accessedRouters +} + +function hasPermission(permission, route) { + if (route.meta && route.meta.permission) { + let flag = true + for (let i = 0; i < permission.length; i++) { + flag = route.meta.permission.includes(permission[i]) + if (flag) { + return true + } + } + return false + } + return true +} const getDefaultState = () => { return { token: getToken(), name: '', avatar: '', - corpName: '浙江大胜达包装股份有限公司' + corpName: '', + abbrCorpName: '', + permissions: [], + asyncRouters: [] } } @@ -33,6 +63,19 @@ const mutations = { }, SET_AVATAR: (state, avatar) => { state.avatar = avatar + }, + SET_CORPNAME: (state, corpName) => { + state.corpName = corpName + }, + SET_ABBRCORPNAME: (state, abbrCorpName) => { + state.abbrCorpName = abbrCorpName + }, + SET_PERMISSIONS: (state, permissions) => { + state.permissions = permissions + }, + SET_ROUTERS: (state, asyncRouters) => { + state.asyncRouters = asyncRouters + state.routes = [...constantRoutes, ...asyncRouters] } } @@ -42,7 +85,6 @@ const actions = { const { username, password } = userInfo const data = { username, password } return new Promise((resolve, reject) => { - // console.log('res', res) login(data).then(res => { if (res.code === '200000') { setToken('success') @@ -57,26 +99,26 @@ const actions = { }) }, - // // get user info - // getInfo({ commit, state }) { - // return new Promise((resolve, reject) => { - // getInfo(state.token).then(response => { - // const { data } = response + // get user info + getInfo({ commit, state }) { + return new Promise((resolve, reject) => { + getInfo(state.token).then(response => { + const { data } = response - // if (!data) { - // return reject('Verification failed, please Login again.') - // } + if (!data) { + return reject('Verification failed, please Login again.') + } + // const { name, avatar } = data - // const { name, avatar } = data - - // commit('SET_NAME', name) - // commit('SET_AVATAR', avatar) - // resolve(data) - // }).catch(error => { - // reject(error) - // }) - // }) - // }, + commit('SET_CORPNAME', data.corpname) + commit('SET_ABBRCORPNAME', data.abbrcorpname) + commit('SET_PERMISSIONS', [data.permissions]) + resolve(data) + }).catch(error => { + reject(error) + }) + }) + }, // user logout logout({ commit, state }) { @@ -99,6 +141,14 @@ const actions = { commit('RESET_STATE') resolve() }) + }, + generateRoutes({ commit }, roles) { + return new Promise(resolve => { + const accessedRouters = filterAsyncRouter(asyncRouter, roles) + console.log('accessedRouters', accessedRouters) + commit('SET_ROUTERS', accessedRouters) + resolve() + }) } } diff --git a/xiuosiot-frontend/src/views/components/Cover/index.vue b/xiuosiot-frontend/src/views/components/Cover/index.vue index 9dbe229..00b7410 100644 --- a/xiuosiot-frontend/src/views/components/Cover/index.vue +++ b/xiuosiot-frontend/src/views/components/Cover/index.vue @@ -48,6 +48,28 @@ +