SysModuleController.cs 7.75 KB
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.POJO.ViewModel;
using Hh.Mes.Service.SystemAuth;
using Hh.Mes.Service.WebService.Base;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using WebMvc.Aop;

namespace WebMvc
{
    /// <summary>
    /// 模块管理
    /// </summary>
    [Area("base")]
    public class SysModuleController : BaseController
    {
        private readonly SysModuleService service;
        private readonly List<CheckBoxItem> defaultBtns;
        private readonly int MaxAreas = 3;
        public SysModuleController(IAuth authUtil, SysModuleService service) : base(authUtil)
        {
            this.service = service;
            this.service.sysWebUser = authUtil.GetCurrentUser().User;
            defaultBtns = service.DefaultBtns;
        }

        // GET: /SysModule/
        [Authenticate]
        public ActionResult Index()
        {
            return View();
        }

        [Authenticate]
        public ActionResult Assign()
        {
            return View();
        }

        /// <summary>
        /// 常用按钮展示页面
        /// </summary>
        /// <param name="moduleId">菜单Id</param>
        /// <returns></returns>
        [Authenticate]
        public IActionResult CommonBtn(int moduleId)
        {
            var user = _authUtil.GetCurrentUser();
            var module = user.Modules.SingleOrDefault(u => u.Id == moduleId);
            ViewBag.Name = $"{module.ParentName}/{module.Name}";
            ViewBag.ModuleId = moduleId;
            var areaBtns = new List<AreaBtnModel>();
            for (int i = 1; i <= MaxAreas; i++)
            {
                var areaBtnItem = new AreaBtnModel
                {
                    Area = i.ToString(),
                };
                var data = module?.Elements.Where(x => x.AreaMenus.Equals(i.ToString())).ToList();
                if (data.Any())
                {
                    var btns = data.Where(btn => defaultBtns.Select(x => x.Value).Contains(btn.DomId)).Select(n => new CheckBoxItem
                    {
                        Text = n.Name,
                        Value = n.DomId,
                        Icon = n.Icon,
                        BtnClass = n.Class,
                        Sort = (int)n.Sort,
                        Checked = true
                    }).ToList();
                    var disenableBtns = defaultBtns.Where(x => !btns.Select(b => b.Value).Contains(x.Value)).ToList();
                    btns.AddRange(disenableBtns);
                    areaBtnItem.CheckBoxItems = btns;
                }
                else
                {
                    areaBtnItem.CheckBoxItems = defaultBtns;
                }
                areaBtns.Add(areaBtnItem);
            }
            return View(areaBtns);
        }

        /// <summary>
        /// 更新常用按钮
        /// </summary>
        /// <param name="moduleId">菜单页面编号</param>
        /// <param name="area1">区域1按钮集合</param>
        /// <param name="area2">区域2按钮集合</param>
        /// <param name="area3">区域3按钮集合</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SaveChanageCommonBtn(int moduleId, List<string> area1, List<string> area2, List<string> area3)
        {
            try
            {
                var modulesView = _authUtil.GetCurrentUser().Modules;
                service.SaveChanageCommonBtn(moduleId, area1, area2, area3, modulesView);
                return Content($"操作成功,请手动关闭页面!");
            }
            catch (Exception ex)
            {
                return Content($"操作失败,{ex.Message}");
            }
        }

        /// <summary>
        /// 加载特定用户的模块
        /// </summary>
        /// <param name="firstId">The user identifier.</param>
        /// <returns>System.String.</returns>
        public string LoadForUser(int firstId)
        {
            var modules = service.LoadForUser(firstId);
            return Serialize(modules);
        }
        /// <summary>
        /// 根据某用户ID获取可访问某模块的菜单项
        /// </summary>
        /// <returns></returns>
        public string LoadMenusForUser(int moduleId, int firstId)
        {
            var menus = service.LoadMenusForUser(moduleId, firstId);
            return Serialize(menus);
        }

        /// <summary>
        /// 加载角色模块
        /// </summary>
        /// <param name="firstId">The role identifier.</param>
        /// <returns>System.String.</returns>
        public string LoadForRole(int firstId)
        {
            var modules = service.LoadForRole(firstId);
            return Serialize(modules);
        }

        /// <summary>
        /// 根据某角色ID获取可访问某模块的菜单项
        /// </summary>
        /// <returns></returns>
        public string LoadMenusForRole(int moduleId, int firstId)
        {
            var menus = service.LoadMenusForRole(moduleId, firstId);
            return Serialize(menus);
        }

        /// <summary>
        /// 获取发起页面的菜单权限
        /// </summary>
        /// <returns>System.String.</returns>
        public string LoadAuthorizedMenus(string modulecode, string AreaMenus)
        {
            var user = _authUtil.GetCurrentUser();
            var module = user.Modules.Find(u => u.Code == modulecode);
            if (module != null)
            {
                var moduleMenus = module.Elements.FindAll(u => u.AreaMenus == AreaMenus);
                return Serialize(moduleMenus);
            }

            return "";
        }

        #region 添加编辑模块

        //添加模块
        [HttpPost]
        [XSSFilter]
        public string Add(SysModule model)
        {
            model.Url = model.Url ?? "";
            return Serialize(service.Ins(model));
        }

        //修改模块
        [HttpPost]
        [XSSFilter]
        public string Update(SysModule model)
        {
            model.Url = model.Url ?? "";
            return Serialize(service.Upd(model));
        }

        /// <summary>
        /// 删除模块
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public string Delete(int[] ids)
        {
            return Serialize(service.DelById(ids));
        }

        #endregion 添加编辑模块

        /// <summary>
        /// 加载当前用户可访问模块的菜单
        /// </summary>
        /// <param name="moduleId">The module identifier.</param>
        /// <returns>System.String.</returns>
        public string LoadMenus(int moduleId)
        {
            var user = _authUtil.GetCurrentUser();
            var data = new Response();

            if (moduleId != 0)
            {
                var module = user.Modules.SingleOrDefault(u => u.Id == moduleId);
                if (module != null)
                {
                    data.Result = module.Elements;
                    data.Count = module.Elements.Count();
                }
            }

            return Serialize(data);
        }

        //添加菜单
        [HttpPost]
        [XSSFilter]
        public string AddMenu(SysModuleElement model)
        {            
            return Serialize(service.AddMenu(model, this.service.sysWebUser.Account));
        }

        //添加菜单
        [HttpPost]
        [XSSFilter]
        public string UpdateMenu(SysModuleElement model)
        {
            return Serialize(service.UpdateMenu(model));
        }


        /// <summary>
        /// 删除菜单
        /// </summary>
        [HttpPost]
        public string DelMenu(params int[] ids)
        {
            return Serialize(service.DelMenu(ids));
        }
    }
}