using System; using System.Collections.Generic; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.XtraEditors; using DevExpress.XtraGrid.Views.Base; using Gs.DevApp.Entity; using Gs.DevApp.Properties; using Gs.DevApp.ToolBox; using Gs.DevApp.UserControl; using Newtonsoft.Json; namespace Gs.DevApp.DevFrm.User { public partial class Role : XtraForm { private StringBuilder _actionLst = new StringBuilder(); private List _filterList = new List(); public Role() { InitializeComponent(); toolBarMenu1.btnAddClick += ToolBarMenu1_btnAddClick; toolBarMenu1.btnEdtClick += ToolBarMenu1_btnEdtClick; toolBarMenu1.btnSaveClick += ToolBarMenu1_btnSaveClick; toolBarMenu1.btnLoadClick += ToolBarMenu1_btnLoadClick; toolBarMenu1.btnDelClick += ToolBarMenu1_btnDelClick1; toolBarMenu1.btnEscClick += ToolBarMenu1_btnEscClick; toolBarMenu1.btnRoleClick += ToolBarMenu1_btnRoleClick; toolBarMenu1.btnQueryClick += ToolBarMenu1_btnQueryClick; gcMain.MouseDoubleClick += GcMain_MouseDoubleClick; gridView1.FocusedRowChanged += GridView1_FocusedRowChanged; getPageList(1, UtilityHelper.GetPageSize()); pageBar1.PagerEvent += PageBar1_PagerEvent; trv.AfterCheck += Trv_AfterCheck; gridView1.IndicatorWidth = 50; gridView1.CustomDrawRowIndicator += (s, e) => { if (e.Info.IsRowIndicator && e.RowHandle >= 0) e.Info.DisplayText = (e.RowHandle + 1).ToString(); }; gridView1.CustomDrawEmptyForeground += (s, e) => { var str = "暂未查找到匹配的数据!"; var f = new Font("微软雅黑", 16); var r = new Rectangle(gridView1.GridControl.Width / 2 - 100, e.Bounds.Top + 45, e.Bounds.Right - 5, e.Bounds.Height - 5); e.Graphics.DrawString(str, f, Brushes.Gray, r); }; } /// /// 换切换事件 /// /// /// private void GridView1_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e) { var rowGuid = ""; var myDataRow = gridView1.GetDataRow(e.FocusedRowHandle); if (myDataRow == null) return; rowGuid = myDataRow["guid"].ToString(); getTree(rowGuid); groupBox1.Text = "请勾选【" + myDataRow["roleName"] + "】的权限"; } /// /// 双击事件 /// /// /// private void GcMain_MouseDoubleClick(object sender, MouseEventArgs e) { var rowGuid = UtilityHelper.GetCurrentDoubleRow(gridView1, e, "guid"); if (!string.IsNullOrEmpty(rowGuid)) getModel(rowGuid, false, 999); } /// /// 分页事件 /// /// /// private void PageBar1_PagerEvent(int curPage, int pageSize) { getPageList(curPage, pageSize); } /// /// 查询事件 /// /// /// private void ToolBarMenu1_btnQueryClick(object sender, EventArgs e) { var frm = new ShowFilter(gridView1.Columns, _filterList); frm.UpdateParent += Frm_UpdateParent; frm.ShowDialog(); } /// /// 查询回调 /// /// /// private void Frm_UpdateParent(object sender, UpdateParentEventArgs e) { _filterList = e.FilterList; getPageList(1, pageBar1.RowsCount); } /// /// 取消事件 /// /// /// private void ToolBarMenu1_btnEscClick(object sender, EventArgs e) { UtilityHelper.JumpToTab(xtraTabControl1, 0); } /// /// 删除事件 /// /// /// private void ToolBarMenu1_btnDelClick1(object sender, EventArgs e) { string rowGuid, rowName; (rowGuid, rowName) = UtilityHelper.GetCurrentRow(xtraTabControl1, lbGuid, txt_roleName, gridView1, 2); if (string.IsNullOrEmpty(rowGuid)) { MsgHelper.Warning("请先选择你要操作的行!"); return; } if (!MsgHelper.AskQuestion("你选择了【" + rowName + "】,确定删除吗?\n该操作将会清空相应的用户权限")) return; var lst = new List(); lst.Add(rowGuid); var _obj = lst; try { var strJson = UtilityHelper.HttpPost("", "Role/DeleteModel", JsonConvert.SerializeObject(_obj)); var _rtn = UtilityHelper.ReturnToDynamic(strJson); if (_rtn.rtnCode > 0) { UtilityHelper.JumpToTab(xtraTabControl1, 0); getPageList(1, UtilityHelper.GetPageSize()); } MsgHelper.Warning("提示:" + _rtn.rtnMsg); } catch (Exception ex) { MsgHelper.Warning("提示:" + ex.Message); } } /// /// 刷新事件 /// /// /// private void ToolBarMenu1_btnLoadClick(object sender, EventArgs e) { UtilityHelper.JumpToTab(xtraTabControl1, 0); getPageList(1, UtilityHelper.GetPageSize()); } /// /// 修改事件 /// /// /// private void ToolBarMenu1_btnEdtClick(object sender, EventArgs e) { string rowGuid, rowName; (rowGuid, rowName) = UtilityHelper.GetCurrentRow(xtraTabControl1, lbGuid, txt_roleName, gridView1); if (string.IsNullOrEmpty(rowGuid)) { MsgHelper.Warning("请先选择你要操作的行!"); return; } getModel(rowGuid, true, 1); } /// /// 新增事件 /// /// /// private void ToolBarMenu1_btnAddClick(object sender, EventArgs e) { UtilityHelper.JumpToTab(xtraTabControl1, 1); lbGuid.Text = ""; UtilityHelper.CleanValueByControl(panel1.Controls, true); } /// /// 保存事件 /// /// /// private void ToolBarMenu1_btnSaveClick(object sender, EventArgs e) { toolBarMenu1.isSetBtn = false; if (string.IsNullOrEmpty(txt_roleName.Text.Trim())) { MsgHelper.Warning("角色名称不能为空!"); txt_roleName.Focus(); return; } if (string.IsNullOrEmpty(txt_roleRemark.Text.Trim())) { MsgHelper.Warning("角色备注不能为空!"); txt_roleRemark.Focus(); return; } var _obj = new { guid = lbGuid.Text.Trim(), roleName = txt_roleName.Text.Trim(), roleRemark = txt_roleRemark.Text.Trim(), isStatus = txt_isStatus.Checked //状态 }; try { var strJson = UtilityHelper.HttpPost("", "Role/EditModel", JsonConvert.SerializeObject(_obj)); var _rtn = UtilityHelper.ReturnToDynamic(strJson); MsgHelper.Warning("提示:" + _rtn.rtnMsg); if (_rtn.rtnCode > 0) { lbGuid.Text = _rtn.rtnData; toolBarMenu1.isSetBtn = true; UtilityHelper.ChangeEnableByControl(panel1.Controls, false); } } catch (Exception ex) { MsgHelper.Warning("提示:" + ex.Message); } } /// /// 权限设置事件 /// /// /// private void ToolBarMenu1_btnRoleClick(object sender, EventArgs e) { string rowGuid, rowName; (rowGuid, rowName) = UtilityHelper.GetCurrentRow(xtraTabControl1, lbGuid, txt_roleName, gridView1); if (string.IsNullOrEmpty(rowGuid)) { MsgHelper.Warning("请先选择你要操作的行!"); return; } if (!MsgHelper.AskQuestion("你选择了【" + rowName + "】,确定设置权限吗?")) return; _actionLst = new StringBuilder(); TraverseTreeViewNodes(trv.Nodes); var _obj = new { roleGuid = rowGuid, actionLst = _actionLst.ToString() }; try { var strJson = UtilityHelper.HttpPost("", "Role/SetRoleMenuAction", JsonConvert.SerializeObject(_obj)); var _rtn = UtilityHelper.ReturnToDynamic(strJson); MsgHelper.Warning("提示:" + _rtn.rtnData.outMsg); } catch (Exception ex) { MsgHelper.Warning("提示:" + ex.Message); } } /// /// /// 第几页 /// 每页几条 private void getPageList(int curPage, int pageSize) { var _sbSqlWhere = new StringBuilder(); foreach (var itm in _filterList) _sbSqlWhere.Append(" and " + itm.fileId + itm.fileOper + "'" + itm.fileValue + "'"); var pgq = new PageQueryModel(curPage, pageSize, "CREATE_TIME", "desc", "", _sbSqlWhere.ToString()); var json = JsonConvert.SerializeObject(pgq); try { var strReturn = UtilityHelper.HttpPost("", "Role/GetListPage", json); var dd = UtilityHelper.ReturnToTablePage(strReturn); var dt = dd.rtnData.list; gcMain.BindingContext = new BindingContext(); gcMain.DataSource = dt; gcMain.ForceInitialize(); var dddd = dd.rtnData.pages; //总页 pageBar1.TotalPages = dddd; pageBar1.RecordCount = dd.rtnData.total; //记录总数 pageBar1.CurrentPage = curPage; //当前页 pageBar1.RowsCount = pageSize; //每页显示 pageBar1.setTxt(); } catch (Exception ex) { MsgHelper.Warning("提示:" + ex.Message); } } private void getModel(string strGuid, bool isEdit, int tabIdx) { if (string.IsNullOrEmpty(strGuid)) { MsgHelper.Warning("请先选择你要操作的行!"); return; } UtilityHelper.JumpToTab(xtraTabControl1, tabIdx); var _obj = new { guid = strGuid //主建 }; var strJson = ""; try { strJson = UtilityHelper.HttpPost("", "Role/GetModel", JsonConvert.SerializeObject(_obj)); var _rtn = UtilityHelper.ReturnToDynamic(strJson); if (_rtn.rtnCode > 0) { var dy = _rtn.rtnData; lbGuid.Text = strGuid; UtilityHelper.SetValueByObj(panel1.Controls, dy, isEdit); } else { MsgHelper.Warning("提示:" + _rtn.rtnMsg); } } catch (Exception ex) { MsgHelper.Warning("提示:" + ex.Message); } } private void getTree(string roleGuid) { trv.Nodes.Clear(); var imageList = new ImageList(); imageList.Images.Add("icon0", Resources.publicfix_32x32); imageList.Images.Add("icon1", Resources.addfooter_32x32); imageList.Images.Add("icon2", Resources.listmultilevel_16x16); imageList.Images.Add("icon3", Resources.addcalculatedfield_16x16); trv.ImageList = imageList; var _obj = new { roleGuid //主建 }; try { var strReturn = UtilityHelper.HttpPost("", "Role/GetListByRole", JsonConvert.SerializeObject(_obj)); var dd = UtilityHelper.ReturnToTablePage(strReturn); var dt = dd.rtnData.list; var drGrp = dt.Select("upGuid='' or upGuid is null"); foreach (var _dy in drGrp) { var _orgGuid = _dy["guid"] + "&"; var node = new TreeNode(); //定义结点 node.Text = _dy["name"].ToString(); //为结点赋值 node.Name = _dy["guid"].ToString(); node.ImageIndex = 0; node.Checked = _dy["isCk"].ToString() == "1" ? true : false; var drItem = dt.Select("upGuid='" + _dy["guid"] + "' and orgGuid='" + _dy["guid"] + "'"); foreach (var _dy2 in drItem) { var node22 = new TreeNode(); //定义结点 node22.Text = _dy2["name"].ToString(); //为结点赋值 node22.Name = _orgGuid + _dy2["guid"]; node22.ImageIndex = 1; node22.Checked = _dy2["isCk"].ToString() == "1" ? true : false; var drItem33 = dt.Select("upGuid='" + _dy2["guid"] + "' and orgGuid='" + _dy["guid"] + "'"); foreach (var _dy3 in drItem33) { var node33 = new TreeNode(); //定义结点 node33.Text = _dy3["name"].ToString(); //为结点赋值 node33.Name = _orgGuid + _dy3["guid"]; node33.ImageIndex = 2; node33.Checked = _dy3["isCk"].ToString() == "1" ? true : false; var drItem44 = dt.Select("upGuid='" + _dy3["guid"] + "' and orgGuid='" + _dy["guid"] + "'"); foreach (var _dy4 in drItem44) { var node44 = new TreeNode(); //定义结点 node44.Text = _dy4["name"].ToString(); //为结点赋值 node44.Name = _orgGuid + _dy4["guid"]; node44.ImageIndex = 3; node44.Checked = _dy4["isCk"].ToString() == "1" ? true : false; node33.Nodes.Add(node44); } node22.Nodes.Add(node33); } node22.Expand(); node.Nodes.Add(node22); } node.Expand(); trv.Nodes.Add(node); } } catch (Exception ex) { MsgHelper.Warning("提示:" + ex.Message); } } private void Trv_AfterCheck(object sender, TreeViewEventArgs e) { try { trv.AfterCheck -= Trv_AfterCheck; UtilityHelper.TreeViewCheck(e); trv.AfterCheck += Trv_AfterCheck; } catch (Exception ex) { MsgHelper.ShowError(ex.Message); } } // 遍历TreeView中的所有节点 private void TraverseTreeViewNodes(TreeNodeCollection nodes) { foreach (TreeNode node in nodes) { if (node.Checked) _actionLst.Append(node.Name + "~"); // 递归遍历子节点 if (node.Nodes.Count > 0) TraverseTreeViewNodes(node.Nodes); } } } }