/** * 将菜单构建成树html * * @param userId * @param roleId * @param isAdmin * @return */ @Transactional(readOnly = true) public String getUserMenuHtml(String userId, String roleId, String isAdmin) { List<Menu> menus = getUserMenuObject(userId, roleId, isAdmin); StringBuffer sb = new StringBuffer(); sb.append("<div id='ddtopmenubar' class='mattblackmenu'><ul>"); // 一级菜单 for (Menu m1 : menus) { sb.append("<li><a rel='" + m1.getId() + "'>" + m1.getMenuName() + "</a></li>"); } sb.append( "<li class='to-right list-item bold'><a href='javascript:ipe.fuc.logout()'>退出系统</a></li>"); sb.append( "<li class='to-right list-item bold'><a href='javascript:ipe.fuc.upPwd()'>修改密码</a></li>"); sb.append( "<li class='to-right list-item bold'><a href='javascript:ipe.fuc.changeRole()'>切换角色</a></li>"); sb.append("</ul></div>"); // 多级菜单 for (Menu m1 : menus) { if (m1.getRows() != null && !m1.getRows().isEmpty()) { sb.append("<ul id='" + m1.getId() + "' class='ddsubmenustyle'>"); eachHtmlMenu(m1.getRows(), sb); sb.append("</ul>"); } } return sb.toString(); }
/** * 将菜单组装成树 * * @param menus * @param root */ void eachMenu(List<Menu> menus, Menu root) { for (Menu m1 : menus) { if (m1.getParent() != null && root.getId().equals(m1.getParent().getId())) { if (root.getRows() == null) { root.setRows(new ArrayList<Menu>()); } root.getRows().add(m1); root.setLeaf(false); eachMenu(menus, m1); } } }
void eachHtmlMenu(List<Menu> menus, StringBuffer sbt) { for (Menu m1 : menus) { if (m1.getRows() != null && !m1.getRows().isEmpty()) { sbt.append("<li>"); sbt.append("<a>" + m1.getMenuName() + "</a><ul>"); eachHtmlMenu(m1.getRows(), sbt); sbt.append("</ul></li>"); } else { String result = JSON.toJSONString( m1, PROPERTYFILTER, SerializerFeature.UseSingleQuotes, SerializerFeature.WriteNullListAsEmpty); sbt.append( "<li><a onclick=ipe.fuc.openMenu2(" + result + ")>" + m1.getMenuName() + "</a></li>"); } } }
/** * 用户所具有的菜单,关联权限 * * @param userId * @return */ @Transactional(readOnly = true) public List<Menu> getUserMenuObject(String userId, String roleId, String isAdmin) { List<Menu> menus = null; if (!"1".equals(isAdmin)) { // 非管理员 String sql = "select * from ( select t01.* from t_cor_menu t01 join (\n" + "SELECT t4.resource_id from t_cor_user t1 join \n" + "t_cor_user_role t2 on t1.id_=t2.user_id\n" + "join t_cor_role t3 on t2.role_id=t3.id_\n" + "join t_cor_authority t4 on t4.role_id=t3.id_ where t1.id_=? and t3.enabled_='1' and t1.enabled_='1') t02 \n" + "on t01.resource_id=t02.resource_id) t order by t.sno_ asc"; menus = menuDao.listBySql(sql, userId); } else { menus = menuDao.listAll(); } if (menus == null || menus.isEmpty()) { return null; } Menu root = getRootMenu(menus); eachMenu(menus, root); List<Menu> trees = root.getRows(); CollectionSort.sortList(trees, "sno", true); return trees; }