CM_OrgRoleController.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. using JCSoft.WX.Framework.Api;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.Http;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Common.Wechat;
  9. using Common;
  10. using System.Data;
  11. using Common.Model;
  12. using ZcPeng.PublicLibrary;
  13. using CoreEntity.Entity;
  14. using CoreEntity.DAL;
  15. using System.Collections.Concurrent;
  16. using Newtonsoft.Json.Converters;
  17. using Newtonsoft.Json;
  18. using System.Data.SqlClient;
  19. using Microsoft.Extensions.Primitives;
  20. using Microsoft.Extensions.Caching.Memory;
  21. using Jwt;
  22. using PublicLibrary.Model;
  23. using Newtonsoft.Json.Linq;
  24. using SupplierWeb.Codes.mvc;
  25. using SupplierWeb.Codes.Auth;
  26. using Common.Config;
  27. namespace SupplierWeb.Controllers
  28. {
  29. [Route("web/OrgRole")]
  30. public class OrgRoleController : BaseController
  31. {
  32. public OrgRoleController(IMemoryCache cache, IApiClient client) : base(cache, client)
  33. {
  34. }
  35. private ConcurrentDictionary<String, CM_OrgRole> roleMap = new ConcurrentDictionary<String, CM_OrgRole>();
  36. public class SortContent
  37. {
  38. public Object content { get; set; }//权限内容
  39. public string code { get; set; }//权限id
  40. public int sort { get; set; }//权限序号
  41. }
  42. public class TempTableResult
  43. {
  44. public int id { get; set; }
  45. public string permissionName { get; set; }
  46. public string permissionUrl { get; set; }
  47. public int roleId { get; set; }
  48. public int checkedValue { get; set; }
  49. public string permissionRule { get; set; }
  50. public string permissionRuleType { get; set; }
  51. public int relateId { get; set; }
  52. public string AuthType { get; set; }
  53. }
  54. public class TreeNode
  55. {
  56. public string parentValue { get; set; }
  57. public string label { get; set; }
  58. public int value { get; set; }
  59. }
  60. public class TempQuery
  61. {
  62. public int[] data { get; set; }
  63. public int roleId { get; set; }
  64. }
  65. /// <summary>
  66. /// 机构角色列表数据源
  67. /// </summary>
  68. /// <param name="filters"></param>
  69. /// <param name="pageIndex"></param>
  70. /// <param name="pageSize"></param>
  71. /// <param name="sortField"></param>
  72. /// <param name="sortDirection"></param>
  73. /// <param name="sumFields"></param>
  74. /// <param name="data"></param>
  75. /// <returns></returns>
  76. [AuthPermission]
  77. [HttpPost, Route("index")]
  78. public JsonResult Indexp(QueryFilter[] filters, Int32 pageIndex, Int32 pageSize,
  79. string sortField, Int32 sortDirection, string[] sumFields, [FromBody]dynamic data)
  80. {
  81. if (data != null)
  82. {
  83. filters = data.filters.ToObject<QueryFilter[]>();
  84. pageIndex = data.pageIndex;
  85. pageSize = data.pageSize;
  86. sortField = data.sortField;
  87. sortDirection = data.sortDirection;
  88. sumFields = data.sumFields.ToObject<string[]>();
  89. }
  90. #region 获取角色列表
  91. DataTable dt = new DataTable();
  92. string result;
  93. IList<CM_OrgRole> permss = new List<CM_OrgRole>(0);
  94. List<SqlParameter> parameters = new List<SqlParameter>();
  95. string filterstr = QueryFilter.getFilterSqlParam(filters, out parameters, new CM_OrgRole(), "A.");
  96. string direct = " desc ";
  97. if (sortDirection != 1)
  98. {
  99. direct = " asc";
  100. }
  101. int start = (pageIndex - 1) * pageSize;
  102. int end = (start + 1 + pageSize);
  103. string commandText0 = "select * from ";
  104. //string commandText1 = "(" +
  105. // "select A.*,(select OrgName from CM_Org where OrgId=A.orgid) as orgname ," +
  106. // "(select RoleName from CM_Role where RoleId=A.RoleId) as rolename ,row_number() over" +
  107. // "( order by A.OrgRoleId " + direct + " ) as rownum from " +
  108. // "CM_OrgRole as A " +
  109. // " where 1=1 " +
  110. // //" and A.CreationPerson = '" + userids + "'" +
  111. // filterstr +
  112. //")AAA ";
  113. string commandText1 = "(" +
  114. "select A.*,row_number() over" +
  115. "( order by A.OrgRoleId " + direct + " ) as rownum from " +
  116. "CM_OrgRole as A " +
  117. " where 1=1 " +
  118. //" and A.CreationPerson = '" + userids + "'" +
  119. filterstr +
  120. ")AAA ";
  121. string commandText2 = " where AAA.rownum>" + start + " and AAA.rownum<" + end;
  122. string commandText3 = commandText0 + commandText1 + commandText2;
  123. bool success = DataAccess.GetValues(commandText3, ref dt, parameters.ToArray(), out result);
  124. #endregion
  125. if (dt != null && dt.Rows.Count > 0)
  126. {
  127. // 把DataTable转换为IList<Permission>
  128. permss = ModelConvertHelper<CM_OrgRole>.ConvertToModel(dt);
  129. #region 获取权限的角色列表,角色列表
  130. //角色列表
  131. IList<CM_OrgRole> roles = GetRoles();
  132. // 把DataTable转换为IList<OrgRole>
  133. if (roleMap.Count < roles.Count)
  134. {
  135. foreach (CM_OrgRole role in roles)
  136. {
  137. roleMap.TryAdd(role.OrgRoleId, role);
  138. }
  139. }
  140. #region
  141. foreach (CM_OrgRole perms in permss)
  142. {
  143. string direct1 = " desc ";
  144. dt = DataAccess.GetDataTable("CM_OrgRole", "OrgRoleId", "*", "OrgRoleId=" + perms.OrgRoleId, "", "OrgRoleId" + direct1, 1, 100, out var msg);
  145. // 把DataTable转换为IList<RoleRelatePermission>
  146. if (dt != null && dt.Rows.Count > 0)
  147. {
  148. IList<CM_OrgRole> permRelates = ModelConvertHelper<CM_OrgRole>.ConvertToModel(dt);
  149. List<CM_OrgRole> listRoles = new List<CM_OrgRole>();
  150. List<int> superior_list = new List<int>(0);
  151. foreach (CM_OrgRole permsr in permRelates)
  152. {
  153. CM_OrgRole role;
  154. if (roleMap.TryGetValue(permsr.OrgRoleId, out role))
  155. {
  156. listRoles.Add(role);
  157. }
  158. superior_list.Add(Convert.ToInt32(permsr.OrgRoleId));
  159. }
  160. //perms.RoleList = listRoles;
  161. perms.OrgRoleId = string.Join(",", superior_list.ToArray());
  162. }
  163. }
  164. #endregion
  165. #endregion
  166. }
  167. string result1;
  168. long totalcount = DataAccess.GetRowCountDefine("select count(OrgRoleId) from " + commandText1, parameters.ToArray(), out result1);
  169. IsoDateTimeConverter timejson = new IsoDateTimeConverter
  170. {
  171. DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"
  172. };
  173. //IList<Menu> menus = Permission.Convert(permss);
  174. var jsonData = JsonConvert.SerializeObject(permss, timejson);
  175. return Json(new
  176. {
  177. items = JsonConvert.DeserializeObject(jsonData),
  178. sum = new { },
  179. totalCount = totalcount
  180. });
  181. }
  182. /// <summary>
  183. /// 新增机构角色
  184. /// </summary>
  185. /// <param name="OrgRoleId"></param>
  186. /// <param name="OrgId"></param>
  187. /// <param name="RoleId"></param>
  188. /// <param name="data"></param>
  189. /// <returns></returns>
  190. [AuthPermission]
  191. [HttpPost, Route("add")]
  192. public JsonResult Addp(
  193. String OrgRoleId, String OrgId,
  194. String RoleId, [FromBody]dynamic data
  195. )
  196. {
  197. if (data != null)
  198. {
  199. OrgRoleId = data.id;
  200. OrgId = data.OrgId;
  201. RoleId = data.RoleRemark;
  202. }
  203. #region 添加角色
  204. String uuid = System.Guid.NewGuid().ToString("N");
  205. string commandText = "INSERT INTO CM_OrgRole (OrgRoleId," +
  206. "OrgId,RoleId)" +
  207. " VALUES ('" + uuid + "',@OrgId,@RoleId)";
  208. string result;
  209. //准备参数
  210. List<List<Object>> parameters = new List<List<Object>>();
  211. parameters.Add(new List<Object>() { "OrgRoleId", uuid });
  212. parameters.Add(new List<Object>() { "OrgId", OrgId });
  213. parameters.Add(new List<Object>() { "RoleId", RoleId });
  214. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  215. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  216. #endregion
  217. //#region 添加角色
  218. //int id = BaseDAL.GetId(Config.TablePrefix + "Permission");
  219. //RoleDAL.SaveRolesRelatePermission(id, OrgRoleIdis);
  220. //#endregion
  221. return Json(new
  222. {
  223. success = success
  224. });
  225. }
  226. /// <summary>
  227. /// 修改机构角色
  228. /// </summary>
  229. /// <param name="OrgRoleId"></param>
  230. /// <param name="OrgId"></param>
  231. /// <param name="RoleId"></param>
  232. /// <param name="data"></param>
  233. /// <returns></returns>
  234. [AuthPermission]
  235. [HttpPost, Route("edit")]
  236. public JsonResult editp(String OrgRoleId, String OrgId,
  237. String RoleId, [FromBody]dynamic data)
  238. {
  239. //string RoleName = "";
  240. //string OrgName = "";
  241. if (data != null)
  242. {
  243. OrgRoleId = data.OrgRoleId;
  244. OrgId = data.OrgId;
  245. RoleId = data.RoleRemark;
  246. //RoleName = data.RoleName;
  247. //OrgName = data.OrgName;
  248. }
  249. #region 编辑用户
  250. string commandText = "UPDATE CM_OrgRole " +
  251. " SET OrgId = @OrgId,RoleId = @RoleId WHERE OrgRoleId= @OrgRoleId";
  252. string result;
  253. //准备参数
  254. List<List<Object>> parameters = new List<List<Object>>();
  255. parameters.Add(new List<Object>() { "OrgId", OrgId });
  256. parameters.Add(new List<Object>() { "RoleId", RoleId });
  257. //parameters.Add(new List<Object>() { "OrgId", OrgId });
  258. //parameters.Add(new List<Object>() { "RoleId", RoleId });
  259. parameters.Add(new List<Object>() { "OrgRoleId", OrgRoleId });
  260. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  261. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  262. #endregion
  263. return Json(new
  264. {
  265. success = success
  266. });
  267. }
  268. /// <summary>
  269. /// 删除角色
  270. /// </summary>
  271. /// <param name="id"></param>
  272. /// <returns></returns>
  273. [AuthPermission]
  274. [HttpPost, Route("delete")]
  275. public ActionResult Delete(string id)
  276. {
  277. string commandText = "DELETE FROM CM_OrgRole WHERE OrgRoleId = @OrgRoleId";
  278. //准备参数
  279. List<List<Object>> parameters = new List<List<Object>>();
  280. parameters.Add(new List<Object>() { "OrgRoleId", id });
  281. string result = "";
  282. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  283. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  284. return Content("{success:" + success + "}");
  285. }
  286. /// <summary>
  287. /// 获取所有角色
  288. /// </summary>
  289. /// <returns></returns>
  290. [HttpGet, Route("roles")]
  291. public ActionResult Roles()
  292. {
  293. var permss = GetAllXTRoles();
  294. IList<Option> options = new List<Option>();
  295. foreach (Role perms in permss)
  296. {
  297. var option = new Option() { label = perms.RoleRemark, value = perms.Id.ToString() };
  298. options.Add(option);
  299. }
  300. var jsonData = JsonConvert.SerializeObject(options);
  301. return Content(jsonData);
  302. }
  303. /// <summary>
  304. /// 获取所有组织结构
  305. /// </summary>
  306. /// <returns></returns>
  307. [HttpGet, Route("Orgs")]
  308. public ActionResult Orgs()
  309. {
  310. var permss = GetAllOrgs();
  311. IList<Option> options = new List<Option>();
  312. foreach (CM_Org perms in permss)
  313. {
  314. var option = new Option() { label = perms.OrgName, value = perms.OrgId.ToString() };
  315. options.Add(option);
  316. }
  317. var jsonData = JsonConvert.SerializeObject(options);
  318. return Content(jsonData);
  319. }
  320. /// <summary>
  321. /// 权限列表
  322. /// </summary>
  323. /// <param name="staffid"></param>
  324. /// <returns></returns>
  325. [HttpGet, Route("menus")]
  326. public ActionResult menus(string staffid)
  327. {
  328. HttpRequest request = HttpContext.Request;
  329. StringValues oo;
  330. request.Headers.TryGetValue("token", out oo);
  331. if (oo.Count > 0 && oo.ToArray()[0] != "")
  332. {
  333. staffid = oo.ToArray()[0];
  334. }
  335. //取token
  336. var token = (string)_cache.Get(staffid);
  337. var secret = TokenConfig.SecretKey;
  338. Dictionary<string, object> data;
  339. Object roleid;
  340. Int64 roleid1;
  341. string jsonData = "";
  342. if (token != null)
  343. {
  344. try
  345. {
  346. data = JsonWebToken.DecodeToObject<Dictionary<string, object>>(token, secret);
  347. data.TryGetValue("roleid", out roleid);
  348. roleid1 = (Int64)roleid;
  349. var options = RoleDAL.GetPermissions(roleid1);
  350. var permissions = Permission.Convert(options);
  351. jsonData = JsonConvert.SerializeObject(permissions);
  352. }
  353. catch (SignatureVerificationException)
  354. {
  355. // Given token is either expired or hashed with an unsupported algorithm.
  356. }
  357. }
  358. return Content(jsonData
  359. );
  360. }
  361. [HttpGet, Route("parents")]
  362. public ActionResult parents()
  363. {
  364. var permss = RoleDAL.GetTopPermissions();
  365. IList<Option> options = new List<Option>();
  366. foreach (Permission perms in permss)
  367. {
  368. var option = new Option() { label = perms.PermissionName, value = perms.Id.ToString(), disabled = perms.IsDelete };
  369. options.Add(option);
  370. }
  371. var jsonData = JsonConvert.SerializeObject(options);
  372. return Content(jsonData
  373. );
  374. }
  375. [HttpGet, Route("authtype")]
  376. public ActionResult AuthType()
  377. {
  378. IList<Option> options = new List<Option>();
  379. var option = new Option() { label = "界面", value = "U", disabled = false };
  380. options.Add(option);
  381. var option1 = new Option() { label = "操作", value = "O", disabled = false };
  382. options.Add(option1);
  383. var option2 = new Option() { label = "按钮", value = "A", disabled = false };
  384. options.Add(option2);
  385. var jsonData = JsonConvert.SerializeObject(options);
  386. return Content(jsonData
  387. );
  388. }
  389. [HttpGet, Route("permissions")]
  390. public ActionResult Permissions()
  391. {
  392. string roleid = getStaff("roleid");
  393. if (roleid == null || roleid == "")
  394. {
  395. return Json(new { success = false, msg = "没有登陆" });
  396. }
  397. Dictionary<string, Permission> permission = PermissionHelper.Permissions(Convert.ToInt32(roleid));
  398. return Json(permission);
  399. }
  400. /// <summary>
  401. /// 获取所有CM_角色
  402. /// </summary>
  403. /// <returns></returns>
  404. public IList<CM_Role> GetAllRoles()
  405. {
  406. IList<CM_Role> permsList = new List<CM_Role>(0);
  407. DataTable dt = new DataTable();
  408. string result = string.Empty;
  409. var sortDirection = 1;
  410. var pageIndex = 1;
  411. var pageSize = Config.MaxPageSize;
  412. string direct = " desc ";
  413. if (sortDirection != 1)
  414. direct = " asc";
  415. int start = (pageIndex - 1) * pageSize;
  416. int end = (start + 1 + pageSize);
  417. string commandText = "select * from (" +
  418. "select A.*,row_number() over" +
  419. "( order by A.RoleID " + direct + " ) as rownum" +
  420. " from CM_Role A ) AAA" +
  421. " where AAA.rownum>" + start + " and AAA.rownum<" + end +
  422. " ";
  423. List<List<Object>> parameters1 = new List<List<Object>>();
  424. parameters1.Add(new List<Object>() { "RoleId", 0 });
  425. bool result1 = DataAccess.GetValues(commandText, ref dt, DataAccess.ToParameters(parameters1).ToArray(), out result);
  426. if (result1 && dt.Rows.Count > 0)
  427. {
  428. // 把DataTable转换为IList<Role>
  429. permsList = ModelConvertHelper<CM_Role>.ConvertToModel(dt);
  430. }
  431. return permsList;
  432. }
  433. /// <summary>
  434. /// 获取所有"协同"角色
  435. /// </summary>
  436. /// <returns></returns>
  437. public IList<Role> GetAllXTRoles()
  438. {
  439. IList<Role> permsList = new List<Role>(0);
  440. DataTable dt = new DataTable();
  441. string result = string.Empty;
  442. var sortDirection = 1;
  443. var pageIndex = 1;
  444. var pageSize = Config.MaxPageSize;
  445. string direct = " desc ";
  446. if (sortDirection != 1)
  447. direct = " asc";
  448. int start = (pageIndex - 1) * pageSize;
  449. int end = (start + 1 + pageSize);
  450. string commandText = "select * from (" +
  451. "select A.*,row_number() over" +
  452. "( order by A.Id " + direct + " ) as rownum" +
  453. " from sup_Role A ) AAA" +
  454. " where AAA.rownum>" + start + " and AAA.rownum<" + end +
  455. " ";
  456. List<List<Object>> parameters1 = new List<List<Object>>();
  457. parameters1.Add(new List<Object>() { "Id", 0 });
  458. bool result1 = DataAccess.GetValues(commandText, ref dt, DataAccess.ToParameters(parameters1).ToArray(), out result);
  459. if (result1 && dt.Rows.Count > 0)
  460. {
  461. // 把DataTable转换为IList<Role>
  462. permsList = ModelConvertHelper<Role>.ConvertToModel(dt);
  463. }
  464. return permsList;
  465. }
  466. /// <summary>
  467. /// 获取所有组织机构
  468. /// </summary>
  469. /// <returns></returns>
  470. public IList<CM_Org> GetAllOrgs()
  471. {
  472. IList<CM_Org> permsList = new List<CM_Org>(0);
  473. DataTable dt = new DataTable();
  474. string result = string.Empty;
  475. var sortDirection = 1;
  476. var pageIndex = 1;
  477. var pageSize = Config.MaxPageSize;
  478. string direct = " desc ";
  479. if (sortDirection != 1)
  480. direct = " asc";
  481. int start = (pageIndex - 1) * pageSize;
  482. int end = (start + 1 + pageSize);
  483. string commandText = "select * from (" +
  484. "select A.*,row_number() over" +
  485. "( order by A.OrgId " + direct + " ) as rownum" +
  486. " from CM_Org A ) AAA" +
  487. " where AAA.rownum>" + start + " and AAA.rownum<" + end +
  488. " ";
  489. List<List<Object>> parameters1 = new List<List<Object>>();
  490. parameters1.Add(new List<Object>() { "OrgId", 0 });
  491. bool result1 = DataAccess.GetValues(commandText, ref dt, DataAccess.ToParameters(parameters1).ToArray(), out result);
  492. if (result1 && dt.Rows.Count > 0)
  493. {
  494. // 把DataTable转换为IList<Role>
  495. permsList = ModelConvertHelper<CM_Org>.ConvertToModel(dt);
  496. }
  497. return permsList;
  498. }
  499. public static IList<CM_OrgRole> GetRoles()
  500. {
  501. IList<CM_OrgRole> roleList = new List<CM_OrgRole>(0);
  502. DataTable result;
  503. string direct = " desc ";
  504. result = DataAccess.GetDataTable("CM_OrgRole", "Id", "OrgRoleId,RoleName,RoleCode", "", "", "Id" + direct, 1, 100, out var msg);
  505. if (result != null && result.Rows.Count > 0)
  506. {
  507. // 把DataTable转换为IList<Role>
  508. roleList = ModelConvertHelper<CM_OrgRole>.ConvertToModel(result);
  509. }
  510. return roleList;
  511. }
  512. }
  513. }