import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Test1 from '../views/Test1.vue'
import Test2 from '../views/Test2.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'Login',
component: Login,
hidden : true
},
{
path: '/home',
name: 'Home',
component: Home,
hidden:true
},{
path: '/home',
name: '导航一',
component: Home,
children:[
{
path: '/test1',
name: '选项1',
component: Test1
}, {
path: '/test2',
name: '选项2',
component: Test2
}
]
}
]
const router = new VueRouter({
routes
})
export default router
<template>
<div>
<el-container>
<el-header class="homeHeader">
<div class="title">微人事管理系统div>
<el-dropdown class="userInfo" @command="commandHandler">
<span class="el-dropdown-link">
{{ user.name }}<i><img :src="user.userface" alt="">i>
span>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item command="userInfo">个人中心el-dropdown-item>
<el-dropdown-item command="setting">设置el-dropdown-item>
<el-dropdown-item command="logout" divided>注销登录el-dropdown-item>
el-dropdown-menu>
el-dropdown>
el-header>
<el-container>
<el-aside width="200px">
<el-menu router>
<el-submenu index="1" v-for="(item,index) in this.$router.options.routes" v-if="!item.hidden" :key="index">
<template slot="title">
<i class="el-icon-location">i>
<span>{{item.name}}span>
template>
<el-menu-item :index="child.path" v-for="(child,indexj) in item.children " :key="indexj">
{{child.name}}
el-menu-item>
el-submenu>
el-menu>
el-aside>
<el-main>
<router-view>router-view>
el-main>
el-container>
el-container>
div>
template>
<script>
export default {
name: "Home",
data() {
return {
// 从 sessionStorage 读取数据
user: JSON.parse(window.sessionStorage.getItem("user"))
}
},
methods: {
commandHandler(cmd) {
if (cmd == 'logout') {
this.$confirm('此操作将注销登录, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.getRequest("/logout");
// 清空登录信息
window.sessionStorage.removeItem("user")
// 回到登录页
this.$router.replace("/")
}).catch(() => {
this.$message({
type: 'info',
message: '已取消操作'
});
});
}
}
}
}
script>
<style>
.homeHeader {
background-color: #00aeff;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0px 15px;
box-sizing: border-box;
}
.homeHeader .title {
font-size: 30px;
font-family: 华文行楷, serif;
color: white;
}
.homeHeader .userInfo {
cursor: pointer;
}
.el-dropdown-link img {
width: 48px;
height: 48px;
border-radius: 24px;
margin-left: 8px;
}
.el-dropdown-link {
display: flex;
align-items: center;
}
style>
从数据库加载菜单项,不写死在 router
SELECT * FROM menu m1,menu m2,hr_role hrr,menu_role mr
where
m1.id = m2.parentId and hrr.hrid=10
and hrr.rid=mr.rid and mr.mid=m2.id and
m2.enabled=true order by m1.id,m2.id;
SELECT distinct m1.*,m2.id as id2,m2.component as component2,
m2.enabled as enabled2,
m2.iconCls as iconCls2,m2.keepAlive as keepAlive2,m2.name as name2,
m2.parentId as parentId2,m2.requireAuth as requireAuth2,m2.path as path2
FROM menu m1,menu m2,hr_role hrr,menu_role mr
where
m1.id = m2.parentId and hrr.hrid=10
and hrr.rid=mr.rid and mr.mid=m2.id and
m2.enabled=true order by m1.id,m2.id;
package com.tzb.service;
import com.tzb.mapper.MenuMapper;
import com.tzb.model.Hr;
import com.tzb.model.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MenuService {
@Autowired
MenuMapper menuMapper;
public List<Menu> getMenusByHrId() {
return menuMapper.getMenusByHrId(
((Hr)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
}
}
package com.tzb.controller;
import com.tzb.model.Menu;
import com.tzb.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/system/config")
public class SystemConfigController {
@Autowired
MenuService menuService;
@GetMapping("/menu")
public List<Menu> getMenusByHrId(){
return menuService.getMenusByHrId();
}
}