package com.java1234.repository;
import com.java1234.entity.Menu;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
/**
* 菜单= Repository接口
*/
public interface MenuRepository extends JpaRepository< Menu,Integer> {
/**
* 根据父节点以及用户角色id查询子节点
* @param parentId
* @param roleId
* @return
*/
@Query( value = "SELECT * FROM t_menu WHERE p_id=?1 AND id IN (SELECT menu_id FROM t_role_menu WHERE role_id=?2)" ,nativeQuery = true )
public List< Menu> findByParentIdAndRoleId( int parentId,int roleId) ;
}
package com.java1234.service;
import com.java1234.entity.Menu;
import java.util.List;
/**
* 权限菜单Service接口
*/
public interface MenuService {
/**
* 根据父节点以及用户角色id查询子节点
* @param parentId
* @param roleId
* @return
*/
public List< Menu> findByParentIdAndRoleId( int parentId, int roleId) ;
}
package com.java1234.service.impl;
import com.java1234.entity.Menu;
import com.java1234.repository.MenuRepository;
import com.java1234.service.MenuService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* 权限菜单Service实现类
*/
@Service( "menuService" )
public class MenuServiceImpl implements MenuService {
@Resource
private MenuRepository menuRepository;
@Override
public List< Menu> findByParentIdAndRoleId( int parentId, int roleId) {
return menuRepository.findByParentIdAndRoleId( parentId,roleId) ;
}
}
package com.java1234.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.java1234.entity.Menu;
import com.java1234.service.MenuService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.java1234.entity.Role;
import com.java1234.entity.User;
import com.java1234.service.RoleService;
import com.java1234.service.UserService;
import com.java1234.util.StringUtil;
/**
* 用户Controller
* @author Administrator
*
*/
@Controller
@RequestMapping( "/user" )
public class UserController {
@Resource
private UserService userService;
@Resource
private RoleService roleService;
@Resource
private MenuService menuService;
/**
* 用户登录判断
* @param imageCode
* @param user
* @param bindingResult
* @param session
* @return
*/
@ResponseBody
@RequestMapping( "/login" )
public Map< String,Object> login( String imageCode,@Valid User user,BindingResult bindingResult,HttpSession session) {
Map< String,Object> map = new HashMap< String,Object> ( ) ;
if( StringUtil.isEmpty( imageCode)) {
map.put( "success" , false ) ;
map.put( "errorInfo" , "请输入验证码!" ) ;
return map;
}
if( ! session.getAttribute( "checkcode" ) .equals( imageCode)) {
map.put( "success" , false ) ;
map.put( "errorInfo" , "验证码输入错误!" ) ;
return map;
}
if( bindingResult.hasErrors( )) {
map.put( "success" , false ) ;
map.put( "errorInfo" , bindingResult.getFieldError( ) .getDefaultMessage( )) ;
return map;
}
Subject subject = SecurityUtils.getSubject( ) ;
UsernamePasswordToken token = new UsernamePasswordToken( user.getUserName( ) , user.getPassword( )) ;
try{
subject.login( token) ;
String userName = ( String) SecurityUtils.getSubject( ) .getPrincipal( ) ;
User currentUser = userService.findByUserName( userName) ;
session.setAttribute( "currentUser" , currentUser) ;
List< Role> roleList = roleService.findByUserId( currentUser.getId( )) ;
map.put( "roleList" , roleList) ;
map.put( "roleSize" , roleList.size( )) ;
map.put( "success" , true ) ;
return map;
} catch( Exception e) {
e.printStackTrace( ) ;
map.put( "success" , false ) ;
map.put( "errorInfo" , "用户名或者密码错误!" ) ;
return map;
}
}
/**
* 保存角色信息
* @param roleId
* @param session
* @return
* @throws Exception
*/
@ResponseBody
@RequestMapping( "/saveRole" )
public Map< String,Object> saveRole( Integer roleId,HttpSession session) throws Exception{
Map< String,Object> map = new HashMap< String,Object> ( ) ;
Role currentRole = roleService.findById( roleId) ;
session.setAttribute( "currentRole" , currentRole) ;
map.put( "success" , true ) ;
return map;
}
/**
* 加载当前用户信息
* @param session
* @return
* @throws Exception
*/
@ResponseBody
@GetMapping( "/loadUserInfo" )
public String loadUserInfo( HttpSession session) throws Exception{
User currentUser = ( User) session.getAttribute( "currentUser" ) ;
Role currentRole = ( Role) session.getAttribute( "currentRole" ) ;
return "欢迎您:" +currentUser.getTrueName( ) +" [ " +currentRole.getName( ) +" ]" ;
}
/**
* 加载权限菜单
* @param session
* @param parentId
* @return
* @throws Exception
*/
@ResponseBody
@PostMapping( "/loadMenuInfo" )
public String loadMenuInfo( HttpSession session,Integer parentId) throws Exception{
Role currentRole = ( Role) session.getAttribute( "currentRole" ) ;
return getAllMenuByParentId( parentId,currentRole.getId( )) .toString( ) ;
}
/**
* 获取所有菜单信息
* @param parentId
* @param roleId
* @return
*/
public JsonArray getAllMenuByParentId( Integer parentId,Integer roleId) {
JsonArray jsonArray = this.getMenuByParentId( parentId, roleId) ;
for( int i = 0 ; i< jsonArray.size( ) ; i++) {
JsonObject jsonObject = ( JsonObject) jsonArray.get( i) ;
if( "open" .equals( jsonObject.get( "state" ) .getAsString( )) ) {
continue ;
} else{
jsonObject.add( "children" , getAllMenuByParentId( jsonObject.get( "id" ) .getAsInt( ) , roleId)) ;
}
}
return jsonArray;
}
/**
* 根据父节点和用户角色Id查询菜单
* @param parentId
* @param roleId
* @return
*/
public JsonArray getMenuByParentId( Integer parentId,Integer roleId) {
List< Menu> menuList = menuService.findByParentIdAndRoleId( parentId, roleId) ;
JsonArray jsonArray = new JsonArray( ) ;
for( Menu menu:menuList) {
JsonObject jsonObject = new JsonObject( ) ;
jsonObject.addProperty( "id" , menu.getId( )) ; // 节点Id
jsonObject.addProperty( "text" , menu.getName( )) ; // 节点名称
if( menu.getState( ) == 1 ) {
jsonObject.addProperty( "state" , "closed" ) ; // 根节点
} else{
jsonObject.addProperty( "state" , "open" ) ; // 叶子节点
}
jsonObject.addProperty( "iconCls" , menu.getIcon( )) ; // 节点图标
JsonObject attributeObject = new JsonObject( ) ; // 扩展属性
attributeObject.addProperty( "url" , menu.getUrl( )) ; // 菜单请求地址
jsonObject.add( "attributes" , attributeObject) ;
jsonArray.add( jsonObject) ;
}
return jsonArray;
}
}
$( "#tree") .tree( {
lines:true,
url:'/user/loadMenuInfo?parentId=-1' ,
onLoadSuccess:function ( ) {
$( "#tree") .tree( "expandAll" ) ;
}
} ) ;
< ul id = "tree" class = "easyui-tree" style = "padding: 10px" > < /ul>
< ! DOCTYPE html>
< html>
< head>
< meta charset = "UTF-8" >
< title> 后台管理-进销存管理系统< /title>
< link rel = "stylesheet" type = "text/css" href = "/static/jquery-easyui-1.3.3/themes/default/easyui.css" > < /link>
< link rel = "stylesheet" type = "text/css" href = "/static/jquery-easyui-1.3.3/themes/icon.css" > < /link>
< style type = "text/css" >
.clock {
float:right;
width: 300px;
height: 30px;
padding-left: 20px;
color: rgb( 0 , 76 , 126 ) ;
background: url( /static/images/clock.gif) no-repeat;
font-size: 14px;
}
.userInfo{
float:left;
padding-left: 20px;
padding-top: 30px;
}
< /style>
< script type = "text/javascript" src = "/static/jquery-easyui-1.3.3/jquery.min.js" > < /script>
< script type = "text/javascript" src = "/static/jquery-easyui-1.3.3/jquery.easyui.min.js" > < /script>
< script type = "text/javascript" src = "/static/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js" > < /script>
< script type = "text/javascript" >
function showTime ( ) {
var date = new Date( ) ;
this.year = date.getFullYear( ) ;
this.month = date.getMonth( ) + 1 ;
this.date = date.getDate( ) ;
this.day = new Array( "星期日" , "星期一" , "星期二" , "星期三" , "星期四" , "星期五" , "星期六" ) [ date.getDay( ) ] ;
this.hour = date.getHours( ) < 10 ? "0" + date.getHours( ) : date.getHours( ) ;
this.minute = date.getMinutes( ) < 10 ? "0" + date.getMinutes( ) : date.getMinutes( ) ;
this.second = date.getSeconds( ) < 10 ? "0" + date.getSeconds( ) : date.getSeconds( ) ;
$( "#clock") .text( "现在是:" + this.year + "年" + this.month + "月" + this.date + "日 " + this.hour + ":" + this.minute + ":" + this.second + " " + this.day) ;
}
$( document) .ready( function ( ) {
window.setInterval( "showTime()" ,1000) ;
$( "#userInfo") .load( "/user/loadUserInfo" ) ; // 加载用户信息
$( "#tree") .tree( {
lines:true,
url:'/user/loadMenuInfo?parentId=-1' ,
onLoadSuccess:function ( ) {
$( "#tree") .tree( "expandAll" ) ;
}
} ) ;
} ) ;
< /script>
< /head>
< body class = "easyui-layout" >
< div region = "north" style = "height: 72px;" >
< table width = "100%" height = "100%" border = "0" cellspacing = "0" cellpadding = "0" >
< tr>
< td width = "381px" style = "background:url(/static/images/top_left.jpg)" >
< /td>
< td style = "background:url(/static/images/top_center.jpg)" >
< div id = "userInfo" class = "userInfo" > < /div>
< /td>
< td valign = "bottom" width = "544px" style = "background:url(/static/images/top_right.jpg)" >
< div id = "clock" class = "clock" > < /div>
< /td>
< /tr>
< /table>
< /div>
< div region = "center" >
< /div>
< div region = "west" style = "width: 200px" title = "导航菜单" split = "true" iconCls = "icon-navigation" >
< ul id = "tree" class = "easyui-tree" style = "padding: 10px" > < /ul>
< /div>
< div region = "south" style = "height: 30px;padding: 5px" align = "center" >
Copyright © 2012 -2017 南通小锋网络科技有限公司 版权所有
< /div>
< /body>
< /html>