ABP框架学习-菜单设计

目的介绍

实现一个类似于abp.zero的项目。在完成项目的过程中学习ABP框架知识。

功能列表

我们对项目做功能分析。归类出下以功能功知识点。
1.角色
2.测试项目
3.用户
4.权限
5.组织
6.登陆
7.菜单
8.日志
9.错误处理
10.并发处理
11.用队列方式解决高并发请求。

功能介绍

菜单分系统菜单,平台菜单。菜单方法等。最终菜单会跟角色关联组成权限管理。这里看了ABP的授权管理,感觉自已之前用的权限管理有冲突。所以不打算使用ABP的方式管理。

菜单结构定义

1.权限的最小单位方法

public class SysAction : DbSetBase
    {
        public SysAction()
        {
            SystemId = "000";
        }

        [MaxLength(40)]
        [Required]
        public string ActionDisplayName { get; set; }

        [MaxLength(40)]
        [Required]
        public string ActionName { get; set; }

        [MaxLength(50)]
        [Required]
        public string SystemId { get; set; }

        public virtual ICollection<SysControllerSysAction> SysControllerSysActions { get; set; }
    }

2.菜单类第个菜单都有不同的方法。如果方法不够可以增加方法。

public class SysController : DbSetBase
    {
        public SysController()
        {
            SystemId = "000";
            TargetBlank = false;
            ControllerName = "Index";
            ActionName = "Index";
            Display = true;
            Enabled = true;
            Ico = "icon-list-ul";

            SysControllerSysActions = new List<SysControllerSysAction>();
        }

        [Display(Name = "Area")]

        [Required]
        [ForeignKey("SysArea")]
        public Guid SysAreaId { get; set; }

        [ScaffoldColumn(false)]
        public virtual SysArea SysArea { get; set; }

        [MaxLength(50)]
        [Required]
        public string ControllerDisplayName { get; set; }

        [MaxLength(50)]
        public string ControllerName { get; set; }

        [MaxLength(50)]
        public string ActionName { get; set; }

        [MaxLength(50)]
        public string Parameter { get; set; }

        [MaxLength(50)]
        [Required]
        public string SystemId { get; set; }

        public bool Display { get; set; }

        public bool Enabled { get; set; }

        public bool TargetBlank { get; set; }

        [DataType("Ico")]
        public string Ico { get; set; }

        [Display(Name = "Action")]
        [DataType("MultiSelectList")]
        public List<Guid> SysActionsId { get; set; }

        public virtual ICollection<SysControllerSysAction> SysControllerSysActions { get; set; }
    }

3.菜单对应方法关系表

public class SysControllerSysAction : DbSetRelationBase
    {
        [ForeignKey("SysController")]
        public Guid SysControllerId { get; set; }

        public virtual SysController SysController { get; set; }


        [ForeignKey("SysAction")]
        public Guid SysActionId { get; set; }

        public virtual SysAction SysAction { get; set; }

        public virtual ICollection<SysRoleSysControllerSysAction> SysRoleSysControllerSysActions { get; set; }
    }

4.平台表 SysArea 关联菜单,使不同平台拥有不同的菜单

public class SysArea : DbSetBase
    {
        public SysArea()
        {
            SystemId = "000";
        }

        [MaxLength(40)]
        [Required]
        public string AreaDisplayName { get; set; }

        [MaxLength(40)]
        [Required]
        public string AreaName { get; set; }

        [MaxLength(30)]
        [Required]
        public string SystemId { get; set; }

        public virtual ICollection<SysController> SysControllers { get; set; }
    }

5.权限。即角色相关的菜单

public class SysRoleSysControllerSysAction : DbSetRelationBase
   {
       [ForeignKey("SysRole")]
       public Guid SysRoleId { get; set; }

       public virtual SysRole SysRole { get; set; }

       [ForeignKey("SysControllerSysAction")]
       public Guid SysControllerSysActionId { get; set; }

       public virtual SysControllerSysAction SysControllerSysAction { get; set; }
   }

生成菜单应用服务

public class SysControllerAppService : AppServiceBase<SysController, SysControllerDto, Guid, PageListInput, UpdateSysControllerDto, UpdateSysControllerDto>, 
   	ISysControllerAppService
   {
       public SysControllerAppService(
   			IRepository<SysController, Guid> repository
           )
           : base(repository)
       {
   	
       }
   }

菜单初始化

一般情况下菜单都是由开发人员指定死的。所以还需要一个初始菜单的功能。

  internal sealed class Configuration : DbMigrationsConfiguration<Zero.EntityFramework.ZeroDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
            ContextKey = "Zero";
        }

        protected override void Seed(Zero.EntityFramework.ZeroDbContext context)
        {
            // This method will be called every time after migrating to the latest version.
            // You can add any seed data here...
            // 初始化信息

            #region SysArea

            var sysAreas = new[]
            {
                new SysArea
                {
                    AreaName = "Login",
                    AreaDisplayName = "用户登录",
                    SystemId = "000"
                },
                new SysArea
                {
                    AreaName = "Platform",
                    AreaDisplayName = "操作平台",
                    SystemId = "001"
                },
                new SysArea
                {
                    AreaName = "Admin",
                    AreaDisplayName = "管理平台",
                    SystemId = "002"
                }
            };
            context.SysAreas.AddOrUpdate(a => new { a.AreaName, a.AreaDisplayName, a.SystemId }, sysAreas);

            #endregion

            #region SysAction

            var sysActions = new[]
            {
                new SysAction
                {
                    ActionDisplayName = "列表",
                    ActionName = "Index",
                    SystemId = "001"
                },
                new SysAction
                {
                    ActionDisplayName = "详细",
                    ActionName = "Details",
                    SystemId = "003"
                },
                new SysAction
                {
                    ActionDisplayName = "新建",
                    ActionName = "Create",
                    SystemId = "004"
                },
                new SysAction
                {
                    ActionDisplayName = "编辑",
                    ActionName = "Edit",
                    SystemId = "005"
                },
                new SysAction
                {
                    ActionDisplayName = "删除",
                    ActionName = "Delete",
                    SystemId = "006"
                },
                new SysAction
                {
                    ActionDisplayName = "导入",
                    ActionName = "Import",
                    SystemId = "007"
                },
                new SysAction
                {
                    ActionDisplayName = "导出",
                    ActionName = "Export",
                    SystemId = "008"
                }
            };
            context.SysActions.AddOrUpdate(a => new { a.ActionName, a.SystemId, a.ActionDisplayName }, sysActions);

            #endregion

            #region SysController

            var sysControllers = new[]
            {
                // Platform
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Platform").Id,
                    ControllerDisplayName = "操作平台",
                    ControllerName = "Index",
                    SystemId = "100",
                    Display = false,
                },
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Admin").Id,
                    ControllerDisplayName = "管理平台",
                    ControllerName = "Index",
                    SystemId = "100",
                    Display = false,
                },
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Platform").Id,
                    ControllerDisplayName = "桌面",
                    ControllerName = "Desktop",
                    SystemId = "100100",
                    Display = false,
                },

                #region 平台通用菜单
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Platform").Id,
                    ControllerDisplayName = "管理",
                    ControllerName = "SysUser",
                    SystemId = "700",
                    Ico = "fa-cog"
                },
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Platform").Id,
                    ControllerDisplayName = "组织架构",
                    ControllerName = "SysDepartment",
                    SystemId = "700200",
                    Ico = "fa-sitemap"
                },
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Platform").Id,
                    ControllerDisplayName = "用户日志",
                    ControllerName = "SysUserLog",
                    SystemId = "700900",
                    Ico = "fa-calendar-o"
                },
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Platform").Id,
                    ControllerDisplayName = "角色管理",
                    ControllerName = "SysRole",
                    SystemId = "700300",
                    Ico = "fa-users"
                },
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Platform").Id,
                    ControllerDisplayName = "用户管理",
                    ControllerName = "SysUser",
                    SystemId = "700400",
                    Ico = "fa-user"
                },
                #endregion

                #region 后台管理菜单
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Admin").Id,
                    ControllerDisplayName = "企业管理",
                    ControllerName = "Index",
                    SystemId = "700",
                },
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Admin").Id,
                    ControllerDisplayName = "企业管理",
                    ControllerName = "SysEnterprise",
                    SystemId = "700200",
                },
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Admin").Id,
                    ControllerDisplayName = "角色管理",
                    ControllerName = "SysRole",
                    SystemId = "700300",
                },
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Admin").Id,
                    ControllerDisplayName = "用户管理",
                    ControllerName = "SysUser",
                    SystemId = "700400",
                },
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Admin").Id,
                    ControllerDisplayName = "系统设置",
                    ControllerName = "Index",
                    SystemId = "800",
                },
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Admin").Id,
                    ControllerDisplayName = "系统参数",
                    ControllerName = "WebConfigAppSetting",
                    SystemId = "800100",
                },
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Admin").Id,
                    ControllerDisplayName = "系统区域",
                    ControllerName = "SysArea",
                    SystemId = "800200",
                },
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Admin").Id,
                    ControllerDisplayName = "操作类型",
                    ControllerName = "SysAction",
                    SystemId = "800300",
                },
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Admin").Id,
                    ControllerDisplayName = "系统模块",
                    ControllerName = "SysController",
                    SystemId = "800400",
                },
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Admin").Id,
                    ControllerDisplayName = "帮助信息",
                    ControllerName = "SysHelp",
                    SystemId = "800900",
                },
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Admin").Id,
                    ControllerDisplayName = "系统信息",
                    ControllerName = "Index",
                    SystemId = "900",
                },
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Admin").Id,
                    ControllerDisplayName = "统计信息",
                    ControllerName = "SysStatistic",
                    SystemId = "900100",
                },
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Admin").Id,
                    ControllerDisplayName = "用户日志",
                    ControllerName = "SysUserLog",
                    SystemId = "900300",
                },
                new SysController
                {
                    SysAreaId = sysAreas.Single(a => a.AreaName == "Admin").Id,
                    ControllerDisplayName = "系统日志",
                    ControllerName = "SysLog",
                    SystemId = "900400",
                },
                #endregion


                #region 用户自定义菜单
                
                #endregion 
            };
            context.SysControllers.AddOrUpdate(
                a => new { a.SysAreaId, a.SystemId }, sysControllers);

            #endregion

            #region SysControllerSysAction

            SysControllerSysAction[] sysControllerSysActions = (from sysAction in sysActions
                                                                from sysController in sysControllers
                                                                select
                                                                    new SysControllerSysAction
                                                                    {
                                                                        SysActionId = sysAction.Id,
                                                                        SysControllerId = sysController.Id
                                                                    }).ToArray();

            context.SysControllerSysActions.AddOrUpdate(a => new { a.SysActionId, a.SysControllerId },
                sysControllerSysActions);

            #endregion

            #region 初始基础信息



            #endregion

        }
    }

在EF数据仓储服务下的Configuration 类的Seed 方法里增加初始代码。初始化菜单和管理员权限。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值