CM_RoleController.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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/CM_Role")]
  30. public class CM_RoleController : BaseController
  31. {
  32. public CM_RoleController(IMemoryCache cache, IApiClient client) : base(cache, client)
  33. {
  34. }
  35. private ConcurrentDictionary<String, CM_Role> roleMap = new ConcurrentDictionary<String, CM_Role>();
  36. public class TempTableResult
  37. {
  38. public int id { get; set; }
  39. public string permissionName { get; set; }
  40. public string permissionUrl { get; set; }
  41. public int roleId { get; set; }
  42. public int checkedValue { get; set; }
  43. public string permissionRule { get; set; }
  44. public string permissionRuleType { get; set; }
  45. public int relateId { get; set; }
  46. public string AuthType { get; set; }
  47. }
  48. public class TreeNode
  49. {
  50. public string parentValue { get; set; }
  51. public string label { get; set; }
  52. public int value { get; set; }
  53. }
  54. public class TempQuery
  55. {
  56. public int[] data { get; set; }
  57. public int roleId { get; set; }
  58. }
  59. public class SortContent
  60. {
  61. public Object content { get; set; }//权限内容
  62. public string code { get; set; }//权限id
  63. public int sort { 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. //取出参数
  82. if (data != null)
  83. {
  84. filters = data.filters.ToObject<QueryFilter[]>();
  85. pageIndex = data.pageIndex;
  86. pageSize = data.pageSize;
  87. sortField = data.sortField;
  88. sortDirection = data.sortDirection;
  89. sumFields = data.sumFields.ToObject<string[]>();
  90. }
  91. #region 获取角色列表
  92. DataTable dt = new DataTable();
  93. string result;
  94. IList<CM_Role> permss = new List<CM_Role>(0);
  95. List<SqlParameter> parameters = new List<SqlParameter>();
  96. string filterstr = QueryFilter.getFilterSqlParam(filters, out parameters, new CM_Role(),"A.");
  97. string direct = " desc ";
  98. if (sortDirection != 1)
  99. {
  100. direct = " asc";
  101. }
  102. int start = (pageIndex - 1) * pageSize;
  103. int end = (start + 1 + pageSize);
  104. string commandText0 = "select * from ";
  105. string commandText1 = "(" +
  106. "select A.*,row_number() over" +
  107. "( order by A.RoleId " + direct + " ) as rownum from " +
  108. "CM_Role as A " +
  109. " where 1=1 " +
  110. //" and A.CreationPerson = '" + userids + "'" +
  111. filterstr +
  112. ")AAA ";
  113. string commandText2 = " where AAA.rownum>" + start + " and AAA.rownum<" + end;
  114. string commandText3 = commandText0 + commandText1 + commandText2;
  115. bool success = DataAccess.GetValues(commandText3, ref dt, parameters.ToArray(), out result);
  116. #endregion
  117. if (dt != null && dt.Rows.Count > 0)
  118. {
  119. // 把DataTable转换为IList<Permission>
  120. permss = ModelConvertHelper<CM_Role>.ConvertToModel(dt);
  121. #region 获取权限的角色列表,角色列表
  122. //角色列表
  123. IList<CM_Role> roles = GetRoles();
  124. // 把DataTable转换为IList<Role>
  125. if (roleMap.Count < roles.Count)
  126. {
  127. foreach (CM_Role role in roles)
  128. {
  129. roleMap.TryAdd(role.RoleId, role);
  130. }
  131. }
  132. #region
  133. foreach (CM_Role perms in permss)
  134. {
  135. string direct1 = " desc ";
  136. dt = DataAccess.GetDataTable("CM_Role", "RoleId", "*", "RoleId=" + perms.RoleId , "", "RoleId" + direct1, 1, 100, out var msg);
  137. // 把DataTable转换为IList<RoleRelatePermission>
  138. if (dt != null && dt.Rows.Count > 0)
  139. {
  140. IList<CM_Role> permRelates = ModelConvertHelper<CM_Role>.ConvertToModel(dt);
  141. List<CM_Role> listRoles = new List<CM_Role>();
  142. List<int> superior_list = new List<int>(0);
  143. foreach (CM_Role permsr in permRelates)
  144. {
  145. CM_Role role;
  146. if (roleMap.TryGetValue(permsr.RoleId, out role))
  147. {
  148. listRoles.Add(role);
  149. }
  150. superior_list.Add(Convert.ToInt32(permsr.RoleId));
  151. }
  152. //perms.RoleList = listRoles;
  153. perms.RoleId = string.Join(",", superior_list.ToArray());
  154. }
  155. }
  156. #endregion
  157. #endregion
  158. }
  159. string result1;
  160. long totalcount = DataAccess.GetRowCountDefine("select count(RoleId) from " + commandText1, parameters.ToArray(), out result1);
  161. IsoDateTimeConverter timejson = new IsoDateTimeConverter
  162. {
  163. DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"
  164. };
  165. //IList<Menu> menus = Permission.Convert(permss);
  166. var jsonData = JsonConvert.SerializeObject(permss, timejson);
  167. return Json(new
  168. {
  169. items = JsonConvert.DeserializeObject(jsonData),
  170. sum = new { },
  171. totalCount = totalcount
  172. });
  173. }
  174. /// <summary>
  175. /// 新增角色
  176. /// </summary>
  177. /// <param name="RoleId"></param>
  178. /// <param name="RoleCode"></param>
  179. /// <param name="RoleName"></param>
  180. /// <param name="data"></param>
  181. /// <returns></returns>
  182. [AuthPermission]
  183. [HttpPost, Route("add")]
  184. public JsonResult Addp(
  185. String RoleId, String RoleCode,
  186. string RoleName,[FromBody]dynamic data
  187. )
  188. {
  189. if (data != null)
  190. {
  191. RoleId = data.id;
  192. RoleCode = data.RoleCode;
  193. RoleName = data.RoleName;
  194. }
  195. #region 添加角色
  196. String uuid = System.Guid.NewGuid().ToString("N");
  197. string commandText = "INSERT INTO CM_Role (RoleId," +
  198. "RoleCode,RoleName)" +
  199. " VALUES ('" + uuid + "',@RoleCode,@RoleName)";
  200. string result;
  201. //准备参数
  202. List<List<Object>> parameters = new List<List<Object>>();
  203. parameters.Add(new List<Object>() { "RoleId", uuid });
  204. parameters.Add(new List<Object>() { "RoleCode", RoleCode });
  205. parameters.Add(new List<Object>() { "RoleName", RoleName });
  206. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  207. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  208. #endregion
  209. //#region 添加角色
  210. //int id = BaseDAL.GetId(Config.TablePrefix + "Permission");
  211. //RoleDAL.SaveRolesRelatePermission(id, RoleIdis);
  212. //#endregion
  213. return Json(new
  214. {
  215. success = success
  216. });
  217. }
  218. /// <summary>
  219. /// 修改角色
  220. /// </summary>
  221. /// <param name="RoleId"></param>
  222. /// <param name="RoleCode"></param>
  223. /// <param name="RoleName"></param>
  224. /// <param name="data"></param>
  225. /// <returns></returns>
  226. [AuthPermission]
  227. [HttpPost, Route("edit")]
  228. public JsonResult editp(String RoleId, String RoleCode,
  229. string RoleName, [FromBody]dynamic data)
  230. {
  231. if (data != null)
  232. {
  233. RoleId = data.RoleId;
  234. RoleCode = data.RoleCode;
  235. RoleName = data.RoleName;
  236. }
  237. #region 编辑用户
  238. string commandText = "UPDATE CM_Role " +
  239. " SET RoleCode = @RoleCode,RoleName = @RoleName WHERE RoleId= @RoleId";
  240. string result;
  241. //准备参数
  242. List<List<Object>> parameters = new List<List<Object>>();
  243. parameters.Add(new List<Object>() { "RoleCode", RoleCode });
  244. parameters.Add(new List<Object>() { "RoleName", RoleName });
  245. parameters.Add(new List<Object>() { "RoleId", RoleId });
  246. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  247. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  248. #endregion
  249. return Json(new {
  250. success = success
  251. });
  252. }
  253. /// <summary>
  254. /// 删除角色
  255. /// </summary>
  256. /// <param name="id"></param>
  257. /// <returns></returns>
  258. [AuthPermission]
  259. [HttpPost, Route("delete")]
  260. public ActionResult Delete(string id)
  261. {
  262. //准备SQL语句
  263. string commandText = "DELETE FROM CM_Role WHERE RoleId = @RoleId";
  264. string commandText1 = "DELETE FROM CM_OrgRole WHERE RoleId = @RoleId";
  265. //准备参数
  266. List<List<Object>> parameters = new List<List<Object>>();
  267. parameters.Add(new List<Object>() { "RoleId", id });
  268. string result = "";
  269. string result1 = "";
  270. //转换参数
  271. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  272. //执行并返回结果
  273. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  274. int success1 = DataAccess.ExecuteCommand(commandText1, parameters1, out result1);
  275. //如果CM_Role和CM_OrgRole表记录都删除成功
  276. int success2 = 0;
  277. if (success > 0 && success1 > 0)
  278. {
  279. success2 = success;//此处应该是success2 = success+success1,由于不清楚前端是怎么接收处理的,暂且这么处理。
  280. }
  281. else
  282. {
  283. success2 = -1;
  284. }
  285. return Content("{success:" + success2 + "}");
  286. }
  287. [HttpGet, Route("roles")]
  288. public ActionResult Roles()
  289. {
  290. var permss = GetTopRoles();
  291. IList<Option> options = new List<Option>();
  292. foreach (CM_Role perms in permss)
  293. {
  294. var option = new Option() { label = perms.RoleName, value = perms.RoleId.ToString() };
  295. options.Add(option);
  296. }
  297. var jsonData = JsonConvert.SerializeObject(options);
  298. return Content(jsonData);
  299. }
  300. /// <summary>
  301. /// 权限列表
  302. /// </summary>
  303. /// <param name="staffid"></param>
  304. /// <returns></returns>
  305. [HttpGet, Route("menus")]
  306. public ActionResult menus(string staffid)
  307. {
  308. HttpRequest request = HttpContext.Request;
  309. StringValues oo;
  310. request.Headers.TryGetValue("token", out oo);
  311. if (oo.Count > 0 && oo.ToArray()[0] != "")
  312. {
  313. staffid = oo.ToArray()[0];
  314. }
  315. //取token
  316. var token = (string)_cache.Get(staffid);
  317. var secret = TokenConfig.SecretKey;
  318. Dictionary<string, object> data;
  319. Object roleid;
  320. Int64 roleid1;
  321. string jsonData = "";
  322. if (token != null)
  323. {
  324. try
  325. {
  326. data = JsonWebToken.DecodeToObject<Dictionary<string, object>>(token, secret);
  327. data.TryGetValue("roleid", out roleid);
  328. roleid1 = (Int64)roleid;
  329. var options = RoleDAL.GetPermissions(roleid1);
  330. var permissions = Permission.Convert(options);
  331. jsonData = JsonConvert.SerializeObject(permissions);
  332. }
  333. catch (SignatureVerificationException)
  334. {
  335. // Given token is either expired or hashed with an unsupported algorithm.
  336. }
  337. }
  338. return Content(jsonData
  339. );
  340. }
  341. [HttpGet, Route("authtype")]
  342. public ActionResult AuthType()
  343. {
  344. IList<Option> options = new List<Option>();
  345. var option = new Option() { label = "界面", value = "U" ,disabled = false };
  346. options.Add(option);
  347. var option1 = new Option() { label = "操作", value = "O", disabled = false };
  348. options.Add(option1);
  349. var option2 = new Option() { label = "按钮", value = "A", disabled = false };
  350. options.Add(option2);
  351. var jsonData = JsonConvert.SerializeObject(options);
  352. return Content(jsonData
  353. );
  354. }
  355. [HttpGet, Route("permissions")]
  356. public ActionResult Permissions()
  357. {
  358. string roleid = getStaff("roleid");
  359. if(roleid == null || roleid == "")
  360. {
  361. return Json(new { success = false,msg="没有登陆" });
  362. }
  363. Dictionary<string, Permission> permission = PermissionHelper.Permissions(Convert.ToInt32(roleid));
  364. return Json(permission);
  365. }
  366. public IList<CM_Role> GetTopRoles()
  367. {
  368. IList<CM_Role> permsList = new List<CM_Role>(0);
  369. DataTable dt = new DataTable();
  370. string result = string.Empty;
  371. var sortDirection = 1;
  372. var pageIndex = 1;
  373. var pageSize = Config.MaxPageSize;
  374. string direct = " desc ";
  375. if (sortDirection != 1)
  376. direct = " asc";
  377. int start = (pageIndex - 1) * pageSize;
  378. int end = (start + 1 + pageSize);
  379. string commandText = "select * from (" +
  380. "select A.*, row_number() over" +
  381. "( order by A.RoleId " + direct + " ) as rownum" +
  382. " from CM_Role A ) AAA" +
  383. " where AAA.rownum>" + start + " and AAA.rownum<" + end +
  384. " ";
  385. List<List<Object>> parameters1 = new List<List<Object>>();
  386. parameters1.Add(new List<Object>() { "RoleId", 0 });
  387. bool result1 = DataAccess.GetValues(commandText, ref dt, DataAccess.ToParameters(parameters1).ToArray(), out result);
  388. if (result1 && dt.Rows.Count > 0)
  389. {
  390. // 把DataTable转换为IList<Role>
  391. permsList = ModelConvertHelper<CM_Role>.ConvertToModel(dt);
  392. }
  393. return permsList;
  394. }
  395. public static IList<CM_Role> GetRoles()
  396. {
  397. IList<CM_Role> roleList = new List<CM_Role>(0);
  398. DataTable result;
  399. string direct = " desc ";
  400. result = DataAccess.GetDataTable("CM_Role", "Id", "RoleId,RoleName,RoleCode", "", "", "Id" + direct, 1, 100, out var msg);
  401. if (result != null && result.Rows.Count > 0)
  402. {
  403. // 把DataTable转换为IList<Role>
  404. roleList = ModelConvertHelper<CM_Role>.ConvertToModel(result);
  405. }
  406. return roleList;
  407. }
  408. }
  409. }