实现过程
1 角色列表页和信息页面布局
2 功能实现分析
加载所有角色列表 新增 --角色信息页面 权限分配--入口--角色菜单设置页面
行操作 修改--角色修改页面
删除--删除角色信息--同时删除与角色相关的角色菜单关系表、用户角色关系表
角色信息功能:
判断存在性(查询操作)
add----清空 edit---加载角色信息(查询,根据角色Id)
提交 --add 执行insert --edit执行update
权限分配:
主页面菜单树--权限分配--权限分配页面
角色管理页面:权限分配按钮
权限分配链接列---某个确定的角色 角色下拉框不可用状态
3 DAL层方法定义实现
获取所有角色列表
删除角色信息(连同关系数据一起删除)
根据角色编号获取角色信息
判断角色名称是否已经存在
添加角色信息
修改角色信息
UI--BLL-DAL三层架构
4 角色权限分配页面流程分析
页面加载过程:
加载角色下拉框
加载菜单树
加载角色已经设置的菜单关系(菜单树中节点勾选)
角色列表
效果图
代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Xwy.WindowsFormsApp.DAL; using Xwy.WindowsFormsApp.FModels; using Xwy.WindowsFormsApp.Models; namespace Xwy.WindowsFormsApp.sm { public partial class FrmRoleList : Form { public FrmRoleList() { InitializeComponent(); } RoleDAL roleDAL = new RoleDAL(); private void btnAdd_Click(object sender, EventArgs e) { ShowRoleInfoPage(0); } private void btnAssign_Click(object sender, EventArgs e) { } private void ShowRoleInfoPage(int roleId) { FrmRoleInfo fRole = new FrmRoleInfo(); fRole.Tag = new FInfoModel { FId=roleId, ReloadList= LoadAllRoles }; fRole.MdiParent=this.MdiParent; fRole.Show(); } private void ShowRightPage(int roleId) { FrmRight fRight; if (!FUtility.CheckForm("FrmRight")) { fRight = new FrmRight(); } else { fRight=(FrmRight)FUtility.GetOpenForm("FrmRight"); } fRight.Tag = new FInfoModel { FId = roleId, ReloadList = LoadAllRoles }; fRight.MdiParent = this.MdiParent; if (!fRight.Visible) fRight.Show(); else fRight.Activate(); } private void LoadAllRoles() { List<RoleInfoModel> list = roleDAL.GetAllRoles(); dgvRoles.AutoGenerateColumns = false; dgvRoles.DataSource = list; } private void dgvRoles_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0) { var curCell = dgvRoles.Rows[e.RowIndex].Cells[e.ColumnIndex]; string val = curCell.FormattedValue.ToString(); RoleInfoModel roleInfo = dgvRoles.Rows[e.RowIndex].DataBoundItem as RoleInfoModel; switch (val) { case "修改": ShowRoleInfoPage(roleInfo.RoleId); break; case "分配": ShowRightPage(roleInfo.RoleId); break; case "删除": //提示 if (MsgBoxHelper.MsgBoxConfirm("删除菜单", "您确定要删除该条角色数据吗?删除角色数据会同角色菜单关系数据一并删除?") == DialogResult.Yes) { //调用删除方法 bool blDel = roleDAL.DeleteRole(roleInfo.RoleId); if (blDel) { MsgBoxHelper.MsgBoxShow("成功提示", $"角色:{roleInfo.RoleName} 信息删除成功!"); LoadAllRoles(); } else { MsgBoxHelper.MsgErrorShow($"角色:{roleInfo.RoleName} 信息删除失败!"); } } break; } } } private void FrmRoleList_Load(object sender, EventArgs e) { LoadAllRoles(); } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Xwy.WindowsFormsApp.DAL; using Xwy.WindowsFormsApp.FModels; using Xwy.WindowsFormsApp.Models; namespace Xwy.WindowsFormsApp.sm { public partial class FrmRoleInfo : Form { public FrmRoleInfo() { InitializeComponent(); } FInfoModel fModel; RoleDAL roleDAL = new RoleDAL(); private string oName; /// <summary> /// 新增或修改提交 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOk_Click(object sender, EventArgs e) { //1接收页面输入 string roleName = txtRoleName.Text.Trim(); string remark = txtRemark.Text.Trim(); //判断角色名称是否为空 if(string.IsNullOrEmpty(roleName)) { MsgBoxHelper.MsgErrorShow("角色名称不能为空!"); txtRoleName.Focus(); return; } //判断存在性 if (fModel.FId == 0 || oName != "" && oName != roleName) { if (roleDAL.ExistRoleName(roleName)) { MsgBoxHelper.MsgErrorShow("角色名称已存在!"); txtRoleName.Focus(); return; } } //信息封装 RoleInfoModel roleInfo = new RoleInfoModel { RoleName=roleName, Remark=remark }; //信息提交 到底是新增还是修改 bool bl = false; if (fModel.FId == 0) { bl=roleDAL.AddRoleInfo(roleInfo); } else if(fModel.FId>0) { roleInfo.RoleId = fModel.FId; bl = roleDAL.UpdateRoleInfo(roleInfo); } string actMsg = fModel.FId == 0 ? "新增" : "修改"; if (bl) { MsgBoxHelper.MsgBoxShow($"{actMsg}角色", $"角色:{ roleName} 信息{actMsg}成功!"); fModel.ReloadList?.Invoke(); } else { MsgBoxHelper.MsgErrorShow($"角色:{ roleName} 信息{ actMsg}失败!"); return; } } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } private void FrmRoleInfo_Load(object sender, EventArgs e) { if (this.Tag != null) { fModel = this.Tag as FInfoModel; if (fModel != null) { if (fModel.FId == 0) { txtRoleName.Clear(); txtRemark.Clear(); this.Text += "--新增"; }else if (fModel.FId > 0) { RoleInfoModel roleInfo = roleDAL.GetRoleById(fModel.FId); if (roleInfo != null) { oName = roleInfo.RoleName; txtRoleName.Text = roleInfo.RoleName; txtRemark.Text = roleInfo.Remark; this.Text += "--修改"; } } } } } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Xwy.WindowsFormsApp.Common; using Xwy.WindowsFormsApp.DAL; using Xwy.WindowsFormsApp.FModels; using Xwy.WindowsFormsApp.Models; namespace Xwy.WindowsFormsApp.sm { public partial class FrmRight : Form { public FrmRight() { InitializeComponent(); } FInfoModel fModel = new FInfoModel(); RoleDAL roleDAL = new RoleDAL(); MenuDAL menuDAL = new MenuDAL(); bool blFlag = false; private void FrmRight_Load(object sender, EventArgs e) { if (this.Tag != null) { fModel= this.Tag as FInfoModel; } //加载角色下拉框 LoadCboRoles(); blFlag = true; //加载菜单树 LoadTvMenus(); //加载角色已经设置的菜单关系(菜单树中节点勾选) LoadRightSet(); cbAll.Checked = false; } private void LoadRightSet() { if (fModel.FId > 0) { cboRoles.SelectedValue = fModel.FId; cboRoles.Enabled = false; } } private void LoadTvMenus() { //获取菜单数据:编号 名称 父级编号 //DataTable dt= menuDAL.GetAllTvMenus(); //DataRow dr = dt.NewRow(); //dr["RoleId"] List<MenuInfoModel> menus = menuDAL.GetUserMenuList(""); tvMenus.Nodes.Clear(); tvMenus.CheckBoxes = true; TreeNode rootNode = new TreeNode("系统菜单"); rootNode.Name = "0"; tvMenus.Nodes.Add(rootNode); CreateTreeNode(menus, rootNode, 0); tvMenus.ExpandAll(); } private void CreateTreeNode(List<MenuInfoModel> list,TreeNode pNode,int parentId) { var childList = list.Where(m => m.ParentId == parentId); foreach(MenuInfoModel menu in childList) { TreeNode tn = new TreeNode(menu.MenuName.ToString()); tn.Name = menu.MenuId.ToString(); if (pNode != null) pNode.Nodes.Add(tn); CreateTreeNode(list, tn, menu.MenuId); } } private void LoadCboRoles() { DataTable dt=roleDAL.GetCboRoles(); DataRow dr= dt.NewRow(); dr["RoleId"] = "0"; dr["RoleName"] = "请选择"; dt.Rows.InsertAt(dr, 0); cboRoles.DataSource = dt; cboRoles.DisplayMember = "RoleName"; cboRoles.ValueMember = "RoleId"; cboRoles.SelectedIndex = 0; } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } private void cboRoles_SelectedIndexChanged(object sender, EventArgs e) { if (blFlag) { CheckChildNodes(tvMenus.Nodes[0], false); List<MenuInfoModel> list = menuDAL.GetUserMenuList(cboRoles.SelectedValue.ToString()); if(list.Count>0) { List<int> menuIds = list.Select(m => m.MenuId).ToList(); CheckTvNodes(tvMenus.Nodes[0].Nodes, menuIds); } } } private void CheckTvNodes(TreeNodeCollection tnc,List<int> menuIds) { foreach(TreeNode tn in tnc) { if (menuIds.Contains(tn.Name.GetInt())) { tn.Checked = true; } CheckTvNodes(tn.Nodes, menuIds); } } private void CheckChildNodes(TreeNode curTn, bool check) { foreach (TreeNode tn in curTn.Nodes) { tn.Checked = check; CheckChildNodes(tn, check); } } private void cbAll_CheckedChanged(object sender, EventArgs e) { CheckChildNodes(tvMenus.Nodes[0], cbAll.Checked); } private void tvMenus_AfterCheck(object sender, TreeViewEventArgs e) { if (e.Action == TreeViewAction.ByKeyboard || e.Action == TreeViewAction.ByMouse) { SetChildNodesCheckState(e.Node); SetParentNodesCheckState(e.Node); } } private void SetChildNodesCheckState(TreeNode tn) { foreach(TreeNode child in tn.Nodes) { child.Checked = tn.Checked; SetChildNodesCheckState(child); } } private void SetParentNodesCheckState (TreeNode tn) { TreeNode pNode = tn.Parent; if (pNode != null) { bool bl = false; foreach (TreeNode item in pNode.Nodes) { if(item.Checked) { bl = true; break; } } pNode.Checked = bl; SetParentNodesCheckState(pNode); } } private void btnSubmit_Click(object sender, EventArgs e) { int roleId = cboRoles.SelectedValue.ToString().GetInt(); if (roleId > 0) { List<int> menuIds = new List<int>(); menuIds = GetCheckedMenuIds(tvMenus.Nodes[0].Nodes,menuIds); //保存权限设置 bool bl = roleDAL.SaveRights(roleId,menuIds); if (bl) { MsgBoxHelper.MsgBoxShow("权限设置","权限菜单设置成功!"); } else { MsgBoxHelper.MsgErrorShow("权限设置失败!"); } } } private List<int> GetCheckedMenuIds(TreeNodeCollection nodes, List<int> menuIds) { foreach(TreeNode tn in nodes) { if (tn.Checked) { menuIds.Add(tn.Name.ToString().GetInt()); } GetCheckedMenuIds(tn.Nodes,menuIds); } return menuIds; } } }