RoleController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using Central.Control.Domain;
  7. using MES.Production.Service.IService;
  8. using Ant.Service.Common;
  9. using Ant.Service.Common.Enums;
  10. using ChangFa.Machinery.WebPage.Controllers;
  11. namespace ChangFa.Machinery.WebPage.Areas.SysManage.Controllers
  12. {
  13. public class RoleController : BaseController
  14. {
  15. #region 声明容器
  16. /// <summary>
  17. /// 角色
  18. /// </summary>
  19. private IRoleManage RoleManage { get; set; }
  20. /// <summary>
  21. /// 用户角色
  22. /// </summary>
  23. private IUserRoleManage UserRoleManage { get; set; }
  24. /// <summary>
  25. /// 角色权限
  26. /// </summary>
  27. private IRolePermissionManage RolePermissionManage { get; set; }
  28. #endregion
  29. /// <summary>
  30. /// 加载主页
  31. /// </summary>
  32. /// <returns></returns>
  33. [UserAuthorizeAttribute(ModuleAlias = "Role", OperaAction = "View")]
  34. public ActionResult Index()
  35. {
  36. try
  37. {
  38. #region 处理查询参数
  39. #endregion
  40. #region 加载列表
  41. var result = BindList();
  42. ViewBag.Search = base.keywords;
  43. #endregion
  44. return View(result);
  45. }
  46. catch (Exception e)
  47. {
  48. WriteLog(enumOperator.Select, "加载角色列表:", e);
  49. throw e.InnerException;
  50. }
  51. }
  52. /// <summary>
  53. /// 绑定页面需要的属性
  54. /// </summary>
  55. private PageInfo BindList()
  56. {
  57. //基础数据
  58. var query = this.RoleManage.LoadAll(null);
  59. if (!string.IsNullOrEmpty(keywords))
  60. {
  61. query = query.Where(p => p.ROLENAME.Contains(keywords));
  62. }
  63. query = query.OrderByDescending(p => p.CREATEDATE);
  64. var result = this.RoleManage.Query(query, base.page, base.pagesize);
  65. var list = result.List.Select(p => new
  66. {
  67. //以下是视图需要展示的内容,加动态可循环
  68. p.CREATEDATE,
  69. p.ROLENAME,
  70. p.ROLEDESC,
  71. USERNAME = p.CREATEPERID,
  72. p.ID,
  73. ISCUSTOM = p.ISCUSTOM == 0 ? "是" : "否"
  74. }).ToList();
  75. return new PageInfo(result.Index, result.PageSize, result.Count, JsonConverter.JsonClass(list));
  76. }
  77. /// <summary>
  78. /// 加载详情
  79. /// </summary>
  80. /// <returns></returns>
  81. [HttpGet]
  82. [UserAuthorizeAttribute(ModuleAlias = "Role", OperaAction = "Detail")]
  83. public ActionResult Detail(int? id)
  84. {
  85. var _entity = this.RoleManage.Get(p => p.ID == id) ?? new SYS_ROLE();
  86. return View(_entity);
  87. }
  88. /// <summary>
  89. /// 保存角色
  90. /// </summary>
  91. [UserAuthorizeAttribute(ModuleAlias = "Role", OperaAction = "Add,Edit")]
  92. public ActionResult Save(SYS_ROLE entity)
  93. {
  94. bool isEdit = false;
  95. var json = new JsonHelper() { Msg = "保存成功", ReUrl = "/Sys/Role/Index", Status = "n" };
  96. try
  97. {
  98. if (entity != null)
  99. {
  100. //判断角色名是否汉字
  101. if (System.Text.RegularExpressions.Regex.IsMatch(entity.ROLENAME.Trim(), "^[\u4e00-\u9fa5]+$"))
  102. {
  103. if (entity.ROLENAME.Length > 36)
  104. {
  105. json.Msg = "角色名称最多只能能包含36个汉字";
  106. return Json(json);
  107. }
  108. //获取不是HTML绑定的标签
  109. entity.ISCUSTOM = string.IsNullOrEmpty(Request.Form["iscustom"]) ? 1 : 0;
  110. //添加
  111. if (entity.ID <= 0)
  112. {
  113. entity.CREATEDATE = DateTime.Now;
  114. entity.CREATEPERID = this.CurrentUser.Name;
  115. entity.UPDATEDATE = DateTime.Now;
  116. entity.UPDATEUSER = this.CurrentUser.Name;
  117. }
  118. else //修改
  119. {
  120. SYS_ROLE _entity = this.RoleManage.Get(p => p.ID == entity.ID);
  121. entity.CREATEDATE = _entity.CREATEDATE;
  122. entity.CREATEPERID = _entity.CREATEPERID;
  123. entity.UPDATEDATE = DateTime.Now;
  124. entity.UPDATEUSER = this.CurrentUser.Name;
  125. isEdit = true;
  126. }
  127. //判断角色是否重名
  128. if (!this.RoleManage.IsExist(p => p.ROLENAME == entity.ROLENAME && p.ID != entity.ID))
  129. {
  130. if (RoleManage.SaveOrUpdate(entity, isEdit))
  131. {
  132. json.Status = "y";
  133. }
  134. else
  135. {
  136. json.Msg = "保存失败";
  137. }
  138. }
  139. else
  140. {
  141. json.Msg = "角色名" + entity.ROLENAME + "已被使用,请修改角色名称再提交";
  142. }
  143. }
  144. else
  145. {
  146. json.Msg = "角色名称只能包含汉字";
  147. }
  148. }
  149. else
  150. {
  151. json.Msg = "未找到需要保存的角色信息";
  152. }
  153. if (isEdit)
  154. {
  155. WriteLog(enumOperator.Edit, "修改用户角色,结果:" + json.Msg, enumLog4net.INFO);
  156. }
  157. else
  158. {
  159. WriteLog(enumOperator.Add, "添加用户角色,结果:" + json.Msg, enumLog4net.INFO);
  160. }
  161. }
  162. catch(Exception e)
  163. {
  164. json.Msg = "保存用户角色发生内部错误!";
  165. WriteLog(enumOperator.None, "保存用户角色:", e);
  166. }
  167. return Json(json);
  168. }
  169. /// <summary>
  170. /// 删除角色
  171. /// </summary>
  172. [UserAuthorizeAttribute(ModuleAlias = "Role", OperaAction = "Remove")]
  173. public ActionResult Delete(string idList)
  174. {
  175. var json = new JsonHelper() { Msg = "删除角色完毕", Status = "n" };
  176. var id = idList.Trim(',').Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(p => int.Parse(p)).ToList();
  177. if (id.Contains(ClsDic.DicRole["超级管理员"]))
  178. {
  179. json.Msg = "删除失败,不能删除系统固有角色!";
  180. WriteLog(enumOperator.Remove, "删除用户角色:" + json.Msg, enumLog4net.ERROR);
  181. return Json(json);
  182. }
  183. if (this.UserRoleManage.IsExist(p => id.Contains(p.FK_ROLEID)))
  184. {
  185. json.Msg = "删除失败,不能删除系统中正在使用的角色!";
  186. WriteLog(enumOperator.Remove, "删除用户角色:" + json.Msg, enumLog4net.ERROR);
  187. return Json(json);
  188. }
  189. try
  190. {
  191. //1、删除角色权限
  192. RolePermissionManage.Delete(p => id.Contains(p.ROLEID));
  193. //2、删除角色
  194. RoleManage.Delete(p => id.Contains(p.ID));
  195. json.Status = "y";
  196. WriteLog(enumOperator.Remove, "删除用户角色:" + json.Msg, enumLog4net.WARN);
  197. }
  198. catch (Exception e)
  199. {
  200. json.Msg = "删除用户角色发生内部错误!";
  201. WriteLog(enumOperator.Remove, "删除用户角色:", e);
  202. }
  203. return Json(json);
  204. }
  205. #region 帮助方法及其他控制器调用
  206. /// <summary>
  207. /// 获取用户分配的角色
  208. /// </summary>
  209. [UserAuthorizeAttribute(ModuleAlias = "Role", OperaAction = "Allocation")]
  210. public ActionResult RoleCall(int? id)
  211. {
  212. try
  213. {
  214. if (id != null && id > 0)
  215. {
  216. ViewData["userId"] = id;
  217. //获取当前用户设置过的角色信息
  218. var userrole = this.UserRoleManage.LoadAll(p => p.FK_USERID == id).Select(p => p.FK_ROLEID).ToList();
  219. string roleId = userrole.Aggregate(string.Empty, (current, t) => current + (t + ",")).TrimEnd(',');
  220. ViewData["roleId"] = roleId;
  221. }
  222. base.pagesize = 1000;
  223. return View(BindList());
  224. }
  225. catch (Exception e)
  226. {
  227. WriteLog(enumOperator.Select, "获取用户分配的角色:", e);
  228. throw e.InnerException;
  229. }
  230. }
  231. /// <summary>
  232. /// 设置用户角色
  233. /// </summary>
  234. [UserAuthorizeAttribute(ModuleAlias = "Role", OperaAction = "Allocation")]
  235. public ActionResult UserRole()
  236. {
  237. JsonHelper json = new JsonHelper()
  238. {
  239. Msg = "设置用户角色成功",
  240. Status = "n"
  241. };
  242. string userId = Request.Form["userId"];
  243. string roleId = Request.Form["roleId"];
  244. if (string.IsNullOrEmpty(userId))
  245. {
  246. json.Msg = "未找到要分配角色用户";
  247. return Json(json);
  248. }
  249. roleId = roleId.TrimEnd(',');
  250. try
  251. {
  252. //设置用户角色
  253. this.UserRoleManage.SetUserRole(int.Parse(userId), roleId);
  254. json.Status = "y";
  255. WriteLog(enumOperator.Allocation, "设置用户角色:" + json.Msg, enumLog4net.INFO);
  256. }
  257. catch (Exception e)
  258. {
  259. json.Msg = "设置失败,错误:" + e.Message;
  260. WriteLog(enumOperator.Allocation, "设置用户角色:", e);
  261. }
  262. return Json(json);
  263. }
  264. #endregion
  265. }
  266. }