一、查询当前登录用户权限菜单接口
在【SysRoleDao.java】中添加根据用户id查询权限信息dao接口
List<SysRolePermission>selectRolePermissionByUserId(@Param("userId") int userId);
在【SysRoleMapper.xml】中添加根据用户id查询权限信息sqlMapper
<select id="selectRolePermissionByUserId" parameterType="int" resultType="com.dc.pojo.SysRolePermission">
select tsrp.code,tsrp.permission_value,tsrp.permission_id from t_sys_role_permission tsrp
join
t_sys_user_role tsur on tsur.fk_role_id=tsrp.fk_role_id
where tsur.fk_user_id=#{userId}
</select>
在【SysRoleService.java】中添加根据用户id查询用户权限信息接口
Map<String, String> selectMergeRolePermissionByUserId(int userId)throws Exception;
在【SysRoleServiceImpl.java】中添加根据用户id查询用户权限信息接口实现方法
/**
* @return java.util.List<com.dc.pojo.SysRolePermission>
* @title:<h3> 根据用户id查询并合并权限值 <h3>
* @author: Enzo
* @date: 2018-11-20 10:01
* @params [userId]
**/
public Map<String, String> selectMergeRolePermissionByUserId(int userId) throws Exception {
Map<String, String> map = new HashMap<String, String>();
List<SysRolePermission> list = roleDao.selectRolePermissionByUserId(userId);
for (SysRolePermission permission : list) {
if (map.get(permission.getCode()) == null) {//如果map中不存在编号
map.put(permission.getCode(), permission.getPermission_value());
} else {//如果已存在权限编号,则进行权限合并
StringBuilder mapValue = new StringBuilder(map.get(permission.getCode()));
StringBuilder perValue = new StringBuilder(permission.getPermission_value());
if (mapValue.length() >= perValue.length()) {
//100111 1101
for (int i = 0; i < perValue.length(); i++) {//循环短的权限值
if (mapValue.charAt(i) == '0' && perValue.charAt(i) == '1') {
mapValue.replace(i,i+1,"1");
}
}
map.put(permission.getCode(),mapValue.toString());
}else{//1100 1111101
for (int i = 0; i < mapValue.length(); i++) {//循环短的权限值
if ( perValue.charAt(i) == '0'&&mapValue.charAt(i)=='1') {
perValue.replace(i,i+1,"1");
}
}
map.put(permission.getCode(),perValue.toString());
}
}
}
return map;
}
修改【LoginController.java】中的登录方法,登录成功后,调用【SysRoleService.java】中的根据用户id查询权限信息接口,将权限信息存入session中
@Autowired
SysRoleService roleService;
public BaseModel login(SysLoginLog loginLog, BaseModel baseModel) throws Exception {
...
session.setAttribute(BaseContants.LOGIN_USER, baseModel.getData());
session.setAttribute(BaseContants.LOGIN_PERMISSION, roleService.selectMergeRolePermissionByUserId(loginLog.getUser_id()));
...
}
新建【SysMenuService.java】,添加查询当前用户菜单接口
package com.dc.service;
import com.dc.base.pojo.BaseModel;
import com.dc.pojo.SysMenu;
import java.util.Map;
public interface SysMenuService {
void findMyMenu(Map<String, String> map, BaseModel baseModel) throws Exception;
}
新建菜单业务接口实现类【SysMenuServiceImpl.java】,添加查询当前用户菜单接口实现方法
package com.dc.service.impl;
import com.dc.base.contants.GlobalVar;
import com.dc.base.pojo.BaseModel;
import com.dc.mapper.SysMenuDao;
import com.dc.pojo.SysMenu;
import com.dc.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Enzo
* @Description TODO
* @date 2018-11-14 13:56
*/
@Service
public class SysMenuServiceImpl implements SysMenuService {
@Autowired
SysMenuDao menuDao;
/**
* @return void
* @title:<h3> 查询我的权限菜单 <h3>
* @author: Enzo
* @date: 2018-11-20 11:14
* @params [map, baseModel]
**/
public void findMyMenu(Map<String, String> map, BaseModel baseModel) throws Exception {
Map<Integer, SysMenu> mapParentIdMenu = new HashMap<Integer, SysMenu>();//存储为添加到listMenu1中的父级菜单id
List<SysMenu> listMenu1 = new ArrayList<SysMenu>();//一级菜单列表
List<SysMenu> listMenu2 = new ArrayList<SysMenu>();//二级菜单列表
List<SysMenu> listMenu = menuDao.selectAllMenu();
for (SysMenu menu : listMenu) {//遍历权限
if (menu.getFk_parent_id() == 0) {
mapParentIdMenu.put(menu.getMenu_id(), menu);//将一级菜单信息保存在map中,方便获取上级
}
if (map.get(menu.getCode()) != null) {//如果有该模块的菜单权限
if (menu.getFk_parent_id() == 0) {//如果是一级菜单,并且未添加
listMenu1.add(menu);
mapParentIdMenu.remove(menu.getMenu_id());//标记该一级菜单已添加到listMenu1中
} else {
listMenu2.add(menu);
if (mapParentIdMenu.get(menu.getFk_parent_id()) != null) {//如果父级菜单未添加到listMeun1中
listMenu1.add(mapParentIdMenu.get(menu.getFk_parent_id()));
mapParentIdMenu.remove(menu.getFk_parent_id());
}
}
}
}
Map<String, List<SysMenu>> result = new HashMap<String, List<SysMenu>>();
result.put("menu_1", listMenu1);
result.put("menu_2", listMenu2);
baseModel.setData(result);
}
}
在【LoginController.java】中添加查询当前用户权限菜单控制层接口
/**
* @return com.dc.base.pojo.BaseModel
* @title:<h3> 查询当前用户权限菜单 <h3>
* @author: Enzo
* @date: 2018-11-20 11:37
* @params [baseModel]
**/
@ApiOperation(value = "查询当前用户权限菜单", httpMethod = "GET", response = SysMenu.class)
@RequestMapping("findMyMenu")
public BaseModel findMyMenu(BaseModel baseModel) throws Exception {
Map<String, String> map = (Map<String, String>) session.getAttribute(BaseContants.LOGIN_PERMISSION);
if (map == null) {
throw new BusinessException(ErrorMesgEnum.NO_LOGIN);
}
menuService.findMyMenu(map, baseModel);
baseModel.setMessage("查询权限菜单成功");
return baseModel;
}
二、查询当前登录用户信息接口
在【SysUserController.java】中添加查询当前登录用户信息接口
/**
* @return com.dc.base.pojo.BaseModel
* @title:<h3> 查询当前登录用户信息 <h3>
* @author: Enzo
* @date: 2018-11-20 9:41
* @params [baseModel]
**/
@ApiOperation(value = "查询当前登录用户信息", httpMethod = "GET")
@RequestMapping("findMyUserVo")
public BaseModel findMyUserVo(BaseModel baseModel) throws Exception {
userService.selectUserVoByPk(getSessionUser().getUser_id(), baseModel);
return baseModel;
}