123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using Central.Control.Domain;
- using MES.Production.Service.IService;
- using Ant.Service.Common;
- using Ant.Service.Common.Enums;
- using ChangFa.Machinery.WebPage.Controllers;
- namespace ChangFa.Machinery.WebPage.Areas.SysManage.Controllers
- {
- public class RoleController : BaseController
- {
- #region 声明容器
- /// <summary>
- /// 角色
- /// </summary>
- private IRoleManage RoleManage { get; set; }
- /// <summary>
- /// 用户角色
- /// </summary>
- private IUserRoleManage UserRoleManage { get; set; }
- /// <summary>
- /// 角色权限
- /// </summary>
- private IRolePermissionManage RolePermissionManage { get; set; }
- #endregion
- /// <summary>
- /// 加载主页
- /// </summary>
- /// <returns></returns>
- [UserAuthorizeAttribute(ModuleAlias = "Role", OperaAction = "View")]
- public ActionResult Index()
- {
- try
- {
- #region 处理查询参数
- #endregion
- #region 加载列表
- var result = BindList();
- ViewBag.Search = base.keywords;
- #endregion
- return View(result);
- }
- catch (Exception e)
- {
- WriteLog(enumOperator.Select, "加载角色列表:", e);
- throw e.InnerException;
- }
- }
- /// <summary>
- /// 绑定页面需要的属性
- /// </summary>
- private PageInfo BindList()
- {
- //基础数据
- var query = this.RoleManage.LoadAll(null);
- if (!string.IsNullOrEmpty(keywords))
- {
- query = query.Where(p => p.ROLENAME.Contains(keywords));
- }
- query = query.OrderByDescending(p => p.CREATEDATE);
- var result = this.RoleManage.Query(query, base.page, base.pagesize);
- var list = result.List.Select(p => new
- {
- //以下是视图需要展示的内容,加动态可循环
- p.CREATEDATE,
- p.ROLENAME,
- p.ROLEDESC,
- USERNAME = p.CREATEPERID,
- p.ID,
- ISCUSTOM = p.ISCUSTOM == 0 ? "是" : "否"
- }).ToList();
- return new PageInfo(result.Index, result.PageSize, result.Count, JsonConverter.JsonClass(list));
- }
- /// <summary>
- /// 加载详情
- /// </summary>
- /// <returns></returns>
- [HttpGet]
- [UserAuthorizeAttribute(ModuleAlias = "Role", OperaAction = "Detail")]
- public ActionResult Detail(int? id)
- {
- var _entity = this.RoleManage.Get(p => p.ID == id) ?? new SYS_ROLE();
- return View(_entity);
- }
- /// <summary>
- /// 保存角色
- /// </summary>
- [UserAuthorizeAttribute(ModuleAlias = "Role", OperaAction = "Add,Edit")]
- public ActionResult Save(SYS_ROLE entity)
- {
- bool isEdit = false;
- var json = new JsonHelper() { Msg = "保存成功", ReUrl = "/Sys/Role/Index", Status = "n" };
- try
- {
- if (entity != null)
- {
- //判断角色名是否汉字
- if (System.Text.RegularExpressions.Regex.IsMatch(entity.ROLENAME.Trim(), "^[\u4e00-\u9fa5]+$"))
- {
- if (entity.ROLENAME.Length > 36)
- {
- json.Msg = "角色名称最多只能能包含36个汉字";
- return Json(json);
- }
- //获取不是HTML绑定的标签
- entity.ISCUSTOM = string.IsNullOrEmpty(Request.Form["iscustom"]) ? 1 : 0;
- //添加
- if (entity.ID <= 0)
- {
- entity.CREATEDATE = DateTime.Now;
- entity.CREATEPERID = this.CurrentUser.Name;
- entity.UPDATEDATE = DateTime.Now;
- entity.UPDATEUSER = this.CurrentUser.Name;
- }
- else //修改
- {
- SYS_ROLE _entity = this.RoleManage.Get(p => p.ID == entity.ID);
- entity.CREATEDATE = _entity.CREATEDATE;
- entity.CREATEPERID = _entity.CREATEPERID;
- entity.UPDATEDATE = DateTime.Now;
- entity.UPDATEUSER = this.CurrentUser.Name;
- isEdit = true;
- }
- //判断角色是否重名
- if (!this.RoleManage.IsExist(p => p.ROLENAME == entity.ROLENAME && p.ID != entity.ID))
- {
- if (RoleManage.SaveOrUpdate(entity, isEdit))
- {
- json.Status = "y";
- }
- else
- {
- json.Msg = "保存失败";
- }
- }
- else
- {
- json.Msg = "角色名" + entity.ROLENAME + "已被使用,请修改角色名称再提交";
- }
- }
- else
- {
- json.Msg = "角色名称只能包含汉字";
- }
- }
- else
- {
- json.Msg = "未找到需要保存的角色信息";
- }
- if (isEdit)
- {
- WriteLog(enumOperator.Edit, "修改用户角色,结果:" + json.Msg, enumLog4net.INFO);
- }
- else
- {
- WriteLog(enumOperator.Add, "添加用户角色,结果:" + json.Msg, enumLog4net.INFO);
- }
- }
- catch(Exception e)
- {
- json.Msg = "保存用户角色发生内部错误!";
- WriteLog(enumOperator.None, "保存用户角色:", e);
- }
- return Json(json);
- }
- /// <summary>
- /// 删除角色
- /// </summary>
- [UserAuthorizeAttribute(ModuleAlias = "Role", OperaAction = "Remove")]
- public ActionResult Delete(string idList)
- {
- var json = new JsonHelper() { Msg = "删除角色完毕", Status = "n" };
- var id = idList.Trim(',').Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(p => int.Parse(p)).ToList();
- if (id.Contains(ClsDic.DicRole["超级管理员"]))
- {
- json.Msg = "删除失败,不能删除系统固有角色!";
- WriteLog(enumOperator.Remove, "删除用户角色:" + json.Msg, enumLog4net.ERROR);
- return Json(json);
- }
- if (this.UserRoleManage.IsExist(p => id.Contains(p.FK_ROLEID)))
- {
- json.Msg = "删除失败,不能删除系统中正在使用的角色!";
- WriteLog(enumOperator.Remove, "删除用户角色:" + json.Msg, enumLog4net.ERROR);
- return Json(json);
- }
- try
- {
- //1、删除角色权限
- RolePermissionManage.Delete(p => id.Contains(p.ROLEID));
- //2、删除角色
- RoleManage.Delete(p => id.Contains(p.ID));
- json.Status = "y";
- WriteLog(enumOperator.Remove, "删除用户角色:" + json.Msg, enumLog4net.WARN);
- }
- catch (Exception e)
- {
- json.Msg = "删除用户角色发生内部错误!";
- WriteLog(enumOperator.Remove, "删除用户角色:", e);
- }
- return Json(json);
- }
- #region 帮助方法及其他控制器调用
- /// <summary>
- /// 获取用户分配的角色
- /// </summary>
- [UserAuthorizeAttribute(ModuleAlias = "Role", OperaAction = "Allocation")]
- public ActionResult RoleCall(int? id)
- {
- try
- {
- if (id != null && id > 0)
- {
- ViewData["userId"] = id;
- //获取当前用户设置过的角色信息
- var userrole = this.UserRoleManage.LoadAll(p => p.FK_USERID == id).Select(p => p.FK_ROLEID).ToList();
- string roleId = userrole.Aggregate(string.Empty, (current, t) => current + (t + ",")).TrimEnd(',');
- ViewData["roleId"] = roleId;
- }
- base.pagesize = 1000;
- return View(BindList());
- }
- catch (Exception e)
- {
- WriteLog(enumOperator.Select, "获取用户分配的角色:", e);
- throw e.InnerException;
- }
- }
- /// <summary>
- /// 设置用户角色
- /// </summary>
- [UserAuthorizeAttribute(ModuleAlias = "Role", OperaAction = "Allocation")]
- public ActionResult UserRole()
- {
- JsonHelper json = new JsonHelper()
- {
- Msg = "设置用户角色成功",
- Status = "n"
- };
- string userId = Request.Form["userId"];
- string roleId = Request.Form["roleId"];
- if (string.IsNullOrEmpty(userId))
- {
- json.Msg = "未找到要分配角色用户";
- return Json(json);
- }
- roleId = roleId.TrimEnd(',');
- try
- {
- //设置用户角色
- this.UserRoleManage.SetUserRole(int.Parse(userId), roleId);
- json.Status = "y";
- WriteLog(enumOperator.Allocation, "设置用户角色:" + json.Msg, enumLog4net.INFO);
- }
- catch (Exception e)
- {
- json.Msg = "设置失败,错误:" + e.Message;
- WriteLog(enumOperator.Allocation, "设置用户角色:", e);
- }
- return Json(json);
- }
- #endregion
- }
- }
|