很多用户登录后根据用户的标识查询该用户角色所对应的菜单
- @Override
- public UserRespVo login(UserVo user) {
- if(null != user){//如果传入的用户不为空
- String userId = user.getUserid();
- String password = user.getPassword();
- //根据用户id查询用户
- UserPo userPo = userPoMapper.selectByPrimaryKey(userId);
- if(null != userPo){
- //如果存在该用户,对比密码看是否匹配
- String resultpassword = userPo.getPassword();
- if(resultpassword.equals(password)){
- //如果密码匹配,查询用户的菜单
- List<MenuPo> resultUserPoMenuList = userPoMapper.selectUserMenu(userPo.getUserid());
- List<MenuVo> resultUserVoMenuList = new ArrayList<MenuVo>();
- //将查询的结果po转为vo
- for (MenuPo menuPo : resultUserPoMenuList) {
- MenuVo menuVo = new MenuVo();
- BeanUtils.copyProperties(menuPo, menuVo);
- resultUserVoMenuList.add(menuVo);
- }
- //根据一级菜单id查询所有的菜单
- List<MenuVo> userMenuVos = new ArrayList<MenuVo>();
- for (MenuVo menuVo : resultUserVoMenuList) {
- //这里需要的是一级菜单的id,对于没有父菜单id的就是一级菜单
- if(StringUtils.isBlank(menuVo.getMenuparent())){
- List<MenuVo> iterateMenus = iterateMenus(resultUserVoMenuList, menuVo.getMenuid());
- menuVo.setMenuVo(iterateMenus);
- userMenuVos.add(menuVo);
- }
- }
- UserRespVo userRespVo = new UserRespVo();
- userRespVo.setErrMsg("登录成功");
- userRespVo.setStatusCode("1");
- userRespVo.setMenus(userMenuVos);
- userRespVo.setUserId(userPo.getUserid());
- userRespVo.setUserName(userPo.getUsername());
- logger.info(userPo.getUserid()+":登录成功");
- return userRespVo;
- }else{
- //如果密码不匹配
- UserRespVo userRespVo = new UserRespVo();
- userRespVo.setErrMsg("密码错误");
- userRespVo.setStatusCode("0");
- logger.info(userPo.getUserid()+":密码错误,登录失败");
- return userRespVo;
- }
- }else{
- UserRespVo userRespVo = new UserRespVo();
- userRespVo.setErrMsg("不存在该用户");
- userRespVo.setStatusCode("0");
- logger.info("不存在该用户,登录失败");
- return userRespVo;
- }
- }
- UserRespVo userRespVo = new UserRespVo();
- userRespVo.setErrMsg("用户名和密码不能为空");
- userRespVo.setStatusCode("0");
- logger.info("用户名和密码不能为空");
- return userRespVo;
- }
- //多级菜单查询方法
- public List<MenuVo> iterateMenus(List<MenuVo> menuVoList,String pid){
- List<MenuVo> result = new ArrayList<MenuVo>();
- for (MenuVo menuVo : menuVoList) {
- String menuid = menuVo.getMenuid();//获取菜单的id
- String parentid = menuVo.getMenuparent();//获取菜单的父id
- if(StringUtils.isNotBlank(parentid)){
- if(parentid.equals(pid)){
- List<MenuVo> iterateMenu = iterateMenus(menuVoList,menuid);
- menuVo.setMenuVo(iterateMenu);
- result.add(menuVo);
- }
- }
- }
- return result;
- }