//修改菜单
        public IActionResult EditAdminMenu(int id)
        {
            AdminMenu entity = AdminMenu.Find(AdminMenu._.Id == id);

            if (entity == null)
            {
                return(EchoTipPage("系统找不到本记录!"));
            }
            //获取上级栏目
            IList <AdminMenu> list = AdminMenu.GetListTree(0, -1, true, false);

            ViewBag.ListTree = list;
            IList <TargetEvent> listevent = TargetEvent.FindAll(TargetEvent._.IsDisable != 1, TargetEvent._.Rank.Asc(), null, 0, 0);

            ViewBag.ListEvent = listevent;

            return(View(entity));
        }
        public IActionResult DelAdminMenu(int id)
        {
            AdminMenu entity = AdminMenu.Find(AdminMenu._.Id == id);

            if (entity == null)
            {
                tip.Message = "系统找不到本记录!";
                return(Json(tip));
            }
            if (AdminMenu.FindCount(AdminMenu._.PId == entity.Id, null, null, 0, 0) > 0)
            {
                tip.Message = "本菜单有下级菜单,不允许删除!";
                return(Json(tip));
            }
            entity.Delete();
            tip.Status  = JsonTip.SUCCESS;
            tip.Message = "删除后台菜单成功";
            return(Json(tip));
        }
        public IActionResult EditAdminMenu(AdminMenu model)
        {
            if (!ModelState.IsValid)
            {
                string messages = string.Join("; ", ModelState.Values
                                              .SelectMany(x => x.Errors)
                                              .Select(x => x.ErrorMessage));
                tip.Message = messages;
                return(Json(tip));
            }
            if (model.Id <= 0)
            {
                tip.Message = "错误参数传递!";
                return(Json(tip));
            }
            AdminMenu entity = AdminMenu.Find(AdminMenu._.Id == model.Id);

            if (entity == null)
            {
                tip.Message = "系统找不到本记录!";
                return(Json(tip));
            }
            //赋值
            entity.MenuName = model.MenuName;
            //如果key修改了。判断是否有存在
            if (entity.MenuKey != model.MenuKey)
            {
                if (AdminMenu.FindCount(AdminMenu._.MenuKey == model.MenuKey & AdminMenu._.Id != entity.Id, null, null, 0, 0) > 0)
                {
                    tip.Message = "您修改的菜单KEY已经存在,请填写其他的!";
                    return(Json(tip));
                }
                entity.MenuKey = model.MenuKey;
            }
            entity.Link      = model.Link;
            entity.IsHide    = model.IsHide;
            entity.ClassName = model.ClassName;
            entity.Rank      = model.Rank;
            if (entity.PId != model.PId)
            {
                //重新获取level和location
                entity.Level    = AdminMenu.GetNewLevel(model.PId);
                entity.Location = AdminMenu.GetNewLocation(model.PId);
            }
            entity.PId = model.PId;
            entity.Update();

            //处理权限
            IList <AdminMenuEvent> oldevent = AdminMenuEvent.FindAll(AdminMenuEvent._.MenuId == entity.Id, null, null, 0, 0);

            //所有的权限
            IList <TargetEvent> listevent = TargetEvent.FindAll(TargetEvent._.Id > 0, TargetEvent._.Rank.Asc(), null, 0, 0);

            //新的权限
            string[] eventkeys           = Request.Form["eventkey"];
            List <AdminMenuEvent> listme = new List <AdminMenuEvent>();

            if (eventkeys != null && eventkeys.Length > 0)
            {
                foreach (string s in eventkeys)
                {
                    if (Utils.IsInt(s))
                    {
                        TargetEvent te = listevent.FirstOrDefault(t => t.Id == int.Parse(s));
                        if (te != null)
                        {
                            AdminMenuEvent tmp = new AdminMenuEvent();
                            tmp.EventId   = te.Id;
                            tmp.EventKey  = te.EventKey;
                            tmp.EventName = te.EventName;
                            tmp.MenuKey   = model.MenuKey;
                            tmp.MenuId    = entity.Id;
                            listme.Add(tmp);
                        }
                    }
                }
            }

            //判断是否在旧的里面
            listme.ForEach(s =>
            {
                var oe = oldevent.FirstOrDefault(o => o.EventId == s.EventId);
                //如果是旧列表存在,则删掉旧列表里面的,然后更新自己
                if (oe != null)
                {
                    s.Id = oe.Id;
                    s.Update();
                    oldevent.Remove(oe);
                }
                else
                {
                    //如果不存在,则是新增的。直接新增处理
                    s.Insert();
                }
            });
            //如果还有,则删除旧列表里面的
            if (oldevent != null && oldevent.Count > 0)
            {
                oldevent.Delete();
            }

            tip.Status    = JsonTip.SUCCESS;
            tip.Message   = "编辑后台菜单成功";
            tip.ReturnUrl = "close";
            return(Json(tip));
        }
示例#4
0
        private static void StartRegister()
        {
            if (AdminMenu.Meta.Count > 0)
            {
                return;
            }
            // 操作项定义
            var menuEventDic = new Dictionary <string, KeyValuePair <int, string> >
            {
                { "add", new KeyValuePair <int, string>(1, "添加") },
                { "edit", new KeyValuePair <int, string>(2, "修改") },
                { "del", new KeyValuePair <int, string>(3, "删除") },
                { "view", new KeyValuePair <int, string>(4, "查看") },
                { "viewlist", new KeyValuePair <int, string>(5, "查看列表") },
                { "import", new KeyValuePair <int, string>(6, "导入") },
                { "export", new KeyValuePair <int, string>(7, "导出") },
                { "filter", new KeyValuePair <int, string>(8, "搜索") },
                { "batch", new KeyValuePair <int, string>(9, "批量操作") },
                { "recycle", new KeyValuePair <int, string>(10, "回收站") },
                { "confirm", new KeyValuePair <int, string>(11, "确认") },
            };

            var asms = AppDomain.CurrentDomain.GetAssemblies();

            foreach (var asmItem in asms)
            {
                var types = asmItem.GetTypes().Where(e => e.Name.EndsWith("Controller")).ToList();
                if (types.Count == 0)
                {
                    continue;
                }

                foreach (var type in types)
                {
                    if (!type.Namespace.Contains(".AdminCP.Controller"))
                    {
                        continue;
                    }

                    // 控制器名称
                    var ctrName = type.Name.TrimEnd("Controller");

                    // 控制器展示名
                    var ctrDpName = type.GetDisplayName();

                    var ctrDes = type.GetDescription();

                    var url = ctrName;

                    var fAdminMenu = AdminMenu.Find(AdminMenu._.MenuKey == ctrName + "Sys" & AdminMenu._.Link == "#");
                    var nAdminMenu = new AdminMenu
                    {
                        Description = ctrDes,
                        IsHide      = 0,
                        Level       = 0,
                        Link        = "#",
                        Location    = "0,",
                        MenuKey     = ctrName + "Sys",
                        MenuName    = ctrDpName,
                        PId         = 0
                    };
                    var pAdminMenu = fAdminMenu ?? nAdminMenu;
                    if (pAdminMenu.Id > 0)
                    {
                        pAdminMenu.Description = nAdminMenu.Description;
                        pAdminMenu.MenuName    = nAdminMenu.MenuName;
                    }

                    // 控制器所有包含MyAuthorizeAttribute的方法
                    var acts = type.GetMethods()
                               .Where(w => w.IsDefined(typeof(MyAuthorizeAttribute)))
                               .ToList();

                    if (!acts.Any())
                    {
                        continue;
                    }

                    pAdminMenu.Save();

                    var eventKeyDic        = new Dictionary <string, int>();
                    var adminMenuEventList = new List <AdminMenuEvent>();

                    foreach (var method in acts)
                    {
                        var dn          = method.GetDisplayName();
                        var methodDes   = method.GetDescription();
                        var actName     = method.Name;
                        var myAuthorize = method.GetCustomAttribute <MyAuthorizeAttribute>();

                        var menuKey    = myAuthorize.GetValue("_menuKey").ToString();
                        var eventKey   = myAuthorize.GetValue("_eventKey").ToString();
                        var returnType = myAuthorize.GetValue("_returnType").ToString();

                        //保存菜单
                        if (eventKey == "viewlist" && returnType == "HTML")
                        {
                            var adminMenu = AdminMenu.Find(AdminMenu._.MenuKey == menuKey & AdminMenu._.Link == url + "/" + actName) ?? new AdminMenu
                            {
                                Description = methodDes,
                                IsHide      = 0,
                                Level       = pAdminMenu.Level + 1,
                                Link        = url + "/" + actName,
                                Location    = pAdminMenu.Location + ",",
                                MenuKey     = menuKey,
                                MenuName    = dn,
                                PId         = pAdminMenu.Id
                            };
                            adminMenu.Save();

                            eventKeyDic[menuKey] = adminMenu.Id;
                        }

                        // 保存操作
                        var adminmenuevent = AdminMenuEvent.Find(AdminMenuEvent._.MenuId == menuEventDic[eventKey].Key
                                                                 & AdminMenuEvent._.MenuKey == menuKey & AdminMenuEvent._.EventKey == eventKey)
                                             ?? new AdminMenuEvent
                        {
                            MenuKey   = menuKey,
                            MenuId    = menuEventDic[eventKey].Key,
                            EventKey  = eventKey,
                            EventName = menuEventDic[eventKey].Value
                        };
                        adminMenuEventList.Add(adminmenuevent);
                    }

                    adminMenuEventList.ForEach(f => f.MenuId = eventKeyDic.ContainsKey(f.MenuKey) ? eventKeyDic[f.MenuKey] : 0);
                    adminMenuEventList.Save();
                }
            }
        }