using CommonToolsCore; using DTO; using IServices; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using zhengcaioa.IService; using zhengcaioa.Models; namespace zhengcaioa.Controllers { public class PltAuthController : Controller { private readonly ILogger _logger; private readonly IPltRoleService _pltRoleService; private readonly IUserService _userService; private readonly ISysCodeService _sysCodeService; private readonly IPltPageService _pltPageService; public PltAuthController(ILogger logger, IPltRoleService pltRoleService, IUserService userService, ISysCodeService sysCodeService, IPltPageService pltPageService) { _logger = logger; _pltRoleService = pltRoleService; _userService = userService; _sysCodeService = sysCodeService; _pltPageService = pltPageService; } [CheckLogin] public ActionResult Search() { #region 获取所有角色 var listRole = _pltRoleService.listRole(); ViewBag.listRole = listRole; #endregion return View(); } [CheckLogin] public JsonResult GetPageByRole(string roleid = "") { var liststr = new List(); // var listPageAll = new List(); #region 读取缓存权限 //CacheHelper.RemoveCache("listPage"); // = CacheHelper.GetCache("listPage") as List; //CacheHelper.RemoveCache("listPage");//清除权限缓存 //var listPageAll = CacheHelperNetCore.CacheValue("listPage") as List; //if (listPageAll == null || listPageAll.Count == 0) //{ var listPage = _pltPageService.GePagetList(); var listP = new List(); if (listPage.Count > 0) { listPage.ForEach(p => { listP.Add(new TreeAuthPage() { id = p.PageID, pId = p.PageSuperior, name = p.PageName }); }); } var listPP = GetTreeAuthPage(listP); var listPageAll = listPP; //CacheHelperNetCore.CacheInsert("listPage", listPP); //} //CacheHelper.SetCache("listPage", listPP); #endregion #region 获取所有权限 #endregion #region 获取角色配置的权限 var listAuth = _pltPageService.GetauthList(roleid); listPageAll.ForEach(p => { bool b = false; if (listAuth.Count(a => a.ItemId == p.id) > 0) { b = true; } liststr.Add("{ id:'" + p.id + "', pId:'" + p.pId + "', name:'" + p.name + "', open:" + (string.IsNullOrEmpty(p.pId)) + ",checked:" + b + "}"); }); #endregion return Json(new { sJson = "[" + string.Join(",", liststr) + "]" }); } /// /// 递归去掉没有一级菜单的数据 /// /// /// /// [CheckLogin] public List GetTreeAuthPage(List listP) { listP.ForEach(p => { if (!string.IsNullOrEmpty(p.pId)&& p.pId!="0") { var nCount = listP.Count(pp => pp.id == p.pId); if (nCount == 0) { listP.Remove(p); GetTreeAuthPage(listP); } } }); return listP; } /// /// 保存角色权限操作 /// /// /// /// /// [CheckLogin] public string Save(string roleid = "", string pageid = "") { var curentuser = JsonConvert.DeserializeObject(HttpContext.Session.GetString("User")); ViewData["curentuser"] = curentuser; var result = new ResultEntity(); #region 数据验证 if (string.IsNullOrWhiteSpace(roleid)) { result.Result = false; result.Message = "请选择角色"; return JsonConvert.SerializeObject(result); } if (string.IsNullOrWhiteSpace(pageid)) { result.Result = false; result.Message = "请选择权限"; return JsonConvert.SerializeObject(result); } #endregion return JsonConvert.SerializeObject(_pltPageService.SaveManyEntity(roleid, pageid, curentuser.Id)); } } }