RoleController.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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/Role")]
  30. public class RoleController : BaseController
  31. {
  32. public RoleController(IMemoryCache cache, IApiClient client) : base(cache, client)
  33. {
  34. }
  35. private ConcurrentDictionary<String, Role> roleMap = new ConcurrentDictionary<String, 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 Id { 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 TempRoleResult
  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 Id { 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<Role> permss = new List<Role>(0);
  95. List<SqlParameter> parameters = new List<SqlParameter>();
  96. string filterstr = QueryFilter.getFilterSqlParam(filters, out parameters, new 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.Id " + direct + " ) as rownum from " +
  108. "SUP_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<Role>.ConvertToModel(dt);
  121. #region 获取权限的角色列表,角色列表
  122. //角色列表
  123. IList<Role> roles = GetRoles();
  124. // 把DataTable转换为IList<Role>
  125. if (roleMap.Count < roles.Count)
  126. {
  127. foreach (Role role in roles)
  128. {
  129. roleMap.TryAdd(role.Id.ToString(), role);
  130. }
  131. }
  132. #region
  133. foreach (Role perms in permss)
  134. {
  135. string direct1 = " desc ";
  136. dt = DataAccess.GetDataTable("Role", "Id", "*", "Id=" + perms.Id, "", "Id" + direct1, 1, 100, out var msg);
  137. // 把DataTable转换为IList<RoleRelatePermission>
  138. if (dt != null && dt.Rows.Count > 0)
  139. {
  140. IList<Role> permRelates = ModelConvertHelper<Role>.ConvertToModel(dt);
  141. List<Role> listRoles = new List<Role>();
  142. List<int> superior_list = new List<int>(0);
  143. foreach (Role permsr in permRelates)
  144. {
  145. Role role;
  146. if (roleMap.TryGetValue(permsr.Id.ToString(), out role))
  147. {
  148. listRoles.Add(role);
  149. }
  150. superior_list.Add(Convert.ToInt32(permsr.Id));
  151. }
  152. //perms.RoleList = listRoles;
  153. perms.Id = Convert.ToInt32(string.Join(",", superior_list.ToArray()));
  154. }
  155. }
  156. #endregion
  157. #endregion
  158. }
  159. string result1;
  160. long totalcount = DataAccess.GetRowCountDefine("select count(Id) 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="Id"></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 Id, String RoleCode,
  186. string RoleName, [FromBody]dynamic data
  187. )
  188. {
  189. int RoleDepartmentId = 0;
  190. string RoleRemark = "";
  191. int IsDelete = 0;
  192. int CreationPerson = 0;
  193. DateTime CreationDate = Convert.ToDateTime("1900-01-01 00:00:00");
  194. int LastModifiedPerson = 0;
  195. DateTime LastModified = Convert.ToDateTime("1900-01-01 00:00:00");
  196. if (data != null)
  197. {
  198. Id = data.id;
  199. RoleName = data.RoleName;
  200. //RoleDepartmentId = data.RoleDepartmentId;
  201. RoleRemark = data.RoleRemark;
  202. if (data.IsDelete != null) { IsDelete = data.IsDelete; }
  203. if (data.CreationPerson != null) { CreationPerson = data.CreationPerson; }
  204. if (data.CreationDate != null) { CreationDate = data.CreationDate; }
  205. if (data.LastModifiedPerson != null) { LastModifiedPerson = data.LastModifiedPerson; }
  206. if (data.LastModified != null) {LastModified = data.LastModified; }
  207. }
  208. #region 添加角色
  209. object rid = "";
  210. string sql = "SELECT MAX(Id) AS ID from sup_role";
  211. string result2 = "";
  212. List<SqlParameter> parameters3 = new List<SqlParameter>();
  213. bool success1 = DataAccess.GetOneValue(sql,parameters3.ToArray(),out rid,out result2);
  214. RoleDepartmentId = Convert.ToInt32(rid.ToString());
  215. //String uuid = System.Guid.NewGuid().ToString("N");
  216. string commandText = "INSERT INTO SUP_ROLE (" +
  217. "RoleName,RoleDepartmentId,RoleRemark,IsDelete,CreationPerson," +
  218. "CreationDate,LastModifiedPerson,LastModified)" +
  219. " VALUES (@RoleName,@RoleDepartmentId,@RoleRemark,@IsDelete," +
  220. "@CreationPerson,@CreationDate,@LastModifiedPerson,@LastModified)";
  221. string result;
  222. //准备参数
  223. List<List<Object>> parameters = new List<List<Object>>();
  224. //parameters.Add(new List<Object>() { "Id", rrid });
  225. parameters.Add(new List<Object>() { "RoleName", RoleName });
  226. parameters.Add(new List<Object>() { "RoleDepartmentId", RoleDepartmentId });
  227. parameters.Add(new List<Object>() { "RoleRemark", RoleRemark });
  228. parameters.Add(new List<Object>() { "IsDelete", IsDelete });
  229. parameters.Add(new List<Object>() { "CreationPerson", CreationPerson });
  230. parameters.Add(new List<Object>() { "CreationDate", CreationDate });
  231. parameters.Add(new List<Object>() { "LastModifiedPerson", LastModifiedPerson });
  232. parameters.Add(new List<Object>() { "LastModified", LastModified });
  233. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  234. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  235. #endregion
  236. return Json(new
  237. {
  238. success = success
  239. });
  240. }
  241. /// <summary>
  242. /// 修改角色
  243. /// </summary>
  244. /// <param name="Id"></param>
  245. /// <param name="RoleCode"></param>
  246. /// <param name="RoleName"></param>
  247. /// <param name="data"></param>
  248. /// <returns></returns>
  249. [AuthPermission]
  250. [HttpPost, Route("edit")]
  251. public JsonResult editp(String Id, String RoleCode,
  252. string RoleName, [FromBody]dynamic data)
  253. {
  254. int RoleDepartmentId = 0;
  255. string RoleRemark = "";
  256. int IsDelete = 0;
  257. int CreationPerson = 0;
  258. DateTime CreationDate = Convert.ToDateTime("1900-01-01 00:00:00");
  259. int LastModifiedPerson = 0;
  260. DateTime LastModified = Convert.ToDateTime("1900-01-01 00:00:00");
  261. if (data != null)
  262. {
  263. Id = data.Id;
  264. RoleName = data.RoleName;
  265. RoleDepartmentId = data.RoleDepartmentId;
  266. RoleRemark = data.RoleRemark;
  267. if (data.IsDelete != null) { IsDelete = data.IsDelete; }
  268. if (data.CreationPerson != null) { CreationPerson = data.CreationPerson; }
  269. if (data.CreationDate != null) { CreationDate = data.CreationDate; }
  270. if (data.LastModifiedPerson != null) { LastModifiedPerson = data.LastModifiedPerson; }
  271. if (data.LastModified != null) { LastModified = data.LastModified; }
  272. }
  273. #region 编辑用户
  274. string commandText = "UPDATE SUP_Role " +
  275. " SET RoleName = @RoleName,RoleDepartmentId = @RoleDepartmentId,RoleRemark = @RoleRemark," +
  276. "IsDelete = @IsDelete,CreationPerson = @CreationPerson,CreationDate = @CreationDate," +
  277. "LastModifiedPerson = @LastModifiedPerson,LastModified = @LastModified " +
  278. "WHERE Id= @Id";
  279. string result;
  280. //准备参数
  281. List<List<Object>> parameters = new List<List<Object>>();
  282. parameters.Add(new List<Object>() { "Id", Id });
  283. parameters.Add(new List<Object>() { "RoleName", RoleName });
  284. parameters.Add(new List<Object>() { "RoleDepartmentId", RoleDepartmentId });
  285. parameters.Add(new List<Object>() { "RoleRemark", RoleRemark });
  286. parameters.Add(new List<Object>() { "IsDelete", IsDelete });
  287. parameters.Add(new List<Object>() { "CreationPerson", CreationPerson });
  288. parameters.Add(new List<Object>() { "CreationDate", CreationDate });
  289. parameters.Add(new List<Object>() { "LastModifiedPerson", LastModifiedPerson });
  290. parameters.Add(new List<Object>() { "LastModified", LastModified });
  291. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  292. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  293. #endregion
  294. return Json(new
  295. {
  296. success = success
  297. });
  298. }
  299. /// <summary>
  300. /// 删除角色
  301. /// </summary>
  302. /// <param name="id"></param>
  303. /// <returns></returns>
  304. [AuthPermission]
  305. [HttpPost, Route("delete")]
  306. public ActionResult Delete(string Id)
  307. {
  308. //准备SQL语句
  309. string commandText = "DELETE FROM SUP_Role WHERE Id = @Id";
  310. //准备参数
  311. List<List<Object>> parameters = new List<List<Object>>();
  312. parameters.Add(new List<Object>() { "Id", Id });
  313. string result = "";
  314. //转换参数
  315. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  316. //执行并返回结果
  317. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  318. return Content("{success:" + success + "}");
  319. }
  320. [HttpGet, Route("roles")]
  321. public ActionResult Roles()
  322. {
  323. var permss = GetTopRoles();
  324. IList<Option> options = new List<Option>();
  325. foreach (Role perms in permss)
  326. {
  327. var option = new Option() { label = perms.RoleName, value = perms.Id.ToString() };
  328. options.Add(option);
  329. }
  330. var jsonData = JsonConvert.SerializeObject(options);
  331. return Content(jsonData);
  332. }
  333. /// <summary>
  334. /// 权限列表
  335. /// </summary>
  336. /// <param name="staffid"></param>
  337. /// <returns></returns>
  338. [HttpGet, Route("menus")]
  339. public ActionResult menus(string staffid)
  340. {
  341. HttpRequest request = HttpContext.Request;
  342. StringValues oo;
  343. request.Headers.TryGetValue("token", out oo);
  344. if (oo.Count > 0 && oo.ToArray()[0] != "")
  345. {
  346. staffid = oo.ToArray()[0];
  347. }
  348. //取token
  349. var token = (string)_cache.Get(staffid);
  350. var secret = TokenConfig.SecretKey;
  351. Dictionary<string, object> data;
  352. Object Id;
  353. Int64 Id1;
  354. string jsonData = "";
  355. if (token != null)
  356. {
  357. try
  358. {
  359. data = JsonWebToken.DecodeToObject<Dictionary<string, object>>(token, secret);
  360. data.TryGetValue("Id", out Id);
  361. Id1 = (Int64)Id;
  362. var options = RoleDAL.GetPermissions(Id1);
  363. var permissions = Permission.Convert(options);
  364. jsonData = JsonConvert.SerializeObject(permissions);
  365. }
  366. catch (SignatureVerificationException)
  367. {
  368. // Given token is either expired or hashed with an unsupported algorithm.
  369. }
  370. }
  371. return Content(jsonData
  372. );
  373. }
  374. [HttpGet, Route("authtype")]
  375. public ActionResult AuthType()
  376. {
  377. IList<Option> options = new List<Option>();
  378. var option = new Option() { label = "界面", value = "U", disabled = false };
  379. options.Add(option);
  380. var option1 = new Option() { label = "操作", value = "O", disabled = false };
  381. options.Add(option1);
  382. var option2 = new Option() { label = "按钮", value = "A", disabled = false };
  383. options.Add(option2);
  384. var jsonData = JsonConvert.SerializeObject(options);
  385. return Content(jsonData
  386. );
  387. }
  388. [HttpGet, Route("permissions")]
  389. public ActionResult Permissions()
  390. {
  391. string Id = getStaff("Id");
  392. if (Id == null || Id == "")
  393. {
  394. return Json(new { success = false, msg = "没有登陆" });
  395. }
  396. Dictionary<string, Permission> permission = PermissionHelper.Permissions(Convert.ToInt32(Id));
  397. return Json(permission);
  398. }
  399. public IList<Role> GetTopRoles()
  400. {
  401. IList<Role> permsList = new List<Role>(0);
  402. DataTable dt = new DataTable();
  403. string result = string.Empty;
  404. var sortDirection = 1;
  405. var pageIndex = 1;
  406. var pageSize = Config.MaxPageSize;
  407. string direct = " desc ";
  408. if (sortDirection != 1)
  409. direct = " asc";
  410. int start = (pageIndex - 1) * pageSize;
  411. int end = (start + 1 + pageSize);
  412. string commandText = "select * from (" +
  413. "select A.*, row_number() over" +
  414. "( order by A.Id " + direct + " ) as rownum" +
  415. " from Role A ) AAA" +
  416. " where AAA.rownum>" + start + " and AAA.rownum<" + end +
  417. " ";
  418. List<List<Object>> parameters1 = new List<List<Object>>();
  419. parameters1.Add(new List<Object>() { "Id", 0 });
  420. bool result1 = DataAccess.GetValues(commandText, ref dt, DataAccess.ToParameters(parameters1).ToArray(), out result);
  421. if (result1 && dt.Rows.Count > 0)
  422. {
  423. // 把DataTable转换为IList<Role>
  424. permsList = ModelConvertHelper<Role>.ConvertToModel(dt);
  425. }
  426. return permsList;
  427. }
  428. public static IList<Role> GetRoles()
  429. {
  430. IList<Role> roleList = new List<Role>(0);
  431. DataTable result;
  432. string direct = " desc ";
  433. result = DataAccess.GetDataTable("Role", "Id", "Id,RoleName,RoleCode", "", "", "Id" + direct, 1, 100, out var msg);
  434. if (result != null && result.Rows.Count > 0)
  435. {
  436. // 把DataTable转换为IList<Role>
  437. roleList = ModelConvertHelper<Role>.ConvertToModel(result);
  438. }
  439. return roleList;
  440. }
  441. }
  442. }