123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592 |
- using JCSoft.WX.Framework.Api;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Http;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Common.Wechat;
- using Common;
- using System.Data;
- using Common.Model;
- using ZcPeng.PublicLibrary;
- using CoreEntity.Entity;
- using CoreEntity.DAL;
- using System.Collections.Concurrent;
- using Newtonsoft.Json.Converters;
- using Newtonsoft.Json;
- using System.Data.SqlClient;
- using Microsoft.Extensions.Primitives;
- using Microsoft.Extensions.Caching.Memory;
- using Jwt;
- using PublicLibrary.Model;
- using Newtonsoft.Json.Linq;
- using SupplierWeb.Codes.mvc;
- using SupplierWeb.Codes.Auth;
- using Common.Config;
- namespace SupplierWeb.Controllers
- {
- [Route("web/OrgRole")]
- public class OrgRoleController : BaseController
- {
- public OrgRoleController(IMemoryCache cache, IApiClient client) : base(cache, client)
- {
- }
- private ConcurrentDictionary<String, CM_OrgRole> roleMap = new ConcurrentDictionary<String, CM_OrgRole>();
- public class SortContent
- {
- public Object content { get; set; }//权限内容
- public string code { get; set; }//权限id
- public int sort { get; set; }//权限序号
- }
- public class TempTableResult
- {
- public int id { get; set; }
- public string permissionName { get; set; }
- public string permissionUrl { get; set; }
- public int roleId { get; set; }
- public int checkedValue { get; set; }
- public string permissionRule { get; set; }
- public string permissionRuleType { get; set; }
- public int relateId { get; set; }
- public string AuthType { get; set; }
- }
- public class TreeNode
- {
- public string parentValue { get; set; }
- public string label { get; set; }
- public int value { get; set; }
- }
- public class TempQuery
- {
- public int[] data { get; set; }
- public int roleId { get; set; }
- }
- /// <summary>
- /// 机构角色列表数据源
- /// </summary>
- /// <param name="filters"></param>
- /// <param name="pageIndex"></param>
- /// <param name="pageSize"></param>
- /// <param name="sortField"></param>
- /// <param name="sortDirection"></param>
- /// <param name="sumFields"></param>
- /// <param name="data"></param>
- /// <returns></returns>
- [AuthPermission]
- [HttpPost, Route("index")]
- public JsonResult Indexp(QueryFilter[] filters, Int32 pageIndex, Int32 pageSize,
- string sortField, Int32 sortDirection, string[] sumFields, [FromBody]dynamic data)
- {
- if (data != null)
- {
- filters = data.filters.ToObject<QueryFilter[]>();
- pageIndex = data.pageIndex;
- pageSize = data.pageSize;
- sortField = data.sortField;
- sortDirection = data.sortDirection;
- sumFields = data.sumFields.ToObject<string[]>();
- }
- #region 获取角色列表
- DataTable dt = new DataTable();
- string result;
- IList<CM_OrgRole> permss = new List<CM_OrgRole>(0);
- List<SqlParameter> parameters = new List<SqlParameter>();
- string filterstr = QueryFilter.getFilterSqlParam(filters, out parameters, new CM_OrgRole(), "A.");
- string direct = " desc ";
- if (sortDirection != 1)
- {
- direct = " asc";
- }
- int start = (pageIndex - 1) * pageSize;
- int end = (start + 1 + pageSize);
- string commandText0 = "select * from ";
- //string commandText1 = "(" +
- // "select A.*,(select OrgName from CM_Org where OrgId=A.orgid) as orgname ," +
- // "(select RoleName from CM_Role where RoleId=A.RoleId) as rolename ,row_number() over" +
- // "( order by A.OrgRoleId " + direct + " ) as rownum from " +
- // "CM_OrgRole as A " +
- // " where 1=1 " +
- // //" and A.CreationPerson = '" + userids + "'" +
- // filterstr +
- //")AAA ";
- string commandText1 = "(" +
- "select A.*,row_number() over" +
- "( order by A.OrgRoleId " + direct + " ) as rownum from " +
- "CM_OrgRole as A " +
- " where 1=1 " +
- //" and A.CreationPerson = '" + userids + "'" +
- filterstr +
- ")AAA ";
- string commandText2 = " where AAA.rownum>" + start + " and AAA.rownum<" + end;
- string commandText3 = commandText0 + commandText1 + commandText2;
- bool success = DataAccess.GetValues(commandText3, ref dt, parameters.ToArray(), out result);
- #endregion
- if (dt != null && dt.Rows.Count > 0)
- {
- // 把DataTable转换为IList<Permission>
- permss = ModelConvertHelper<CM_OrgRole>.ConvertToModel(dt);
- #region 获取权限的角色列表,角色列表
- //角色列表
- IList<CM_OrgRole> roles = GetRoles();
- // 把DataTable转换为IList<OrgRole>
- if (roleMap.Count < roles.Count)
- {
- foreach (CM_OrgRole role in roles)
- {
- roleMap.TryAdd(role.OrgRoleId, role);
- }
- }
- #region
- foreach (CM_OrgRole perms in permss)
- {
- string direct1 = " desc ";
- dt = DataAccess.GetDataTable("CM_OrgRole", "OrgRoleId", "*", "OrgRoleId=" + perms.OrgRoleId, "", "OrgRoleId" + direct1, 1, 100, out var msg);
- // 把DataTable转换为IList<RoleRelatePermission>
- if (dt != null && dt.Rows.Count > 0)
- {
- IList<CM_OrgRole> permRelates = ModelConvertHelper<CM_OrgRole>.ConvertToModel(dt);
- List<CM_OrgRole> listRoles = new List<CM_OrgRole>();
- List<int> superior_list = new List<int>(0);
- foreach (CM_OrgRole permsr in permRelates)
- {
- CM_OrgRole role;
- if (roleMap.TryGetValue(permsr.OrgRoleId, out role))
- {
- listRoles.Add(role);
- }
- superior_list.Add(Convert.ToInt32(permsr.OrgRoleId));
- }
- //perms.RoleList = listRoles;
- perms.OrgRoleId = string.Join(",", superior_list.ToArray());
- }
- }
- #endregion
- #endregion
- }
- string result1;
- long totalcount = DataAccess.GetRowCountDefine("select count(OrgRoleId) from " + commandText1, parameters.ToArray(), out result1);
- IsoDateTimeConverter timejson = new IsoDateTimeConverter
- {
- DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"
- };
- //IList<Menu> menus = Permission.Convert(permss);
- var jsonData = JsonConvert.SerializeObject(permss, timejson);
- return Json(new
- {
- items = JsonConvert.DeserializeObject(jsonData),
- sum = new { },
- totalCount = totalcount
- });
- }
-
- /// <summary>
- /// 新增机构角色
- /// </summary>
- /// <param name="OrgRoleId"></param>
- /// <param name="OrgId"></param>
- /// <param name="RoleId"></param>
- /// <param name="data"></param>
- /// <returns></returns>
- [AuthPermission]
- [HttpPost, Route("add")]
- public JsonResult Addp(
- String OrgRoleId, String OrgId,
- String RoleId, [FromBody]dynamic data
- )
- {
- if (data != null)
- {
- OrgRoleId = data.id;
- OrgId = data.OrgId;
- RoleId = data.RoleRemark;
- }
- #region 添加角色
- String uuid = System.Guid.NewGuid().ToString("N");
- string commandText = "INSERT INTO CM_OrgRole (OrgRoleId," +
- "OrgId,RoleId)" +
- " VALUES ('" + uuid + "',@OrgId,@RoleId)";
- string result;
- //准备参数
- List<List<Object>> parameters = new List<List<Object>>();
- parameters.Add(new List<Object>() { "OrgRoleId", uuid });
- parameters.Add(new List<Object>() { "OrgId", OrgId });
- parameters.Add(new List<Object>() { "RoleId", RoleId });
- List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
- int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
- #endregion
- //#region 添加角色
- //int id = BaseDAL.GetId(Config.TablePrefix + "Permission");
- //RoleDAL.SaveRolesRelatePermission(id, OrgRoleIdis);
- //#endregion
- return Json(new
- {
- success = success
- });
- }
- /// <summary>
- /// 修改机构角色
- /// </summary>
- /// <param name="OrgRoleId"></param>
- /// <param name="OrgId"></param>
- /// <param name="RoleId"></param>
- /// <param name="data"></param>
- /// <returns></returns>
- [AuthPermission]
- [HttpPost, Route("edit")]
- public JsonResult editp(String OrgRoleId, String OrgId,
- String RoleId, [FromBody]dynamic data)
- {
- //string RoleName = "";
- //string OrgName = "";
- if (data != null)
- {
- OrgRoleId = data.OrgRoleId;
- OrgId = data.OrgId;
- RoleId = data.RoleRemark;
- //RoleName = data.RoleName;
- //OrgName = data.OrgName;
- }
- #region 编辑用户
- string commandText = "UPDATE CM_OrgRole " +
- " SET OrgId = @OrgId,RoleId = @RoleId WHERE OrgRoleId= @OrgRoleId";
- string result;
- //准备参数
- List<List<Object>> parameters = new List<List<Object>>();
- parameters.Add(new List<Object>() { "OrgId", OrgId });
- parameters.Add(new List<Object>() { "RoleId", RoleId });
- //parameters.Add(new List<Object>() { "OrgId", OrgId });
- //parameters.Add(new List<Object>() { "RoleId", RoleId });
- parameters.Add(new List<Object>() { "OrgRoleId", OrgRoleId });
- List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
- int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
- #endregion
- return Json(new
- {
- success = success
- });
- }
-
- /// <summary>
- /// 删除角色
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- [AuthPermission]
- [HttpPost, Route("delete")]
- public ActionResult Delete(string id)
- {
- string commandText = "DELETE FROM CM_OrgRole WHERE OrgRoleId = @OrgRoleId";
- //准备参数
- List<List<Object>> parameters = new List<List<Object>>();
- parameters.Add(new List<Object>() { "OrgRoleId", id });
- string result = "";
- List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
- int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
- return Content("{success:" + success + "}");
- }
- /// <summary>
- /// 获取所有角色
- /// </summary>
- /// <returns></returns>
- [HttpGet, Route("roles")]
- public ActionResult Roles()
- {
- var permss = GetAllXTRoles();
- IList<Option> options = new List<Option>();
- foreach (Role perms in permss)
- {
- var option = new Option() { label = perms.RoleRemark, value = perms.Id.ToString() };
- options.Add(option);
- }
- var jsonData = JsonConvert.SerializeObject(options);
- return Content(jsonData);
- }
- /// <summary>
- /// 获取所有组织结构
- /// </summary>
- /// <returns></returns>
- [HttpGet, Route("Orgs")]
- public ActionResult Orgs()
- {
- var permss = GetAllOrgs();
- IList<Option> options = new List<Option>();
- foreach (CM_Org perms in permss)
- {
- var option = new Option() { label = perms.OrgName, value = perms.OrgId.ToString() };
- options.Add(option);
- }
- var jsonData = JsonConvert.SerializeObject(options);
- return Content(jsonData);
- }
- /// <summary>
- /// 权限列表
- /// </summary>
- /// <param name="staffid"></param>
- /// <returns></returns>
- [HttpGet, Route("menus")]
- public ActionResult menus(string staffid)
- {
- HttpRequest request = HttpContext.Request;
- StringValues oo;
- request.Headers.TryGetValue("token", out oo);
- if (oo.Count > 0 && oo.ToArray()[0] != "")
- {
- staffid = oo.ToArray()[0];
- }
- //取token
- var token = (string)_cache.Get(staffid);
- var secret = TokenConfig.SecretKey;
- Dictionary<string, object> data;
- Object roleid;
- Int64 roleid1;
- string jsonData = "";
- if (token != null)
- {
- try
- {
- data = JsonWebToken.DecodeToObject<Dictionary<string, object>>(token, secret);
- data.TryGetValue("roleid", out roleid);
- roleid1 = (Int64)roleid;
- var options = RoleDAL.GetPermissions(roleid1);
- var permissions = Permission.Convert(options);
- jsonData = JsonConvert.SerializeObject(permissions);
- }
- catch (SignatureVerificationException)
- {
- // Given token is either expired or hashed with an unsupported algorithm.
- }
- }
- return Content(jsonData
- );
- }
-
- [HttpGet, Route("parents")]
- public ActionResult parents()
- {
- var permss = RoleDAL.GetTopPermissions();
- IList<Option> options = new List<Option>();
- foreach (Permission perms in permss)
- {
- var option = new Option() { label = perms.PermissionName, value = perms.Id.ToString(), disabled = perms.IsDelete };
- options.Add(option);
- }
- var jsonData = JsonConvert.SerializeObject(options);
- return Content(jsonData
- );
- }
-
- [HttpGet, Route("authtype")]
- public ActionResult AuthType()
- {
- IList<Option> options = new List<Option>();
- var option = new Option() { label = "界面", value = "U", disabled = false };
- options.Add(option);
- var option1 = new Option() { label = "操作", value = "O", disabled = false };
- options.Add(option1);
- var option2 = new Option() { label = "按钮", value = "A", disabled = false };
- options.Add(option2);
- var jsonData = JsonConvert.SerializeObject(options);
- return Content(jsonData
- );
- }
- [HttpGet, Route("permissions")]
- public ActionResult Permissions()
- {
- string roleid = getStaff("roleid");
- if (roleid == null || roleid == "")
- {
- return Json(new { success = false, msg = "没有登陆" });
- }
- Dictionary<string, Permission> permission = PermissionHelper.Permissions(Convert.ToInt32(roleid));
- return Json(permission);
- }
-
- /// <summary>
- /// 获取所有CM_角色
- /// </summary>
- /// <returns></returns>
- public IList<CM_Role> GetAllRoles()
- {
- IList<CM_Role> permsList = new List<CM_Role>(0);
- DataTable dt = new DataTable();
- string result = string.Empty;
- var sortDirection = 1;
- var pageIndex = 1;
- var pageSize = Config.MaxPageSize;
- string direct = " desc ";
- if (sortDirection != 1)
- direct = " asc";
- int start = (pageIndex - 1) * pageSize;
- int end = (start + 1 + pageSize);
- string commandText = "select * from (" +
- "select A.*,row_number() over" +
- "( order by A.RoleID " + direct + " ) as rownum" +
- " from CM_Role A ) AAA" +
- " where AAA.rownum>" + start + " and AAA.rownum<" + end +
- " ";
- List<List<Object>> parameters1 = new List<List<Object>>();
- parameters1.Add(new List<Object>() { "RoleId", 0 });
- bool result1 = DataAccess.GetValues(commandText, ref dt, DataAccess.ToParameters(parameters1).ToArray(), out result);
- if (result1 && dt.Rows.Count > 0)
- {
- // 把DataTable转换为IList<Role>
- permsList = ModelConvertHelper<CM_Role>.ConvertToModel(dt);
- }
- return permsList;
- }
- /// <summary>
- /// 获取所有"协同"角色
- /// </summary>
- /// <returns></returns>
- public IList<Role> GetAllXTRoles()
- {
- IList<Role> permsList = new List<Role>(0);
- DataTable dt = new DataTable();
- string result = string.Empty;
- var sortDirection = 1;
- var pageIndex = 1;
- var pageSize = Config.MaxPageSize;
- string direct = " desc ";
- if (sortDirection != 1)
- direct = " asc";
- int start = (pageIndex - 1) * pageSize;
- int end = (start + 1 + pageSize);
- string commandText = "select * from (" +
- "select A.*,row_number() over" +
- "( order by A.Id " + direct + " ) as rownum" +
- " from sup_Role A ) AAA" +
- " where AAA.rownum>" + start + " and AAA.rownum<" + end +
- " ";
-
- List<List<Object>> parameters1 = new List<List<Object>>();
- parameters1.Add(new List<Object>() { "Id", 0 });
- bool result1 = DataAccess.GetValues(commandText, ref dt, DataAccess.ToParameters(parameters1).ToArray(), out result);
- if (result1 && dt.Rows.Count > 0)
- {
- // 把DataTable转换为IList<Role>
- permsList = ModelConvertHelper<Role>.ConvertToModel(dt);
- }
- return permsList;
- }
- /// <summary>
- /// 获取所有组织机构
- /// </summary>
- /// <returns></returns>
- public IList<CM_Org> GetAllOrgs()
- {
- IList<CM_Org> permsList = new List<CM_Org>(0);
- DataTable dt = new DataTable();
- string result = string.Empty;
- var sortDirection = 1;
- var pageIndex = 1;
- var pageSize = Config.MaxPageSize;
- string direct = " desc ";
- if (sortDirection != 1)
- direct = " asc";
- int start = (pageIndex - 1) * pageSize;
- int end = (start + 1 + pageSize);
- string commandText = "select * from (" +
- "select A.*,row_number() over" +
- "( order by A.OrgId " + direct + " ) as rownum" +
- " from CM_Org A ) AAA" +
- " where AAA.rownum>" + start + " and AAA.rownum<" + end +
- " ";
- List<List<Object>> parameters1 = new List<List<Object>>();
- parameters1.Add(new List<Object>() { "OrgId", 0 });
- bool result1 = DataAccess.GetValues(commandText, ref dt, DataAccess.ToParameters(parameters1).ToArray(), out result);
- if (result1 && dt.Rows.Count > 0)
- {
- // 把DataTable转换为IList<Role>
- permsList = ModelConvertHelper<CM_Org>.ConvertToModel(dt);
- }
- return permsList;
- }
- public static IList<CM_OrgRole> GetRoles()
- {
- IList<CM_OrgRole> roleList = new List<CM_OrgRole>(0);
- DataTable result;
- string direct = " desc ";
- result = DataAccess.GetDataTable("CM_OrgRole", "Id", "OrgRoleId,RoleName,RoleCode", "", "", "Id" + direct, 1, 100, out var msg);
- if (result != null && result.Rows.Count > 0)
- {
- // 把DataTable转换为IList<Role>
- roleList = ModelConvertHelper<CM_OrgRole>.ConvertToModel(result);
- }
- return roleList;
- }
- }
- }
|