PermissionController.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. using SupplierWeb.Commonss;
  28. using System.Web;
  29. using System.Runtime.Serialization.Json;
  30. using System.Text;
  31. using System.IO;
  32. using System.Runtime.Serialization;
  33. namespace SupplierWeb.Controllers
  34. {
  35. [Route("web/auth")]
  36. public class PermissionController : BaseController
  37. {
  38. public PermissionController(IMemoryCache cache, IApiClient client) : base(cache, client)
  39. {
  40. }
  41. private ConcurrentDictionary<int, Role> roleMap = new ConcurrentDictionary<int, Role>();
  42. /// 供应商,采购,供应商业务员挂靠,资料管理员,财务,仓库 等角色权限列表
  43. /// 权限列表
  44. //[HttpGet, Route("index")]
  45. //public ActionResult index(string[] filters, Int32 pageIndex, Int32 pageSize,
  46. // string sortField, Int32 sortDirection, string[] sumFields, string callback)
  47. //{
  48. // var r = this.indexp(filters, pageIndex, pageSize,
  49. // sortField, sortDirection, sumFields, null);
  50. // return Content(callback + "(" + JsonConvert.SerializeObject(r.Value)
  51. // + ")");
  52. //}
  53. /// 供应商,采购,供应商业务员挂靠,资料管理员,财务,仓库 等角色权限列表
  54. /// 权限列表
  55. [HttpPost, Route("index")]
  56. public JsonResult indexp(QueryFilter[] filters, Int32 pageIndex, Int32 pageSize,
  57. string sortField, Int32 sortDirection, string[] sumFields, [FromBody]dynamic data)
  58. {
  59. if (data != null)
  60. {
  61. //Newtonsoft.Json.Linq.JArray
  62. filters = data.filters.ToObject<QueryFilter[]>();
  63. pageIndex = data.pageIndex;
  64. pageSize = data.pageSize;
  65. sortField = data.sortField;
  66. sortDirection = data.sortDirection;
  67. sumFields = data.sumFields.ToObject<string[]>();
  68. }
  69. #region 获取权限列表
  70. DataTable dt = new DataTable();
  71. string result;
  72. IList<Permission> permss = new List<Permission>(0);
  73. List<SqlParameter> parameters = new List<SqlParameter>();
  74. string filterstr = QueryFilter.getFilterSqlParam(filters, out parameters, new Permission(), "A.");
  75. string direct = " desc ";
  76. if (sortDirection != 1)
  77. direct = " asc";
  78. int start = (pageIndex - 1) * pageSize;
  79. int end = (start + 1 + pageSize);
  80. string commandText0 = "select * from ";
  81. string commandText1 = "(" +
  82. "select A.*,row_number() over" +
  83. "( order by " + sortField + " " + direct + " ) as rownum from " +
  84. Config.TablePrefix + "Permission as A " +
  85. " where 1=1 " +
  86. //" and A.CreationPerson = '" + userids + "'" +
  87. filterstr +
  88. ")AAA ";
  89. string commandText2 = " where AAA.rownum>" + start + " and AAA.rownum<" + end;
  90. string commandText3 = commandText0 + commandText1 + commandText2;//-tanyang
  91. bool success = DataAccess.GetValues(commandText3, ref dt, parameters.ToArray(), out result);
  92. #endregion
  93. if (dt != null && dt.Rows.Count > 0)
  94. {
  95. // 把DataTable转换为IList<Permission>
  96. permss = ModelConvertHelper<Permission>.ConvertToModel(dt);
  97. #region 获取权限的角色列表,角色列表
  98. //角色列表
  99. IList<Role> roles = RoleDAL.GetRoles();
  100. // 把DataTable转换为IList<Role>
  101. if (roleMap.Count < roles.Count)
  102. {
  103. foreach (Role role in roles)
  104. {
  105. roleMap.TryAdd(role.Id, role);
  106. }
  107. }
  108. foreach (Permission perms in permss)
  109. {
  110. string direct1 = " desc ";
  111. dt = DataAccess.GetDataTable(Config.TablePrefix + "RoleRelatePermission", "Id", "*", "PermissionId=" + perms.Id + " and IsDelete = 0 ", "", "Id" + direct1, 1, 100, out var msg);
  112. // 把DataTable转换为IList<RoleRelatePermission>
  113. if (dt != null && dt.Rows.Count > 0)
  114. {
  115. IList<RoleRelatePermission> permRelates = ModelConvertHelper<RoleRelatePermission>.ConvertToModel(dt);
  116. //List<Role> listRoles = new List<Role>();
  117. List<int> superior_list = new List<int>(0);
  118. foreach (RoleRelatePermission permsr in permRelates)
  119. {
  120. //Role role;
  121. //if (roleMap.TryGetValue(permsr.RoleId, out role))
  122. //{
  123. // listRoles.Add(role);
  124. //}
  125. superior_list.Add(permsr.RoleId);
  126. }
  127. //perms.RoleList = listRoles;
  128. perms.Roleids = string.Join(",", superior_list.ToArray());
  129. }
  130. }
  131. #endregion
  132. }
  133. string result1;
  134. long totalcount = DataAccess.GetRowCountDefine("select count(Id) from " + commandText1, parameters.ToArray(), out result1);
  135. IsoDateTimeConverter timejson = new IsoDateTimeConverter
  136. {
  137. DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"
  138. };
  139. //IList<Menu> menus = Permission.Convert(permss);
  140. var jsonData = JsonConvert.SerializeObject(permss, timejson);
  141. return Json(new
  142. {
  143. items = JsonConvert.DeserializeObject(jsonData),
  144. sum = new { },
  145. totalCount = totalcount
  146. });
  147. }
  148. [AuthPermission]
  149. [HttpPost, Route("add")]
  150. public JsonResult addp(
  151. string PermissionName, string PermissionUrl,
  152. string AuthType, Int32 ActionType,
  153. string Icon
  154. , string ParentId, string Roleids, [FromBody]dynamic data
  155. , Int32 IsDelete = 0
  156. )
  157. {
  158. if (data != null)
  159. {
  160. //Newtonsoft.Json.Linq.JArray
  161. PermissionName = data.PermissionName;
  162. PermissionUrl = data.PermissionUrl;
  163. AuthType = data.AuthType;
  164. if (ActionType != 0)
  165. {
  166. ActionType = data.ActionType;
  167. }
  168. Icon = data.Icon;
  169. ParentId = data.ParentId;
  170. Roleids = data.Roleids;
  171. IsDelete = (data.IsDelete == null) ? 0 : data.IsDelete;
  172. }
  173. string[] RoleIdss = Roleids.Split(",");
  174. int[] RoleIdis = Array.ConvertAll(RoleIdss, s => Convert.ToInt32(s));
  175. #region 添加权限节点
  176. string commandText = "INSERT INTO " + Config.TablePrefix + "Permission (permissionname,permissionurl," +
  177. "AuthType,ActionType," +
  178. "Icon,ParentId," +
  179. "isdelete)"
  180. + " VALUES (@permissionname,@permissionurl,@AuthType,@ActionType,@Icon,@ParentId,@isdelete)";
  181. string result;
  182. //准备参数
  183. List<List<Object>> parameters = new List<List<Object>>();
  184. parameters.Add(new List<Object>() { "permissionname", PermissionName });
  185. parameters.Add(new List<Object>() { "permissionurl", PermissionUrl });
  186. parameters.Add(new List<Object>() { "AuthType", AuthType });
  187. parameters.Add(new List<Object>() { "ActionType", ActionType });
  188. parameters.Add(new List<Object>() { "Icon", Icon });
  189. parameters.Add(new List<Object>() { "ParentId", ParentId != null ? Convert.ToInt32(ParentId) : 0 });
  190. parameters.Add(new List<Object>() { "isdelete", IsDelete });
  191. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  192. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  193. #endregion
  194. #region 添加角色
  195. int id = BaseDAL.GetId(Config.TablePrefix + "Permission");
  196. RoleDAL.SaveRolesRelatePermission(id, RoleIdis);
  197. #endregion
  198. return Json(new
  199. {
  200. success = success
  201. });
  202. }
  203. ////编辑权限
  204. //[HttpGet, Route("edit")]
  205. //public ActionResult edit(Int32 Id,
  206. // string PermissionName, string PermissionUrl, string Icon
  207. // , string ParentId, string Roleids
  208. // , string callback, Int32 IsDelete
  209. // )
  210. //{
  211. // var r = this.editp(Id, PermissionName, PermissionUrl, Icon
  212. // , ParentId, Roleids, null
  213. // , IsDelete);
  214. // return Content(callback + "(" + JsonConvert.SerializeObject(r.Value)
  215. // + ")");
  216. //}
  217. [AuthPermission]
  218. [HttpPost, Route("edit")]
  219. public JsonResult editp(Int32 Id,
  220. string PermissionName, string PermissionUrl,
  221. string AuthType,
  222. string Icon,
  223. string ParentId, string Roleids,
  224. [FromBody]dynamic data
  225. , Int32 IsDelete
  226. )
  227. {
  228. if (data != null)
  229. {
  230. //Newtonsoft.Json.Linq.JArray
  231. Id = data.Id;
  232. PermissionName = data.PermissionName;
  233. PermissionUrl = data.PermissionUrl;
  234. AuthType = data.AuthType;
  235. Icon = data.Icon;
  236. ParentId = data.ParentId;
  237. Roleids = data.Roleids;
  238. IsDelete = data.IsDelete == null ? 0 : data.IsDelete;
  239. }
  240. string[] RoleIdss = Roleids.Split(",");
  241. int[] RoleIdis = Array.ConvertAll(RoleIdss, s => Convert.ToInt32(s));
  242. #region 编辑用户
  243. string commandText = "UPDATE " + Config.TablePrefix + "Permission " +
  244. " SET permissionname = @permissionname,";
  245. if (PermissionUrl != null)
  246. commandText += "permissionurl = @permissionurl,";
  247. commandText += " ParentId = @ParentId" +
  248. ",Icon = @Icon" +
  249. " ,isdelete = @isdelete" +
  250. ",AuthType = @AuthType" +
  251. " WHERE id= @Id";
  252. string result;
  253. //准备参数
  254. List<List<Object>> parameters = new List<List<Object>>();
  255. parameters.Add(new List<Object>() { "permissionname", PermissionName });
  256. parameters.Add(new List<Object>() { "permissionurl", PermissionUrl });
  257. parameters.Add(new List<Object>() { "AuthType", AuthType });
  258. parameters.Add(new List<Object>() { "ParentId", ParentId });
  259. parameters.Add(new List<Object>() { "Icon", Icon });
  260. parameters.Add(new List<Object>() { "isdelete", IsDelete });
  261. parameters.Add(new List<Object>() { "Id", Id });
  262. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  263. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  264. #endregion
  265. #region 角色
  266. RoleDAL.SaveRolesRelatePermission(Id, RoleIdis);
  267. #endregion
  268. return Json(new
  269. {
  270. success = success
  271. });
  272. }
  273. ////删除权限
  274. [AuthPermission]
  275. [HttpGet, Route("delete/{0}")]
  276. public ActionResult Delete(Int32 Id, string callback)
  277. {
  278. string commandText = "DELETE FROM " + Config.TablePrefix + "Permission WHERE Id = @Id";
  279. //准备参数
  280. List<List<Object>> parameters = new List<List<Object>>();
  281. parameters.Add(new List<Object>() { "Id", Id });
  282. string result = "";
  283. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  284. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  285. return Content(callback + "({"
  286. + success
  287. + "})");
  288. }
  289. ////删除权限
  290. [AuthPermission]
  291. [HttpPost, Route("delete")]
  292. public ActionResult Delete(Int32 Id)
  293. {
  294. string commandText = "DELETE FROM " + Config.TablePrefix + "Permission WHERE Id = @Id";
  295. //准备参数
  296. List<List<Object>> parameters = new List<List<Object>>();
  297. parameters.Add(new List<Object>() { "Id", Id });
  298. string result = "";
  299. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  300. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  301. return Content("{success:"
  302. + success
  303. + "}");
  304. }
  305. [HttpPost]
  306. [Route("Esserch")]
  307. public ActionResult Esserch([FromBody]dynamic data)
  308. {
  309. //data={"query":"select a.AppreciationPrice from product a limit 2"};
  310. //data = "{"+"query"+":"+"select a.AppreciationPrice from product a limit 2"+"}";
  311. data = new { query = "select a.AppreciationPrice from product a limit 20" };
  312. var json = JsonConvert.SerializeObject(data);
  313. var str = Util.ElasticQuery("_sql?format=json", "POST", json);
  314. //long totalcount = DataAccess.GetRowCountDefine("select count(Id) from " + commandText1, parameters.ToArray(), out result1);
  315. Root es = new Root();
  316. List<ES> aa = new List<ES>();
  317. Root stobj = (Root)PermissionController.FromJSON<Root>(str);
  318. for (int i = 0; i < stobj.rows.Count; i++)
  319. {
  320. ES bb = new ES();
  321. bb.name = "AA"+i;
  322. bb.price = stobj.rows[i][0].ToString();
  323. aa.Add(bb);
  324. }
  325. var jsonData = JsonConvert.SerializeObject(aa);
  326. //return new JSONNetResult(JsonConvert.DeserializeObject(str));
  327. return Json(new
  328. {
  329. items = JsonConvert.DeserializeObject(jsonData)
  330. });
  331. }
  332. public class ES
  333. {
  334. public string name { get; set; }
  335. public string price { get; set; }
  336. }
  337. public class ColumnsItem
  338. {
  339. public string name { get; set; }
  340. public string type { get; set; }
  341. }
  342. public class Root
  343. {
  344. /// <summary>
  345. ///
  346. /// </summary>
  347. public List<ColumnsItem> columns { get; set; }
  348. /// <summary>
  349. ///
  350. /// </summary>
  351. public List<List<int>> rows { get; set; }
  352. }
  353. public static T FromJSON<T>(string input)
  354. {
  355. try
  356. {
  357. return JsonConvert.DeserializeObject<T>(input);
  358. }
  359. catch (Exception ex)
  360. {
  361. string a = ex.Message;
  362. return default(T);
  363. }
  364. }
  365. ////权限列表
  366. [HttpGet, Route("menus")]
  367. public ActionResult menus(string type,string ParentId)
  368. {
  369. HttpRequest request = HttpContext.Request;
  370. StringValues oo;
  371. string staffid = "";
  372. request.Headers.TryGetValue("Sso-Token", out oo);
  373. if (oo.Count > 0 && oo.ToArray()[0] != "")
  374. {
  375. staffid = oo.ToArray()[0];
  376. }
  377. //取token
  378. var token = (string)_cache.Get(staffid);
  379. var secret = TokenConfig.SecretKey;
  380. Dictionary<string, object> data;
  381. Object roleid;
  382. Int64 roleid1;
  383. string jsonData = "";
  384. if (token != null)
  385. {
  386. try
  387. {
  388. data = JsonWebToken.DecodeToObject<Dictionary<string, object>>(token, secret);
  389. data.TryGetValue("roleid", out roleid);
  390. roleid1 = (Int64)roleid;
  391. var permissions = RoleDAL.GetPermissions(roleid1, " and A.AuthType in('"+ type + "')", ParentId);
  392. var options = Permission.Convert(permissions);
  393. jsonData = JsonConvert.SerializeObject(options);
  394. }
  395. catch (SignatureVerificationException)
  396. {
  397. // Given token is either expired or hashed with an unsupported algorithm.
  398. }
  399. }
  400. return Content(jsonData
  401. );
  402. }
  403. [HttpGet, Route("menusTree")]
  404. public JsonResult menusTree(string type)
  405. {
  406. string roleIdstr = getStaff("roleid");
  407. var sql = $"select A.PermissionName as label,cast(ParentId as varchar) as parentValue,Id as value " +
  408. " from " + Config.TablePrefix + "Permission A" +
  409. " where A.Id in (select B.PermissionId from mem_RoleRelatePermission B" +
  410. " where B.roleid = @roleid and B.permissionId = A.Id and isdelete <> 1 )" +
  411. " and A.AuthType in('" + type + "') " +
  412. " and A.isdelete = 0 ";
  413. DataTable dt = new DataTable();
  414. var param = new List<SqlParameter>();
  415. param.Add(new SqlParameter("roleId", long.Parse(roleIdstr)));
  416. DataAccess.GetValues(sql, ref dt, param.ToArray(), out var msg);
  417. IList<TreeNode> result = new List<TreeNode>();
  418. if (dt != null && dt.Rows.Count > 0)
  419. {
  420. result = ModelConvertHelper<TreeNode>.ConvertToModel(dt);
  421. }
  422. return Json(new
  423. {
  424. result,
  425. msg
  426. });
  427. }
  428. ////父权限列表
  429. //[HttpGet, Route("parents/{0}")]
  430. //public ActionResult parents(string callback)
  431. //{
  432. // var permss = RoleDAL.GetTopPermissions();
  433. // IList<Option> options = new List<Option>();
  434. // foreach (Permission perms in permss)
  435. // {
  436. // var option = new Option() { label = perms.PermissionName, value = perms.Id.ToString(), disabled = perms.IsDelete };
  437. // options.Add(option);
  438. // }
  439. // var jsonData = JsonConvert.SerializeObject(options);
  440. // return Content(callback + "(" + jsonData
  441. // + ")");
  442. //}
  443. [HttpGet, Route("parents")]
  444. public ActionResult parents()
  445. {
  446. var permss = RoleDAL.GetTopPermissions();
  447. IList<Option> options = new List<Option>();
  448. foreach (Permission perms in permss)
  449. {
  450. var option = new Option() { label = perms.PermissionName, value = perms.Id.ToString(), disabled = perms.IsDelete };
  451. options.Add(option);
  452. }
  453. var jsonData = JsonConvert.SerializeObject(options);
  454. return Content(jsonData
  455. );
  456. }
  457. [HttpGet]
  458. [Route("getrole")]
  459. public JsonResult getrole()
  460. {
  461. var sql = $"select RoleRemark as label,'1' as parentValue,id+1 as value from "+Config.TablePrefix+"Role";
  462. DataTable dt = new DataTable();
  463. var param = new List<SqlParameter>();
  464. DataAccess.GetValues(sql, ref dt, param.ToArray(), out _);
  465. IList<TreeNode> result = new List<TreeNode>();
  466. if (dt != null && dt.Rows.Count > 0)
  467. {
  468. result = ModelConvertHelper<TreeNode>.ConvertToModel(dt);
  469. }
  470. result.Add(new TreeNode { parentValue = "0", label = "系统角色", value = 1 });
  471. return Json(new
  472. {
  473. //httpCode = 200,
  474. //message = "获取成功",
  475. result
  476. });
  477. }
  478. /// <summary>
  479. /// 获取组织机构
  480. /// </summary>
  481. /// <returns></returns>
  482. [HttpGet]
  483. [Route("getOrg")]
  484. public JsonResult getOrg()
  485. {
  486. var sql = $"select OrgName as label,'1' as parentValue,(ROW_NUMBER() over(order by OrgId))+1 as value from CM_Org";
  487. DataTable dt = new DataTable();
  488. var param = new List<SqlParameter>();
  489. DataAccess.GetValues(sql, ref dt, param.ToArray(), out _);
  490. IList<TreeNode> result = new List<TreeNode>();
  491. if (dt != null && dt.Rows.Count > 0)
  492. {
  493. result = ModelConvertHelper<TreeNode>.ConvertToModel(dt);
  494. }
  495. result.Add(new TreeNode { parentValue = "0", label = "亮健好医药有限公司", value = 1 });
  496. return Json(new
  497. {
  498. //httpCode = 200,
  499. //message = "获取成功",
  500. result
  501. });
  502. }
  503. /// <summary>
  504. /// 获取机构权限列表
  505. /// </summary>
  506. /// <param name="data"></param>
  507. /// <returns></returns>
  508. [AuthPermission]
  509. [HttpPost]
  510. [Route("table")]
  511. public JsonResult Table([FromBody] dynamic data)
  512. {
  513. QueryFilter[] jArray = JsonConvert.DeserializeObject<QueryFilter[]>(data.filters.ToString());
  514. var a = "";
  515. List<QueryFilter> filterList = new List<QueryFilter> ();
  516. var i = 0;
  517. foreach (QueryFilter filter in jArray) {
  518. if (filter.field == "roleId")
  519. a = filter.value.ToString();
  520. else {
  521. filterList.Add(filter);
  522. i++;
  523. }
  524. }
  525. var roleID = int.Parse(a) - 1;
  526. var param = new List<SqlParameter>();
  527. var filterstr = QueryFilter.getFilterSqlParam(filterList.ToArray(), out param,new TempTableResult(),"A.");
  528. filterstr = filterstr.Replace("A.permissionRule", "B.PermissionRule");
  529. filterstr = filterstr.Replace("A.permissionRuleType", "B.PermissionRuleType");
  530. var sql = $"select case when B.Id is null then 0 else A.id end as checkedValue," +
  531. $"A.*,B.PermissionRule,B.PermissionRuleType,B.Id as relateId from {Config.TablePrefix}Permission A " +
  532. $" left join {Config.TablePrefix}RoleRelatePermission B on B.PermissionId = A.Id and B.RoleId=@roleId and B.IsDelete <> 1 " +
  533. $" where A.IsDelete <> 1 " +
  534. $"" + filterstr;
  535. DataTable dt = new DataTable();
  536. param.Add(new SqlParameter("roleId", roleID));
  537. DataAccess.GetValues(sql, ref dt, param.ToArray(), out var msg);
  538. IList<TempTableResult> result = new List<TempTableResult>();
  539. if (dt != null && dt.Rows.Count > 0)
  540. {
  541. result = ModelConvertHelper<TempTableResult>.ConvertToModel(dt);
  542. }
  543. var countSql =
  544. $"select count(1) from "+Config.TablePrefix+"Permission where IsDelete <> 1";
  545. var count = DataAccess.GetRowCountDefine(countSql, param.ToArray(), out var msg1);
  546. return Json(new
  547. {
  548. items = result,
  549. sum = new { },
  550. totalCount = count,
  551. msg
  552. });
  553. }
  554. /// <summary>
  555. /// 设置机构权限
  556. /// </summary>
  557. /// <param name="data"></param>
  558. /// <returns></returns>
  559. [AuthPermission]
  560. [HttpPost]
  561. [Route("setrole")]
  562. public JsonResult setrole([FromBody] TempQuery data)
  563. {
  564. var param = new List<SqlParameter>();
  565. param.Add(new SqlParameter("RoleId", data.roleId));
  566. var sql = "update sup_RoleRelatePermission set IsDelete =1 where RoleId=@RoleId; ";
  567. var index = 0;
  568. foreach (var item in data.data)
  569. {
  570. index++;
  571. param.Add(new SqlParameter($"{index}", item));
  572. sql += $" if (exists(select * from sup_RoleRelatePermission where RoleId = @RoleId and PermissionId = @{index} and IsDelete = 1)) " +
  573. " begin " +
  574. $" update sup_RoleRelatePermission set IsDelete = 0 where RoleId = @RoleId and PermissionId = @{index} and IsDelete = 1 " +
  575. " end " +
  576. " else " +
  577. " begin " +
  578. $" insert into sup_RoleRelatePermission (PermissionId, RoleId) values (@{index},@RoleId) end ";
  579. }
  580. DataAccess.ExecuteCommand(sql, param.ToArray(), out var x);
  581. return Json(new
  582. {
  583. success = true
  584. });
  585. }
  586. public class TempTableResult
  587. {
  588. public int id { get; set; }
  589. public string permissionName { get; set; }
  590. public string permissionUrl { get; set; }
  591. public int roleId { get; set; }
  592. public int checkedValue { get; set; }
  593. public string permissionRule { get; set; }
  594. public string permissionRuleType { get; set; }
  595. public int relateId { get; set; }
  596. public string AuthType { get; set; }
  597. }
  598. public class TempQuery
  599. {
  600. public int[] data { get; set; }
  601. public int roleId { get; set; }
  602. }
  603. [HttpGet, Route("authtype")]
  604. public ActionResult AuthType()
  605. {
  606. IList<Option> options = new List<Option>();
  607. var option = new Option() { label = "界面", value = "U" ,disabled = false };
  608. options.Add(option);
  609. var option1 = new Option() { label = "操作", value = "O", disabled = false };
  610. options.Add(option1);
  611. var option2 = new Option() { label = "按钮", value = "A", disabled = false };
  612. options.Add(option2);
  613. var option3 = new Option() { label = "顶部菜单", value = "M", disabled = false };
  614. options.Add(option3);
  615. var jsonData = JsonConvert.SerializeObject(options);
  616. return Content(jsonData
  617. );
  618. }
  619. [HttpGet, Route("permissions")]
  620. public ActionResult Permissions()
  621. {
  622. string roleid = getStaff("roleid");
  623. if(roleid == null || roleid == "")
  624. {
  625. return Json(new { success = false,msg="没有登陆" });
  626. }
  627. Dictionary<string, Permission> permission = PermissionHelper.Permissions(Convert.ToInt32(roleid));
  628. return Json(permission);
  629. }
  630. public class SortContent
  631. {
  632. public Object content { get; set; }//权限内容
  633. public string code { get; set; }//权限id
  634. public int sort { get; set; }//权限序号
  635. }
  636. [AuthPermission]
  637. [HttpPost]
  638. [Route("setSort")]
  639. public JsonResult setSort([FromBody]dynamic data)
  640. {
  641. SortContent[] dataItems = new SortContent[] { };
  642. if (data != null)
  643. {
  644. //Newtonsoft.Json.Linq.JArray
  645. dataItems = data.data.ToObject<SortContent[]>();
  646. }
  647. List<DataAccessCommand> list = new List<DataAccessCommand>();
  648. foreach (SortContent item in dataItems) {
  649. var param = new List<SqlParameter>();
  650. param.Add(new SqlParameter("Id", item.code));
  651. param.Add(new SqlParameter("Sort", item.sort));
  652. var sql = "update "+Config.TablePrefix+"Permission set Sort=@Sort where Id=@Id; ";
  653. list.Add(new DataAccessCommand(sql, param,CommandType.Text,false));
  654. }
  655. var success = DataAccess.ExecuteBatchCommands(list, out var x);
  656. return Json(new
  657. {
  658. success,
  659. msg = x
  660. });
  661. }
  662. [AuthPermission]
  663. [HttpPost]
  664. [Route("editroleperm")]
  665. public JsonResult editRolePerm([FromBody] TempTableResult entity)
  666. {
  667. var sql = "update " + Config.TablePrefix + "RoleRelatePermission set " +
  668. " PermissionRule = a1,PermissionRuleType = a2 " +
  669. " from (select @PermissionRule as a1 ,@PermissionRuleType as a2 )A " +
  670. " where Id = @relateId";
  671. var param = new List<SqlParameter>();
  672. param.Add(new SqlParameter("@PermissionRule", entity.permissionRule));
  673. param.Add(new SqlParameter("@PermissionRuleType", entity.permissionRuleType));
  674. var ss = new SqlParameter("@relateId", SqlDbType.Int);
  675. ss.Value = entity.relateId;
  676. param.Add(ss);
  677. List<DataAccessCommand> list = new List<DataAccessCommand>(0);
  678. list.Add(new DataAccessCommand(sql, param, CommandType.Text, false));
  679. var success = DataAccess.ExecuteBatchCommands(list, out var msg);
  680. return Json(new
  681. {
  682. success,
  683. msg
  684. });
  685. }
  686. [AuthPermission]
  687. [HttpGet]
  688. [Route("getruletype")]
  689. public ActionResult getRuleType()
  690. {
  691. IList<Option> options = new List<Option>();
  692. var option = new Option() { label = "SQL", value = "S", disabled = false };
  693. options.Add(option);
  694. var option1 = new Option() { label = "JSON", value = "J", disabled = false };
  695. options.Add(option1);
  696. var jsonData = JsonConvert.SerializeObject(options);
  697. return Content(jsonData
  698. );
  699. }
  700. }
  701. }