需求:需要分配权限时,要通过组织架构分配,也要通过角色分配,还是把选择的结果显示出来
结果如下图
页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="QuanXian02.aspx.cs" Inherits="MyProject.QuanXian02" %>
权限编辑
类
using System;
using System.Collections.Generic;
using System.Text;
using BLL;
using Common;
using Model;
using Newtonsoft.Json;
namespace MyProject
{
public partial class QuanXian02 : BasePage
{
private string action = string.Empty;
private AssignModel assignModel;
public string strid = "0";//要编辑的ID
public string strNames = "";
public string strisEditAll = "0";
public string strisFenzhen = "0";
public string strjsonUser = "";//最终选择的成员总的json
public string strjsonAssign = "";//最终选择的角色组的成员总的json
public string strjsonMenu = "";//最终选择的成员总的json
public string userID = "";//最终选择的成员总的ID
public string userID02 = "";//最终选择的成员总的ID,角色选择的成员
//public StringBuilder sbResultUser = new StringBuilder();
public string menuID = "";//最终选择的权限总的ID
//public StringBuilder sbResultMenus = new StringBuilder();
StringBuilder sbResultUser01 = new StringBuilder();
StringBuilder sbResultUser02 = new StringBuilder();
public List treeNodesUser = new List();
public List treeNodesAssign = new List();
public List treeNodesMenu = new List();
public string jsonTreeUser = "";//以树展示的所有成员
public string jsonTreeAssign = "";//以树展示的所有角色组下的成员
private string strUser = ".";//选中的员工ID
private string strUser02 = ".";//选中的员工ID,从角色组选择的
public string jsonTreeMenu = "";//以树展示的所有菜单权限
private string strMenu = ".";//选中的菜单ID
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Request.Form["action"]))
{
action = Request.Form["action"];
}
switch (action)
{
case "SaveData"://
SaveData();
break;
default:
string strSearch01 = Request["strSearch"];
string userID01 = Request["userID"];
LoadData();
LoadTreeUser(null, 0, strSearch01, userID01);//从数据库取树开结构 的数据
jsonTreeUser = JsonConvert.SerializeObject(treeNodesUser);//把取到的树形数据转化为JSON格式,赋值给前端的LAYUI
LoadTreeAssign(null, 0, strSearch01, userID01);//从数据库取树开结构 的数据
jsonTreeAssign = JsonConvert.SerializeObject(treeNodesAssign);//把取到的树形数据转化为JSON格式,赋值给前端的LAYUI
LoadTreeMenu(null, 0);//从数据库取树开结构 的数据
jsonTreeMenu = JsonConvert.SerializeObject(treeNodesMenu);//把取到的树形数据转化为JSON格式,赋值给前端的LAYUI
string aa = "";
break;
}
}
}
//加载要编辑的数据
private void LoadData()
{
int id = Convert.ToInt32(Request["id"]);
if (id > 0)
{
assignModel = AssignBLL.GetModel(" and t.id =" + id.ToString());
if (assignModel != null)
{
strid = assignModel.Id.ToString();
strNames = assignModel.Names.ToString();
//strisEditAll = assignModel.IsEditAll.ToString();
//strisFenzhen = assignModel.IsFenzhen.ToString();
}
}
//
List listStaffAssign = StaffAssignBLL.GetListByPage("", " and t.assignID=" + id + " ", " t.id asc ");//查此角色下的所有的员工列表
if (listStaffAssign != null)
{
foreach (StaffAssignModel model in listStaffAssign)
{
userID += "" + model.StaffID.ToString() + ".";
}
if (!string.IsNullOrEmpty(userID))
{
userID = userID.Substring(0, userID.Length - 1);
userID02 = userID;
}
}
List list = AssignMenusBLL.GetListByPage("", " and assignID=" + id + " ", " t.id desc ");//查此角色下的所有的菜单权限列表
if (list != null)
{
foreach (AssignMenusModel model in list)
{
menuID += "" + model.MenuID.ToString() + ".";
}
if (!string.IsNullOrEmpty(menuID))
{
menuID = menuID.Substring(0, menuID.Length - 1);
}
}
PublicHelper.CreateWebLog(strStaffUserCodeBase, "操作日志", "操作IP:" + strLoginIPBase + "。\n操作页面:/AdminControl/QuanXian02.aspx\n操作事件:打开页面");
}
///
/// 加载员工循环树的结果
///
///
///
/// 搜索条件
/// 已选择的成员ID。例4.5.6
private void LoadTreeUser(TreeObject parentNode, int topDeptID, string strSearch, string userID)
{
if (!string.IsNullOrEmpty(userID))
{
userID = userID.Replace(".", ",");
}
List listDepartment01 = null;
if (!string.IsNullOrEmpty(strSearch))
{
if (!string.IsNullOrEmpty(userID))
{
listDepartment01 = DepartmentBLL.GetListTree(" and (t.names like '%" + strSearch + "%' or t.id in (" + userID + ") ) and t.isMoJi=1 ", " t.id asc ",1);//根据上级部门ID,取下一级的所有部门
}
else
{
listDepartment01 = DepartmentBLL.GetListTree(" and t.names like '%" + strSearch + "%' and t.isMoJi=1 ", " t.id asc ", 1);//根据上级部门ID,取下一级的所有部门
}
}
else
{
listDepartment01 = DepartmentBLL.GetListTree(" and t.topDeptID=" + topDeptID + " ", " t.id asc ", 0);//根据上级部门ID,取下一级的所有部门
}
//List listDepartment01 = DepartmentBLL.GetListTree(" and t.topDeptID=" + topDeptID + " ", " t.id asc ");//根据上级部门ID,取下一级的所有部门
if (listDepartment01 != null)
{
foreach (TreeObject model in listDepartment01)
{
TreeObject tree = new TreeObject();
tree.id = model.id;
tree.title = model.title;
tree.topDeptID = model.topDeptID;
tree.isMoJi = model.isMoJi;
tree.field = "";
if (model.id == 9999)
{
tree.spread = true;
}
if (model.id > 8000)
{
//tree.disabled = true;
//tree.showCheckbox = false;
}
//spread 节点是否初始展开,默认 false
//checked 节点是否初始为选中状态(如果开启复选框的话),默认 false
//disabled 节点是否为禁用状态。默认 false
//递归性质,函数内调用自身函数
if (string.IsNullOrEmpty(strSearch))
{
LoadTreeUser(tree, tree.id, strSearch, userID);
}
//LoadTreeUser(tree, tree.id);
递归结束的终点条件
if (parentNode == null)
{
treeNodesUser.Add(tree);
}
else
{
if (true)
//if (model.isMoJi==0)
{
parentNode.children.Add(tree);
}
}
}
}
}
///
/// 加载角色组下的员工循环树的结果
///
///
///
/// 搜索条件
/// 已选择的成员ID。例4.5.6
private void LoadTreeAssign(TreeObject parentNode, int topDeptID, string strSearch, string userID)
{
if (!string.IsNullOrEmpty(userID))
{
userID = userID.Replace(".", ",");
}
List listDepartment01 = null;
if (!string.IsNullOrEmpty(strSearch))
{
if (!string.IsNullOrEmpty(userID))
{
listDepartment01 = AssignBLL.GetListTree(" and (t.names like '%" + strSearch + "%' or t.id in (" + userID + ") ) and t.isMoJi=1 ", " t.id asc ", 1);//根据上级部门ID,取下一级的所有部门
}
else
{
listDepartment01 = AssignBLL.GetListTree(" and t.names like '%" + strSearch + "%' and t.isMoJi=1 ", " t.id asc ", 1);//根据上级部门ID,取下一级的所有部门
}
}
else
{
listDepartment01 = AssignBLL.GetListTree(" and t.topDeptID=" + topDeptID + " ", " t.id asc ", 0);//根据上级部门ID,取下一级的所有部门
}
//List listDepartment01 = AssignBLL.GetListTree(" and t.topDeptID=" + topDeptID + " ", " t.id asc ");//根据上级部门ID,取下一级的所有部门
if (listDepartment01 != null)
{
foreach (TreeObject model in listDepartment01)
{
TreeObject tree = new TreeObject();
tree.id = model.id;
tree.title = model.title;
tree.topDeptID = model.topDeptID;
tree.isMoJi = model.isMoJi;
tree.field = "";
if (model.id == 9999)
{
tree.spread = true;
}
if (model.id > 8000)
{
//tree.disabled = true;
//tree.showCheckbox = false;
}
//spread 节点是否初始展开,默认 false
//checked 节点是否初始为选中状态(如果开启复选框的话),默认 false
//disabled 节点是否为禁用状态。默认 false
//递归性质,函数内调用自身函数
if (string.IsNullOrEmpty(strSearch))
{
LoadTreeAssign(tree, tree.id, strSearch, userID);
}
//LoadTreeAssign(tree, tree.id);
递归结束的终点条件
if (parentNode == null)
{
treeNodesAssign.Add(tree);
}
else
{
if (true)
//if (model.isMoJi==0)
{
parentNode.children.Add(tree);
}
}
}
}
}
//加载权限菜单循环树的结果
private void LoadTreeMenu(TreeObject parentNode, int topDeptID)
{
List listTreeObject = MenusBLL.GetListTree(" and t.topDeptID=" + topDeptID + " ", " t.id asc ");//循环所有的菜单和类型
if (listTreeObject != null)
{
foreach (TreeObject model in listTreeObject)
{
TreeObject tree = new TreeObject();
tree.id = model.id;
tree.title = model.title;
tree.topDeptID = model.topDeptID;
tree.isMoJi = model.isMoJi;
tree.field = "";
if (model.id == 9999)
{
tree.spread = true;
}
if (model.id > 8000)
{
//tree.disabled = true;
//tree.showCheckbox = false;
}
//spread 节点是否初始展开,默认 false
//checked 节点是否初始为选中状态(如果开启复选框的话),默认 false
//disabled 节点是否为禁用状态。默认 false
//递归性质,函数内调用自身函数
LoadTreeMenu(tree, tree.id);
//递归结束的终点条件
if (parentNode == null)
{
treeNodesMenu.Add(tree);
}
else
{
if (true)
//if (model.isMoJi==0)
{
parentNode.children.Add(tree);
}
}
}
}
}
private void SaveData()
{
int iresult = 0;//
ResultInfo resultInfo = new ResultInfo() { State = false, ErrorMsg = "" };
int isRepeat = 1;//是否需要查有无重复记录,默认需要
int isPass = 1;//是否验证通过,默认通过
int id = Convert.ToInt32(Request.Form["id"]);
strNames = Request.Form["names"].ToString().Replace(" ", "");
//strisEditAll = Request.Form["isEditAll"];
//strisFenzhen = Request.Form["isFenzhen"];
strjsonUser = Request.Form["jsonUser"];//取前端传来的树的JSON字符串,组织架构下的员工
strjsonAssign = Request.Form["jsonAssign"];//取前端传来的树的JSON字符串,角色下的员工
strjsonMenu = Request.Form["jsonMenu"];//取前端传来的树的JSON字符串
string oldUserID = Request.Form["oldUserID"];//加载时默认选中的员工ID
if (!string.IsNullOrEmpty(strjsonUser))//取选中的员工ID
{
List listTreeUser = new List();
listTreeUser = JsonConvert.DeserializeObject>(strjsonUser);//字符串反序列化为LIST对象集合
GetTreeNodeUser(listTreeUser);//循环取子节点给strUser赋值。strUser赋值后为".1.2.3.4.5.6."这样的格式,再分隔后来操作数据
//strUser=.1.2.3.4.5.6."这样的格式
}
if (!string.IsNullOrEmpty(strjsonAssign))//取选中的员工ID
{
List listTreeAssign = new List();
listTreeAssign = JsonConvert.DeserializeObject>(strjsonAssign);//字符串反序列化为LIST对象集合
GetTreeNodeUser02(listTreeAssign);//循环取子节点给strUser赋值。strUser赋值后为".1.2.3.4.5.6."这样的格式,再分隔后来操作数据
//strUser02=.1.2.3.4.5.6."这样的格式
}
if (!string.IsNullOrEmpty(strjsonMenu))//取选中的权限菜单ID
{
List listTreeMenu = new List();
listTreeMenu = JsonConvert.DeserializeObject>(strjsonMenu);//字符串反序列化为LIST对象集合
GetTreeNodeMenu(listTreeMenu);//循环取子节点给strMenu赋值。strMenu赋值后为".1.2.3.4.5.6."这样的格式,再分隔后来操作数据
//strMenu=.1.2.3.4.5.6."这样的格式
}
//if (strUser02.Length< strUser.Length)//角色选择的人数比组织架构选择的少,就取组织架构选择的人员
//{
// strUser = strUser02;
//}
//if (!string.IsNullOrEmpty(oldUserID))//
//{
// strUser += oldUserID + ".";
//}
strUser = strUser.Replace(",",".");//把,号改成。号
if (!string.IsNullOrEmpty(strNames))
{
if (id > 0)
{
assignModel = AssignBLL.GetModel(" and t.id =" + id.ToString());
if (assignModel.Names == strNames)//编辑时数据库中的字段如果和此次编辑的内容一样,则无需查重
{
isRepeat = 0;
}
}
if (isRepeat == 1)//检查字段内容在数据库中是否已存在
{
AssignModel model = AssignBLL.GetModel(" and t.isDel=0 and t.names ='" + strNames + "' ");
if (model != null)//如果数据库中有相同内容已存在
{
isPass = 0;
resultInfo.ErrorMsg = "该名称已存在";
}
}
if (isPass == 1)
{
assignModel.Names = strNames;
assignModel.IsEditAll = Convert.ToInt32(strisEditAll);
assignModel.IsFenzhen = Convert.ToInt32(strisFenzhen);
if (id > 0)
{
iresult = AssignBLL.UpdateData(assignModel);
}
else
{
iresult = AssignBLL.AddData(assignModel);
}
if (iresult > 0)
{
//角色基本信息保存成功后,保存角色勾选的员工和权限菜单列表
#region 把角色对应员工一对多,存入字典表
StaffAssignBLL.DeleteDataWhere(" assignID =" + id.ToString());//分配角色前,先把这个角色以前分配过的员工清除,针对修改
if (strUser.Replace(".", "") != "")
{
//ordersModel.State = 3;//如果有分配员工,状态改为3,已分配
strUser = strUser.Substring(1, strUser.Length - 2);
String[] strarr = strUser.Split('.');
StaffAssignModel model = null;
string strTemp = ".";
string strAddStaffAssign = " ";//要批量插入数据库的SQL语句汇总
for (int i = 0; i < strarr.Length; i++)
{
if (strarr[i] != "" && strarr[i] != "0")
{
if (strTemp.IndexOf("." + strarr[i] + ".") < 0)//过滤重复的员工ID
{
strAddStaffAssign += " INSERT INTO StaffAssign( staffID, assignID ) VALUES (" + Convert.ToInt32(strarr[i]) + "," + id + "); ";
//model = new StaffAssignModel();
//model.AssignID = id;
//model.StaffID = Convert.ToInt32(strarr[i]);
//StaffAssignBLL.AddData(model);
}
strTemp += strarr[i] + ".";
}
}
iresult += StaffAssignBLL.AddDataMore(strAddStaffAssign);
}
#endregion
#region 把角色对应菜单一对多,存入字典表
AssignMenusBLL.DeleteDataWhere(" assignID =" + id.ToString());//分配前,先把这个以前分配过的清除,针对修改
if (strMenu.Replace(".", "") != "")
{
strMenu = strMenu.Substring(1, strMenu.Length - 2);
String[] strarr = strMenu.Split('.');
AssignMenusModel model = null;
string strTemp = ".";
string strAddAssignMenus = " ";//要批量插入数据库的SQL语句汇总
for (int i = 0; i < strarr.Length; i++)
{
if (strarr[i] != "" && strarr[i] != "0")
{
if (strTemp.IndexOf("." + strarr[i] + ".") < 0)//过滤重复的ID
{
strAddAssignMenus += " INSERT INTO AssignMenus( menuID, assignID ) VALUES (" + Convert.ToInt32(strarr[i]) + "," + id + "); ";
//model = new AssignMenusModel();
//model.AssignID = id;
//model.MenuID = Convert.ToInt32(strarr[i]);
//AssignMenusBLL.AddData(model);
}
strTemp += strarr[i] + ".";
}
}
iresult += AssignMenusBLL.AddDataMore(strAddAssignMenus);
}
#endregion
resultInfo.Data = iresult.ToString();
resultInfo.State = true;
}
}
}
string strLog = id > 0 ? "修改角色,ID为:" + id.ToString() : "添加角色,名为:" + strNames;
PublicHelper.CreateWebLog(strStaffUserCodeBase, "操作日志", "操作IP:" + strLoginIPBase + "。\n操作页面:/AdminControl/QuanXian02.aspx。\n操作事件:" + strLog);
string resultJson = JsonHelper.ObjectToJSON(resultInfo);
Response.Write(resultJson);
Response.End();
}
private void GetTreeNodeUser(List listTree)//无限内循环获取树的每级节点
{
foreach (TreeObject Tree in listTree)
{
if (Tree.isMoJi == 1)
{
strUser += Tree.id + ".";
}
else
{
if (Tree.children != null)
{
GetTreeNodeUser(Tree.children);
}
}
}
}
private void GetTreeNodeUser02(List listTree)//无限内循环获取树的每级节点
{
foreach (TreeObject Tree in listTree)
{
if (Tree.isMoJi == 1)
{
strUser += Tree.id + ".";
}
else
{
if (Tree.children != null)
{
GetTreeNodeUser02(Tree.children);
}
}
}
}
private void GetTreeNodeMenu(List listTree)//无限内循环获取树的每级节点
{
foreach (TreeObject Tree in listTree)
{
if (Tree.isMoJi == 1)
{
strMenu += Tree.id + ".";
}
else
{
if (Tree.children != null)
{
GetTreeNodeMenu(Tree.children);
}
}
}
}
}
}
SQL
SELECT 9999 AS id,'一级树名' AS names,0 AS topDeptID,0 AS isMoJi
UNION ALL
SELECT 9900+t.id AS id,t.names,9999 AS topDeptID,0 AS isMoJi FROM AssignType t WHERE t.isDel=0
UNION ALL
SELECT 8000+id,t.names,9000+typeID AS topDeptID,0 AS isMoJi FROM Assign t WHERE t.isDel=0
UNION ALL
SELECT Staff.id,Staff.userName AS names,8000+StaffAssign.assignID AS topDeptID,1 AS isMoJi FROM StaffAssign INNER JOIN Staff ON StaffAssign.staffID=Staff.id