RoleController.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 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 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. Config.TablePrefix+"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 "
  161. + commandText1, parameters.ToArray(), out result1);
  162. IsoDateTimeConverter timejson = new IsoDateTimeConverter
  163. {
  164. DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"
  165. };
  166. //IList<Menu> menus = Permission.Convert(permss);
  167. var jsonData = JsonConvert.SerializeObject(permss, timejson);
  168. return Json(new
  169. {
  170. items = JsonConvert.DeserializeObject(jsonData),
  171. sum = new { },
  172. totalCount = totalcount
  173. });
  174. }
  175. /// <summary>
  176. /// 新增角色
  177. /// </summary>
  178. /// <param name="Id"></param>
  179. /// <param name="RoleCode"></param>
  180. /// <param name="RoleName"></param>
  181. /// <param name="data"></param>
  182. /// <returns></returns>
  183. [AuthPermission]
  184. [HttpPost, Route("add")]
  185. public JsonResult Addp(
  186. String Id, String RoleCode,
  187. string RoleName, [FromBody]dynamic data
  188. )
  189. {
  190. int RoleDepartmentId = 0;
  191. string RoleRemark = "";
  192. int IsDelete = 0;
  193. int CreationPerson = 0;
  194. DateTime CreationDate = DateTime.Now;
  195. int LastModifiedPerson = 0;
  196. DateTime LastModified = DateTime.Now;
  197. if (data != null)
  198. {
  199. Id = data.id;
  200. RoleName = data.RoleName;
  201. //RoleDepartmentId = data.RoleDepartmentId;
  202. RoleRemark = data.RoleRemark;
  203. if (data.IsDelete != null) { IsDelete = data.IsDelete; }
  204. if (data.CreationPerson != null) { CreationPerson = data.CreationPerson; }
  205. if (data.CreationDate != null) { CreationDate = data.CreationDate; }
  206. if (data.LastModifiedPerson != null) { LastModifiedPerson = data.LastModifiedPerson; }
  207. if (data.LastModified != null) {LastModified = data.LastModified; }
  208. }
  209. #region 添加角色
  210. object rid = "";
  211. string sql = "SELECT MAX(Id) AS ID from " + Config.TablePrefix + "role";
  212. string result2 = "";
  213. List<SqlParameter> parameters3 = new List<SqlParameter>();
  214. bool success1 = DataAccess.GetOneValue(sql,parameters3.ToArray(),out rid,out result2);
  215. RoleDepartmentId = Convert.ToInt32(rid.ToString());
  216. //String uuid = System.Guid.NewGuid().ToString("N");
  217. string commandText = "INSERT INTO " + Config.TablePrefix + "ROLE (" +
  218. "RoleName,RoleDepartmentId,RoleRemark,IsDelete,CreationPerson," +
  219. "CreationDate,LastModifiedPerson,LastModified)" +
  220. " VALUES (@RoleName,@RoleDepartmentId,@RoleRemark,@IsDelete," +
  221. "@CreationPerson,@CreationDate,@LastModifiedPerson,@LastModified)";
  222. string result;
  223. //准备参数
  224. List<List<Object>> parameters = new List<List<Object>>();
  225. //parameters.Add(new List<Object>() { "Id", rrid });
  226. parameters.Add(new List<Object>() { "RoleName", RoleName });
  227. parameters.Add(new List<Object>() { "RoleDepartmentId", RoleDepartmentId });
  228. parameters.Add(new List<Object>() { "RoleRemark", RoleRemark });
  229. parameters.Add(new List<Object>() { "IsDelete", IsDelete });
  230. parameters.Add(new List<Object>() { "CreationPerson", CreationPerson });
  231. parameters.Add(new List<Object>() { "CreationDate", CreationDate });
  232. parameters.Add(new List<Object>() { "LastModifiedPerson", LastModifiedPerson });
  233. parameters.Add(new List<Object>() { "LastModified", LastModified });
  234. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  235. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  236. #endregion
  237. return Json(new
  238. {
  239. success = success
  240. });
  241. }
  242. /// <summary>
  243. /// 修改角色
  244. /// </summary>
  245. /// <param name="Id"></param>
  246. /// <param name="RoleCode"></param>
  247. /// <param name="RoleName"></param>
  248. /// <param name="data"></param>
  249. /// <returns></returns>
  250. [AuthPermission]
  251. [HttpPost, Route("edit")]
  252. public JsonResult editp(String Id, String RoleCode,
  253. string RoleName, [FromBody]dynamic data)
  254. {
  255. int RoleDepartmentId = 0;
  256. string RoleRemark = "";
  257. int IsDelete = 0;
  258. int CreationPerson = 0;
  259. DateTime CreationDate = DateTime.Now;
  260. int LastModifiedPerson = 0;
  261. DateTime LastModified = DateTime.Now;
  262. if (data != null)
  263. {
  264. Id = data.Id;
  265. RoleName = data.RoleName;
  266. RoleDepartmentId = data.RoleDepartmentId;
  267. RoleRemark = data.RoleRemark;
  268. if (data.IsDelete != null) { IsDelete = data.IsDelete; }
  269. if (data.CreationPerson != null) { CreationPerson = data.CreationPerson; }
  270. if (data.CreationDate != null) { CreationDate = data.CreationDate; }
  271. }
  272. #region 编辑用户
  273. string commandText = "UPDATE "+Config.TablePrefix+"Role " +
  274. " SET RoleName = @RoleName,RoleDepartmentId = @RoleDepartmentId,RoleRemark = @RoleRemark," +
  275. "IsDelete = @IsDelete," +
  276. //"CreationPerson = @CreationPerson," +
  277. //"CreationDate = @CreationDate," +
  278. "LastModifiedPerson = @LastModifiedPerson,LastModified = @LastModified " +
  279. "WHERE Id= @Id";
  280. string result;
  281. //准备参数
  282. List<List<Object>> parameters = new List<List<Object>>();
  283. parameters.Add(new List<Object>() { "Id", Id });
  284. parameters.Add(new List<Object>() { "RoleName", RoleName });
  285. parameters.Add(new List<Object>() { "RoleDepartmentId", RoleDepartmentId });
  286. parameters.Add(new List<Object>() { "RoleRemark", RoleRemark });
  287. parameters.Add(new List<Object>() { "IsDelete", IsDelete });
  288. //parameters.Add(new List<Object>() { "CreationPerson", CreationPerson });
  289. //parameters.Add(new List<Object>() { "CreationDate", CreationDate });
  290. parameters.Add(new List<Object>() { "LastModifiedPerson", LastModifiedPerson });
  291. parameters.Add(new List<Object>() { "LastModified", LastModified });
  292. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  293. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  294. #endregion
  295. return Json(new
  296. {
  297. success = success
  298. });
  299. }
  300. /// <summary>
  301. /// 删除角色
  302. /// </summary>
  303. /// <param name="id"></param>
  304. /// <returns></returns>
  305. [AuthPermission]
  306. [HttpPost, Route("delete")]
  307. public ActionResult Delete(string Id)
  308. {
  309. //准备SQL语句
  310. string commandText = "DELETE FROM " + Config.TablePrefix + "Role WHERE Id = @Id";
  311. //准备参数
  312. List<List<Object>> parameters = new List<List<Object>>();
  313. parameters.Add(new List<Object>() { "Id", Id });
  314. string result = "";
  315. //转换参数
  316. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  317. //执行并返回结果
  318. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  319. return Content("{success:" + success + "}");
  320. }
  321. [HttpGet, Route("roles")]
  322. public ActionResult Roles()
  323. {
  324. var permss = GetTopRoles();
  325. IList<Option> options = new List<Option>();
  326. foreach (Role perms in permss)
  327. {
  328. var option = new Option() { label = perms.RoleName, value = perms.Id.ToString() };
  329. options.Add(option);
  330. }
  331. var jsonData = JsonConvert.SerializeObject(options);
  332. return Content(jsonData);
  333. }
  334. /// <summary>
  335. /// 权限列表
  336. /// </summary>
  337. /// <param name="staffid"></param>
  338. /// <returns></returns>
  339. [HttpGet, Route("menus")]
  340. public ActionResult menus(string staffid)
  341. {
  342. HttpRequest request = HttpContext.Request;
  343. StringValues oo;
  344. request.Headers.TryGetValue("token", out oo);
  345. if (oo.Count > 0 && oo.ToArray()[0] != "")
  346. {
  347. staffid = oo.ToArray()[0];
  348. }
  349. //取token
  350. var token = (string)_cache.Get(staffid);
  351. var secret = TokenConfig.SecretKey;
  352. Dictionary<string, object> data;
  353. Object Id;
  354. Int64 Id1;
  355. string jsonData = "";
  356. if (token != null)
  357. {
  358. try
  359. {
  360. data = JsonWebToken.DecodeToObject<Dictionary<string, object>>(token, secret);
  361. data.TryGetValue("Id", out Id);
  362. Id1 = (Int64)Id;
  363. var options = RoleDAL.GetPermissions(Id1);
  364. var permissions = Permission.Convert(options);
  365. jsonData = JsonConvert.SerializeObject(permissions);
  366. }
  367. catch (SignatureVerificationException)
  368. {
  369. // Given token is either expired or hashed with an unsupported algorithm.
  370. }
  371. }
  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 Id = getStaff("Id");
  393. if (Id == null || Id == "")
  394. {
  395. return Json(new { success = false, msg = "没有登陆" });
  396. }
  397. Dictionary<string, Permission> permission = PermissionHelper.Permissions(Convert.ToInt32(Id));
  398. return Json(permission);
  399. }
  400. public IList<Role> GetTopRoles()
  401. {
  402. IList<Role> permsList = new List<Role>(0);
  403. DataTable dt = new DataTable();
  404. string result = string.Empty;
  405. var sortDirection = 1;
  406. var pageIndex = 1;
  407. var pageSize = Config.MaxPageSize;
  408. string direct = " desc ";
  409. if (sortDirection != 1)
  410. direct = " asc";
  411. int start = (pageIndex - 1) * pageSize;
  412. int end = (start + 1 + pageSize);
  413. string commandText = "select * from (" +
  414. "select A.*, row_number() over" +
  415. "( order by A.Id " + direct + " ) as rownum" +
  416. " from Role A ) AAA" +
  417. " where AAA.rownum>" + start + " and AAA.rownum<" + end +
  418. " ";
  419. List<List<Object>> parameters1 = new List<List<Object>>();
  420. parameters1.Add(new List<Object>() { "Id", 0 });
  421. bool result1 = DataAccess.GetValues(commandText, ref dt, DataAccess.ToParameters(parameters1).ToArray(), out result);
  422. if (result1 && dt.Rows.Count > 0)
  423. {
  424. // 把DataTable转换为IList<Role>
  425. permsList = ModelConvertHelper<Role>.ConvertToModel(dt);
  426. }
  427. return permsList;
  428. }
  429. public static IList<Role> GetRoles()
  430. {
  431. IList<Role> roleList = new List<Role>(0);
  432. DataTable result;
  433. string direct = " desc ";
  434. result = DataAccess.GetDataTable("Role", "Id", "Id,RoleName,RoleCode", "", "", "Id" + direct, 1, 100, out var msg);
  435. if (result != null && result.Rows.Count > 0)
  436. {
  437. // 把DataTable转换为IList<Role>
  438. roleList = ModelConvertHelper<Role>.ConvertToModel(result);
  439. }
  440. return roleList;
  441. }
  442. }
  443. }