新增角色修改

This commit is contained in:
RuoYi 2018-03-09 17:20:35 +08:00
parent 0b1793d57d
commit 5b2bb844c4
15 changed files with 316 additions and 68 deletions

View File

@ -59,7 +59,7 @@ insert into sys_user values('2', '8', 'ry', '若依', 'ry@163.com', '152
drop table if exists sys_role;
create table sys_role (
role_id int(10) not null auto_increment comment '角色ID',
role_name varchar(30) not null comment '角色名',
role_name varchar(30) not null comment '角色名',
role_key varchar(100) not null comment '角色权限字符串',
role_sort int(10) not null comment '显示顺序',
status int(1) default 0 comment '角色状态:0正常,1禁用',
@ -189,7 +189,6 @@ insert into sys_role_menu values ('1', '21');
insert into sys_role_menu values ('1', '22');
insert into sys_role_menu values ('1', '23');
insert into sys_role_menu values ('1', '24');
insert into sys_role_menu values ('1', '25');
-- ----------------------------
-- 7、操作日志记录
-- ----------------------------

View File

@ -142,55 +142,4 @@ public class TreeUtils
{
return getChildList(list, t).size() > 0 ? true : false;
}
/**
* 本地模拟数据测试
*/
public static void main(String[] args)
{
long start = System.currentTimeMillis();
List<Menu> permList = new ArrayList<Menu>();
Menu perm1 = new Menu();
perm1.setMenuId(100);
perm1.setMenuName("系统管理");
perm1.setParentId(0);
Menu perm2 = new Menu();
perm2.setMenuId(101);
perm2.setMenuName("用户管理");
perm2.setParentId(100);
Menu perm3 = new Menu();
perm3.setMenuId(102);
perm3.setMenuName("角色管理");
perm3.setParentId(100);
Menu perm4 = new Menu();
perm4.setMenuId(103);
perm4.setMenuName("菜单管理");
perm4.setParentId(100);
Menu perm5 = new Menu();
perm5.setMenuId(103);
perm5.setMenuName("日志管理");
perm5.setParentId(100);
permList.add(perm1);
permList.add(perm2);
permList.add(perm3);
permList.add(perm4);
permList.add(perm5);
List<Menu> ns = TreeUtils.getChildPerms(permList, 0);
for (Menu m : ns)
{
System.out.println(m.getMenuName());
System.out.println(m.getChildren());
}
long end = System.currentTimeMillis();
System.out.println("用时:" + (end - start) + "ms");
}
}

View File

@ -0,0 +1,49 @@
package com.ruoyi.project.system.menu.controller;
import java.util.List;
import java.util.Map;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.menu.service.IMenuService;
/**
* 角色信息
*
* @author ruoyi
*/
@Controller
@RequestMapping("/system/menu")
public class MenuController extends BaseController
{
private String prefix = "system/menu";
@Autowired
private IMenuService menuService;
@RequiresPermissions("system:menu:view")
@GetMapping()
public String user()
{
return prefix + "/menu";
}
/**
* 加载菜单列表树
*/
@GetMapping("/treeData/{roleId}")
@ResponseBody
public List<Map<String, Object>> treeData(@PathVariable("roleId") Long roleId)
{
List<Map<String, Object>> tree = menuService.selectMenuTree(roleId);
return tree;
}
}

View File

@ -27,6 +27,14 @@ public interface IMenuDao
*/
public List<String> selectPermsByUserId(Long userId);
/**
* 根据角色ID查询菜单
*
* @param roleId 角色ID
* @return 菜单列表
*/
public List<String> selectMenuTree(Long roleId);
/**
* 查询系统所有权限
*

View File

@ -45,16 +45,36 @@ public class MenuDaoImpl extends DynamicObjectBaseDao implements IMenuDao
@Override
public List<String> selectPermsByUserId(Long userId)
{
List<String> permsList = null;
List<String> menuList = null;
try
{
permsList = this.findForList("SystemMenuMapper.selectPermsByUserId", userId);
menuList = this.findForList("SystemMenuMapper.selectPermsByUserId", userId);
}
catch (Exception e)
{
e.printStackTrace();
}
return permsList;
return menuList;
}
/**
* 根据角色ID查询菜单
*
* @param roleId 角色ID
* @return 菜单列表
*/
public List<String> selectMenuTree(Long roleId)
{
List<String> menuList = null;
try
{
menuList = this.findForList("SystemMenuMapper.selectMenuTree", roleId);
}
catch (Exception e)
{
e.printStackTrace();
}
return menuList;
}
/**

View File

@ -13,11 +13,11 @@ import lombok.Data;
public class Menu
{
/** 菜单ID */
private Integer menuId;
private Long menuId;
/** 菜单名称 */
private String menuName;
/** 父菜单ID */
private Integer parentId;
private Long parentId;
/** 显示顺序 */
private String orderNum;
/** 菜单URL */

View File

@ -29,6 +29,14 @@ public interface IMenuService
*/
public Set<String> selectPermsByUserId(Long userId);
/**
* 根据角色ID查询菜单
*
* @param roleId 角色ID
* @return 菜单列表
*/
public List<Map<String, Object>> selectMenuTree(Long roleId);
/**
* 查询系统所有权限
*

View File

@ -1,13 +1,18 @@
package com.ruoyi.project.system.menu.service;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.TreeUtils;
import com.ruoyi.project.system.menu.dao.IMenuDao;
@ -60,6 +65,29 @@ public class MenuServiceImpl implements IMenuService
return permsSet;
}
/**
* 根据角色ID查询菜单
*
* @param roleId 角色ID
* @return 菜单列表
*/
public List<Map<String, Object>> selectMenuTree(Long roleId)
{
List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
List<String> roleMenuList = menuDao.selectMenuTree(roleId);
List<Menu> menuList = menuDao.selectPermsAll();
for (Menu menu : menuList)
{
Map<String, Object> deptMap = new HashMap<String, Object>();
deptMap.put("id", menu.getMenuId());
deptMap.put("pId", menu.getParentId());
deptMap.put("name", menu.getMenuName());
deptMap.put("checked", roleMenuList.contains(menu.getMenuId() + menu.getPerms()));
trees.add(deptMap);
}
return trees;
}
/**
* 查询系统所有权限
*
@ -80,4 +108,13 @@ public class MenuServiceImpl implements IMenuService
return section;
}
public static void main(String[] args)
{
List<Long> list = new ArrayList<Long>();
list.add(1L);
list.add(2L);
list.add(3L);
System.out.println(list.contains(Long.valueOf(1)));
}
}

View File

@ -3,13 +3,13 @@ package com.ruoyi.project.system.role.controller;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.JSON;
@ -31,7 +31,7 @@ public class RoleController extends BaseController
@Autowired
private IRoleService roleService;
@RequiresPermissions("system:role:view")
@GetMapping()
public String user()
@ -47,7 +47,19 @@ public class RoleController extends BaseController
TableDataInfo rows = roleService.pageInfoQuery(getPageUtilEntity());
return rows;
}
/**
* 修改角色
*/
@Log(title = "系统管理", action = "角色管理-修改角色")
@GetMapping("/edit/{roleId}")
public String edit(@PathVariable("roleId") Long roleId, Model model)
{
Role role = roleService.selectRoleById(roleId);
model.addAttribute("role", role);
return prefix + "/edit";
}
@Log(title = "系统管理", action = "角色管理-删除角色")
@RequestMapping("/remove/{roleId}")
@ResponseBody

View File

@ -7,10 +7,10 @@ spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://10.213.24.45:3306/ry?useUnicode=true&characterEncoding=utf8
#url: jdbc:mysql://localhost:3306/ry?useUnicode=true&characterEncoding=utf8
username: root
password: password
#url: jdbc:mysql://10.213.24.45:3306/ry?useUnicode=true&characterEncoding=utf8
url: jdbc:mysql://123.207.20.136:3306/ry?useUnicode=true&characterEncoding=utf8
username: ruoyi
password: 123456
# 初始化大小,最小,最大
initialSize: 1
minIdle: 3

View File

@ -37,6 +37,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
where ur.user_id = #{userId}
</select>
<select id="selectMenuTree" parameterType="Long" resultType="String">
select concat(m.menu_id, m.perms) as perms
from sys_menu m
left join sys_role_menu rm on m.menu_id = rm.menu_id
where rm.role_id = #{roleId}
order by m.parent_id, m.order_num
</select>
<select id="selectPermsAll" resultMap="MenuResult">
select * from sys_menu
</select>

View File

@ -0,0 +1,91 @@
$(document).ready(function(){
queryMenuTreeDaTa();
});
$("#form-role-edit").validate({
rules:{
roleName:{
required:true,
},
roleKey:{
required:true,
},
roleSort:{
required:true,
},
},
submitHandler:function(form){
update();
}
});
function update() {
var roleId = $("input[name='roleId']").val();
var roleName = $("input[name='roleName']").val();
var roleKey = $("input[name='roleKey']").val();
var roleSort = $("input[name='roleSort']").val();
var status = $("input[name='status']").is(':checked') == true ? 0 : 1;
$.ajax({
cache : true,
type : "POST",
url : "/system/role/save",
data : {
"roleId": roleId,
"roleName": roleName,
"roleKey": roleKey,
"roleSort": roleSort,
"status": status
},
async : false,
error : function(request) {
parent.layer.alert("系统错误");
},
success : function(data) {
if (data.code == 0) {
parent.layer.msg('修改成功',{icon:1,time:1000});
layer_close();
window.parent.location.reload();
} else {
parent.layer.alert(data.m , {icon: 2,title:"系统提示"});
}
}
});
}
function queryMenuTreeDaTa()
{
// 树结构初始化加载
var setting = {
check:{enable:true,nocheckInherit:true,chkboxType:{"Y":"ps","N":"ps"}},
view:{selectedMulti:false,nameIsHTML: true},
data:{simpleData:{enable:true},key:{title:"title"}},
callback:{
beforeClick: function (treeId, treeNode, clickFlag) {
var tree = $.fn.zTree.getZTreeObj(treeId);
tree.checkNode(treeNode, !treeNode.checked, true, true);
return false;
},
onCheck: function (event, treeId, treeNode){
var tid = treeNode.tId;
if(!treeNode.checked){
$(".checkall[value="+treeId+"]").each(function(){
if(this.checked){
$(this).removeAttr("checked").iCheck('update');
}
return false;
});
}
}
}
}, tree, loadTree = function(){
$.get("/system/menu/treeData/" + $("#roleId").val(), function(data) {
tree = $.fn.zTree.init($("#tree"), setting, data); //.expandAll(true);
// 展开第一级节点
var nodes = tree.getNodesByParam("level", 0);
for (var i = 0; i < nodes.length; i++) {
tree.expandNode(nodes[i], true, false, false);
}
}, null, null, "正在加载,请稍后...");
};loadTree();
}

View File

@ -40,14 +40,22 @@ $(function() {
title: '操作',
align: 'center',
formatter: function(value, row, index) {
var msg = '<a class="btn btn-warning btn-sm" href="#" title="删除" onclick="remove(\'' + row.roleId + '\')"><i class="fa fa-remove"></i></a> ';
return msg;
var actions = [];
actions.push('<a class="btn btn-primary btn-sm" href="#" title="编辑" mce_href="#" onclick="edit(\'' + row.roleId + '\')"><i class="fa fa-edit"></i></a> ');
actions.push('<a class="btn btn-warning btn-sm" href="#" title="删除" onclick="remove(\'' + row.roleId + '\')"><i class="fa fa-remove"></i></a>');
return actions.join('');
}
}];
var url = prefix + "/list";
initTable(columns, url);
});
/*角色管理-修改*/
function edit(roleId) {
var url = prefix + '/edit/' + roleId;
layer_show("修改角色", url, '800', '400');
}
// 单条删除
function remove(id) {
layer.confirm("确定要删除选中角色吗?",{icon: 3, title:'提示'},function(index){

View File

@ -52,7 +52,7 @@ function queryUserList() {
align: 'center',
formatter: function(value, row, index) {
var actions = [];
actions.push('<a class="btn btn-primary btn-sm" href="#" title="编辑" mce_href="#" onclick="edit(\'' + row.userId + '\')"><i class="fa fa-edit"></i></a>');
actions.push('<a class="btn btn-primary btn-sm" href="#" title="编辑" mce_href="#" onclick="edit(\'' + row.userId + '\')"><i class="fa fa-edit"></i></a> ');
actions.push('<a class="btn btn-warning btn-sm" href="#" title="删除" onclick="remove(\'' + row.userId + '\')"><i class="fa fa-remove"></i></a>');
return actions.join('');
}

View File

@ -0,0 +1,60 @@
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head th:include="include :: header"></head>
<link href="/ajax/libs/jquery-ztree/3.5/css/metro/zTreeStyle.css" rel="stylesheet">
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
<form class="form-horizontal m" id="form-role-edit">
<input id="roleId" name="roleId" type="hidden" th:value="${role.roleId}"/>
<div class="form-group">
<label class="col-sm-3 control-label ">角色名称:</label>
<div class="col-sm-8">
<input class="form-control" type="text" name="roleName" id="roleName" th:value="${role.roleName}"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">权限字符:</label>
<div class="col-sm-8">
<input class="form-control" type="text" name="roleKey" id="roleKey" th:value="${role.roleKey}">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">显示顺序:</label>
<div class="col-sm-8">
<input class="form-control" type="text" name="roleSort" id="roleSort" th:value="${role.roleSort}">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">状态:</label>
<div class="col-sm-8">
<div class="onoffswitch">
<input type="checkbox" th:checked="${role.status == 0 ? true : false}" class="onoffswitch-checkbox" id="status" name="status">
<label class="onoffswitch-label" for="status">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">菜单权限</label>
<div class="col-sm-8">
<div id="tree" class="ztree"></div>
</div>
</div>
<div class="form-group">
<div class="form-control-static col-sm-offset-9">
<button type="submit" class="btn btn-primary">提交</button>
<button th:onclick="'javascript:layer_close()'" class="btn btn-danger" type="button">关闭</button>
</div>
</div>
</form>
</div>
<div th:include="include::footer"></div>
<script src="/ajax/libs/jquery-ztree/3.5/js/jquery.ztree.all-3.5.js"></script>
<script type="text/javascript" src="/ruoyi/system/role/edit.js">
</script>
</body>
</html>