YbSoftwareFactory 代碼生成插件【十一】: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 ==  nullthrow  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 ==  nullthrow  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 ==  nullthrow  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,  truetruetrue256" 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);
        }
    }

 

    最終實現的界面效果圖如下,可以方便的排序和升降級:

 

    組織機構的編輯界面如下:

 

2、菜單權限管理的實現

    YbRapidSolution for MVC的權限項分為面板、菜單和按鈕三類,頂層菜單被默認為分組面板並在左邊的導航區域塊標題中顯示,同時每個分組面板還支持樹形結構菜單。首先來看看界面效果(和組織機構管理一樣,可以方便地通過右鍵菜單實現排序和升降級):

    在JQuery EasyUI的Tree中,每個子節點需要一個名稱為 _parentId 的屬性,而頂層節點則不能包含這個屬性,否則你是無法在JQuery EasyUI中顯示出樹形結構的(如果你的程序始終無法顯示出樹形結構,則很可能就是這個原因)。如下是權限子項的模型類,供需要的童鞋參考:

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

    添加菜單項的界面效果如下:

    通過“點擊選擇圖標”按鈕可以很方便地選擇和管理菜單導航圖標。而下圖則是添加按鈕的界面效果圖:

 

   在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/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM