EasyUIYbSoftwareFactory 代码生成插件【十一】:ASP.NET WebApi MVC下组织机构管理与菜单权限管理之落实

     YbSoftwareFactory
的 最新 ASP.NET MVC
插件所非常成的类别时支撑组织机关管理暨菜单权限管理效能,如下将简单介绍该促成之片段基本代码。

1、组织机构管理之兑现

   
组织机构管理应用 Provider 模式实现,使用 ADO.NET 进行多少看,如下是
Provider
实现类似吃之初始化方法,从中可看出,通过当config文件中指定tableName可以随意定义对应的表名称,默认情况下为表的称呼为
YbOrganizations 表。

#region Initialize

        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            // Validate arguments
            if (config == null) throw new ArgumentNullException(“config”);
            if (string.IsNullOrEmpty(name)) name = “YbOrganizationProvider”;
            if (String.IsNullOrEmpty(config[“description”]))
            {
                config.Remove(“description”);
                config.Add(“description”, “Yb organization provider”);
            }
            if (String.IsNullOrEmpty(config[“tableName”]))
            {
                config.Remove(“tableName”);
                config.Add(“tableName”, “YbOrganizations”);
            }
            // Initialize base class
            base.Initialize(name, config);

            // Read connection string
            this.ConnectionStringName = config.GetConfigValue(“connectionStringName”, null);
            if (string.IsNullOrWhiteSpace(this.ConnectionStringName)) throw new ConfigurationErrorsException(Resources.Required_connectionStringName_attribute_not_specified);
            this.connectionStringSetting = ConfigurationManager.ConnectionStrings[this.ConnectionStringName];
            if (this.connectionStringSetting == null) throw new ConfigurationErrorsException(string.Format(Resources.Format_connection_string_was_not_found, this.ConnectionStringName));
            if (string.IsNullOrEmpty(this.connectionStringSetting.ProviderName)) throw new ConfigurationErrorsException(string.Format(Resources.Format_connection_string_does_not_have_specified_the_providerName_attribute, this.ConnectionStringName));

            //激发设置连接字符串前之事件处理程序,主要目的是解密连接字符串
            ConnectionStringChangingEventArgs args = RaiseConnectionStringChangingEvent(connectionStringSetting.ConnectionString);
            if (args == null) throw new ProviderException(Resources.Connection_string_cannot_be_blank);
            if (!this.connectionStringSetting.ConnectionString.Equals(args.ConnectionString))
            {
                this.connectionStringSetting =
                    new ConnectionStringSettings(this.ConnectionStringName, args.ConnectionString, this.connectionStringSetting.ProviderName);
            }
            if (string.IsNullOrEmpty(this.connectionStringSetting.ConnectionString)) throw new ProviderException(Resources.Connection_string_cannot_be_blank);

            this.applicationName = config[“applicationName”];

            this.tableName = config[“tableName”];
            SecUtility.CheckParameter(ref tableName, true, true, true, 256, “tableName”);
        }

        #endregion

 

   
我们好透过Web API发布组织部门管理之关于接口,这样JQuery
EasyUI的前端界面在展示与管制数据时能更加得心应手,同时这些接口还能通过WinForm客户端、WPF客户端,其他Web应用程序及各种类型的智能手机客户端访问,可谓一举N得,呵呵。如下是Web
Api相应Controller的骨干代码实现:

 

// GET api/item/id
        public Organization Get(Guid id)
        {
            string key = string.Format(CacheKeyList.ORGANIZATION_BY_KEY,id);
            return _cacheManager.Get(key, () =>
            {
                var item = OrganizationApi.GetOrganization(id);
                return item;
            });
        }

        // POST api/item
        public HttpResponseMessage Post(Organization item)
        {
            item.DisplayOrder = OrganizationApi.GetMaxDisplayOrder()+1024d;
            item.CreatedBy = User.Identity.Name;
            OrganizationApi.CreateOrganization(item);

            var msg = new Message(true, “数据增长成功”) { data = item };
            var response = Request.CreateResponse(HttpStatusCode.Created);
            var uri = Url.Link(“DefaultApi”, new { id = item.OrganizationId });
            response.Headers.Location = new Uri(uri);
            response.Content = new StringContent(msg.ToString(), Encoding.UTF8);
            return response;
        }

        // PUT api/item/id
        [System.Web.Http.HttpPost]
        [System.Web.Http.HttpPut]
        [System.Web.Http.AcceptVerbs(“POST”, “PUT”)]
        public HttpResponseMessage Put(Guid id, Organization item)
        {
            item.LastUpdatedBy = User.Identity.Name;
            OrganizationApi.UpdateOrganization(item);
            //移除缓存
            _cacheManager.Remove(string.Format(CacheKeyList.ORGANIZATION_BY_KEY, item.OrganizationId));

            var msg = new Message(true, “数据保存成功”) { data = item };
            var response = Request.CreateResponse(HttpStatusCode.OK);
            response.Content = new StringContent(msg.ToString(), Encoding.UTF8);
            return response;
        }

        // DELETE api/item/id
        public HttpResponseMessage Delete(Guid id)
        {
            OrganizationApi.HardDeleteOrganization(id);
            //移除缓存
            _cacheManager.Remove(string.Format(CacheKeyList.ORGANIZATION_BY_KEY, id));

            return new HttpResponseMessage(HttpStatusCode.NoContent);
        }
    }

 

   
最终实现之界面效果图如下,可以好的排序和升降级:

EasyUI 1

 

   
组织机关的编制界面如下:

EasyUI 2

 

2、菜单权限管理的落实

   
YbRapidSolution for
MVC的权位项分为面板、菜单与按钮三类,顶层菜单被默不作声认为分组面板并当左手的导航区域块题中显得,同时每个分组面板还支持树形结构菜单。首先来瞧界面效果(和组织机关管理均等,可以便宜地经右键菜单实现排序和升降级):

EasyUI 3

   
在JQuery EasyUI的Tree中,每个子节点需要一个称号为 _parentId
的习性,而顶层节点则非能够包含这个特性,否则你是无能为力以JQuery
EasyUI中显出树形结构的(如果您的次第始终无法出示有树形结构,则非常可能就是是者缘故)。如下是权子项的模型类,供需要之童鞋参考:

public class ChildPermissionModel : PermissionModel
    {
        public ChildPermissionModel(Permission permission)
            : base(permission)
        {
        }
        public object _parentId { get { return this.ParentPermissionKey; } }
    }

   
添加菜单项的界面效果如下:

EasyUI 4

   
通过“点击选择图标”按钮可以非常有益地挑以及保管菜单导航图标。而生图则是增长按钮的界面效果图:

EasyUI 5

 

   于JQuery EasyUI中设促成右键菜单功能实在是非常容易的,最后,附上JQuery
EasyUI的行右键菜单的兑现代码,一切就是如此简单,继续供应需要之童鞋参考:

 

function createRowContextMenu() {
    var tmenu = $(‘<div
id=”rmenu” style=”width:100px;”></div>’).appendTo(‘body’);
    $(‘<div
iconCls=”icon-goup”/>’).html(‘上移’).appendTo(tmenu);
    $(‘<div
iconCls=”icon-godown”/>’).html(‘下移’).appendTo(tmenu);
    $(‘<div
iconCls=”icon-arrowleft”/>’).html(‘升级’).appendTo(tmenu);
    $(‘<div
iconCls=”icon-arrowright”/>’).html(‘降级’).appendTo(tmenu);
    tmenu.menu({
        onClick: function (item)
{
            if (item.iconCls ==
‘icon-goup’ || item.iconCls == ‘icon-godown’) {
                var isUp =
item.iconCls == ‘icon-goup’;
                $.ajax({
                    url: virpath +
‘/Organization/UpdateDisplayOrder?id=’ + curRowData.OrganizationId +
‘&isUp=’ + isUp,
                    type: ‘Post’,
                    datatype: ‘application/json; charset=utf-8’,
                    error: function
(result) {
                        Msgalert(‘错误’, ‘操作失败:’ + result,
‘error’);
                    },
                    success: function (result) {
                        //result为要处理后底归值  
                        result = result.replace(/\\/g, ”);
                        result = eval(‘(‘ + result + ‘)’);
                        if
(result.success) {
                            $.messager.show({
                                title: ‘成功’,
                                msg: result.msg,
                                timeout: 2000,
                                showType: ‘fade’
                            });
                            grid.treegrid(‘reload’);

                        } else {
                            Msgalert(‘错误’, result.msg, ‘error’);
                        }
                    }
                });
            }
            else if (item.iconCls == ‘icon-arrowleft’ ||
item.iconCls == ‘icon-arrowright’) {
                var isLeft =
item.iconCls == ‘icon-arrowleft’;
                $.ajax({
                    url: virpath + ‘/Organization/UpdateLevel?id=’ +
curRowData.OrganizationId + ‘&isUp=’ + isLeft,
                    type: ‘Post’,
                    datatype: ‘application/json; charset=utf-8’,
                    error: function
(result) {
                        Msgalert(‘错误’, ‘操作失败:’ + result,
‘error’);
                    },
                    success: function (result) {
                        //result为要处理后底回到值  
                        result = result.replace(/\\/g, ”);
                        result = eval(‘(‘ + result + ‘)’);
                        if
(result.success) {
                            $.messager.show({
                                title: ‘成功’,
                                msg: result.msg,
                                timeout: 2000,
                                showType: ‘fade’
                            });
                            grid.treegrid(‘reload’);

                        } else {
                            Msgalert(‘错误’, result.msg, ‘error’);
                        }
                    }
                });
            }
        }
    });
}

    

   
在生单版本中,我们以实现一个而充分有益开展调用的自定义 Setting
管理类,能十分方便地处理应用程序中的各种配置信息。

   
最后附个在线 Demo 地址:http://mvcdemo.yellbuy.com/。