完成导航动态加载

This commit is contained in:
yubaolee 2015-11-22 21:32:38 +08:00
parent cd31e8791d
commit 2793d20ef4
8 changed files with 177 additions and 98 deletions

View File

@ -4,6 +4,7 @@ using OpenAuth.Domain.Interface;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenAuth.App.ViewModel;
namespace OpenAuth.App namespace OpenAuth.App
{ {
@ -16,18 +17,6 @@ namespace OpenAuth.App
_repository = repository; _repository = repository;
} }
public int GetModuleCntInOrg(int orgId)
{
if (orgId == 0)
{
return _repository.Find(null).Count();
}
else
{
return _repository.GetModuleCntInOrgs(GetSubOrgIds(orgId));
}
}
/// <summary> /// <summary>
/// 加载一个部门及子部门全部Modules /// 加载一个部门及子部门全部Modules
/// </summary> /// </summary>
@ -61,6 +50,18 @@ namespace OpenAuth.App
return _repository.Find(u => u.ParentId == 0).ToList(); return _repository.Find(u => u.ParentId == 0).ToList();
} }
public List<ModuleView> LoadByParent(int parentId)
{
var modules = new List<ModuleView>();
var roots = _repository.Find(u => u.ParentId == parentId);
foreach (var module in roots)
{
ModuleView mv = module;
mv.Childern = LoadByParent(module.Id);
modules.Add(mv);
}
return modules;
}
public Module Find(int id) public Module Find(int id)
@ -141,6 +142,5 @@ namespace OpenAuth.App
#endregion #endregion
} }
} }

View File

@ -49,6 +49,7 @@
<Compile Include="UserManagerApp.cs" /> <Compile Include="UserManagerApp.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="OrgManagerApp.cs" /> <Compile Include="OrgManagerApp.cs" />
<Compile Include="ViewModel\ModuleView.cs" />
<Compile Include="ViewModel\UserView.cs" /> <Compile Include="ViewModel\UserView.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,55 @@
using System.Collections.Generic;
using Infrastructure;
using OpenAuth.Domain;
namespace OpenAuth.App.ViewModel
{
public class ModuleView
{
/// <summary>
/// ID
/// </summary>
/// <returns></returns>
public int Id { get; set; }
/// <summary>
/// 组织名称
/// </summary>
/// <returns></returns>
public string Name { get; set; }
/// <summary>
/// 主页面URL
/// </summary>
/// <returns></returns>
public string Url { get; set; }
/// <summary>
/// 父节点流水号
/// </summary>
/// <returns></returns>
public int ParentId { get; set; }
/// <summary>
/// 节点图标文件名称
/// </summary>
/// <returns></returns>
public string IconName { get; set; }
/// <summary>
/// 子节点
/// </summary>
public List<ModuleView> Childern = new List<ModuleView>();
public static implicit operator ModuleView(Module module)
{
return AutoMapperExt.ConvertTo<Module, ModuleView>(module);
}
public static implicit operator Module(ModuleView view)
{
return AutoMapperExt.ConvertTo<ModuleView, Module>(view);
}
}
}

View File

@ -1,12 +1,25 @@
using System.Web.Mvc; using System.Web.Mvc;
using Infrastructure;
using OpenAuth.App;
namespace OpenAuth.Mvc.Controllers namespace OpenAuth.Mvc.Controllers
{ {
public class HomeController : BaseController public class HomeController : BaseController
{ {
private ModuleManagerApp _app;
public HomeController()
{
_app = (ModuleManagerApp)DependencyResolver.Current.GetService(typeof(ModuleManagerApp));
}
public string GetModules(int parentId = 0)
{
return JsonHelper.Instance.Serialize(_app.LoadByParent(parentId));
}
public ActionResult Index() public ActionResult Index()
{ {
return View(); return View(_app.LoadByParent(0));
} }
public ActionResult Main() public ActionResult Main()

View File

@ -23,6 +23,32 @@ namespace OpenAuth.Mvc.Controllers
return View(); return View();
} }
/// <summary>
/// 加载模块下面的所有模块
/// </summary>
public string Load(int orgId, int pageCurrent = 1, int pageSize = 30)
{
return JsonHelper.Instance.Serialize(_app.Load(orgId, pageCurrent, pageSize));
}
/// <summary>
/// 加载tree结构
/// </summary>
public string LoadForTree(bool bAll=false)
{
var orgs = _app.LoadForTree(bAll);
//添加根节点
orgs.Add(new Module
{
Id = 0,
ParentId = -1,
Name = "根节点",
CascadeId = "0"
});
return JsonHelper.Instance.Serialize(orgs);
}
public ActionResult Add(int id = 0) public ActionResult Add(int id = 0)
{ {
return View(_app.Find(id)); return View(_app.Find(id));
@ -45,37 +71,6 @@ namespace OpenAuth.Mvc.Controllers
return JsonHelper.Instance.Serialize(BjuiResponse); return JsonHelper.Instance.Serialize(BjuiResponse);
} }
/// <summary>
/// 加载模块下面的所有模块
/// </summary>
public string Load(int orgId, int pageCurrent = 1, int pageSize = 30)
{
return JsonHelper.Instance.Serialize(_app.Load(orgId, pageCurrent, pageSize));
}
/// <summary>
/// 加载tree结构
/// </summary>
public string LoadForTree(bool bAll=false)
{
var orgs = _app.LoadForTree(bAll);
//添加根节点
orgs.Add(new Module
{
Id = 0,
ParentId = -1,
Name = "全部模块",
CascadeId = "0"
});
return JsonHelper.Instance.Serialize(orgs);
}
//获取模块下面模块个数
public int GetCount(int orgId)
{
return _app.GetModuleCntInOrg(orgId);
}
public string Delete(string Id) public string Delete(string Id)
{ {
try try

View File

@ -1,4 +1,5 @@
<!DOCTYPE html> @model List<OpenAuth.App.ViewModel.ModuleView>
<!DOCTYPE html>
<html lang="zh"> <html lang="zh">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
@ -84,76 +85,70 @@
}, },
debug: true, // [可选]调试模式 [true|false默认false] debug: true, // [可选]调试模式 [true|false默认false]
theme: 'sky' // 若有Cookie['bjui_theme'],优先选择Cookie['bjui_theme']。皮肤[五种皮肤:default, orange, purple, blue, red, green] theme: 'sky' // 若有Cookie['bjui_theme'],优先选择Cookie['bjui_theme']。皮肤[五种皮肤:default, orange, purple, blue, red, green]
}) });
// main - menu // main - menu
$('#bjui-accordionmenu') $('#bjui-accordionmenu')
.collapse() .collapse()
.on('hidden.bs.collapse', function (e) { .on('hidden.bs.collapse', function (e) {
$(this).find('> .panel > .panel-heading').each(function () { $(this).find('> .panel > .panel-heading').each(function () {
var $heading = $(this), $a = $heading.find('> h4 > a') var $heading = $(this), $a = $heading.find('> h4 > a');
if ($a.hasClass('collapsed')) $heading.removeClass('active');
if ($a.hasClass('collapsed')) $heading.removeClass('active') });
})
}) })
.on('shown.bs.collapse', function (e) { .on('shown.bs.collapse', function (e) {
$(this).find('> .panel > .panel-heading').each(function () { $(this).find('> .panel > .panel-heading').each(function () {
var $heading = $(this), $a = $heading.find('> h4 > a') var $heading = $(this), $a = $heading.find('> h4 > a');
if (!$a.hasClass('collapsed')) $heading.addClass('active');
if (!$a.hasClass('collapsed')) $heading.addClass('active') });
}) });
})
$(document).on('click', 'ul.menu-items > li > a', function (e) { $(document).on('click', 'ul.menu-items > li > a', function (e) {
var $a = $(this), $li = $a.parent(), options = $a.data('options').toObj() var $a = $(this), $li = $a.parent(), options = $a.data('options').toObj();
var onClose = function () { var onClose = function () {
$li.removeClass('active') $li.removeClass('active');
} };
var onSwitch = function () { var onSwitch = function () {
$('#bjui-accordionmenu').find('ul.menu-items > li').removeClass('switch') $('#bjui-accordionmenu').find('ul.menu-items > li').removeClass('switch');
$li.addClass('switch') $li.addClass('switch');
} };
$li.addClass('active');
$li.addClass('active')
if (options) { if (options) {
options.url = $a.attr('href') options.url = $a.attr('href');
options.onClose = onClose options.onClose = onClose;
options.onSwitch = onSwitch options.onSwitch = onSwitch;
if (!options.title) options.title = $a.text() if (!options.title) options.title = $a.text();
if (!options.target) if (!options.target)
$a.navtab(options) $a.navtab(options);
else else
$a.dialog(options) $a.dialog(options);
} }
e.preventDefault() e.preventDefault();
}) });
//时钟 //时钟
var today = new Date(), time = today.getTime() var today = new Date(), time = today.getTime();
$('#bjui-date').html(today.formatDate('yyyy/MM/dd')) $('#bjui-date').html(today.formatDate('yyyy/MM/dd'));
setInterval(function () { setInterval(function () {
today = new Date(today.setSeconds(today.getSeconds() + 1)) today = new Date(today.setSeconds(today.getSeconds() + 1));
$('#bjui-clock').html(today.formatDate('HH:mm:ss')) $('#bjui-clock').html(today.formatDate('HH:mm:ss'));
}, 1000) }, 1000);
})
});
//菜单-事件 //菜单-事件
function MainMenuClick(event, treeId, treeNode) { function MainMenuClick(event, treeId, treeNode) {
event.preventDefault() event.preventDefault();
//if (treeNode.isParent) {
if (treeNode.isParent) { // var zTree = $.fn.zTree.getZTreeObj(treeId);
var zTree = $.fn.zTree.getZTreeObj(treeId) // zTree.expandNode(treeNode, !treeNode.open, false, true, true);
// return;
zTree.expandNode(treeNode, !treeNode.open, false, true, true) //}
return
}
if (treeNode.target && treeNode.target == 'dialog') if (treeNode.target && treeNode.target == 'dialog')
$(event.target).dialog({ id: treeNode.tabid, url: treeNode.url, title: treeNode.name }) $(event.target).dialog({ id: treeNode.tabid, url: treeNode.url, title: treeNode.name });
else else
$(event.target).navtab({ id: treeNode.tabid, url: treeNode.url, title: treeNode.name, fresh: treeNode.fresh, external: treeNode.external }) $(event.target).navtab({ id: treeNode.tabid, url: treeNode.url, title: treeNode.name, fresh: treeNode.fresh, external: treeNode.external });
} }
</script> </script>
</head> </head>
@ -200,19 +195,30 @@
<ul id="bjui-hnav-navbar"> <ul id="bjui-hnav-navbar">
<li class="active"> <li class="active">
<a href="javascript:;" data-toggle="slidebar"><i class="fa fa-check-square-o"></i> 应用管理</a> <a href="javascript:;" data-toggle="slidebar"><i class="fa fa-check-square-o"></i> 应用管理</a>
<div class="items hide" data-noinit="true"> <div class="items hide" data-noinit="true" id="navTree">
<ul id="bjui-doc-tree-base" class="ztree ztree_main" data-toggle="ztree" data-on-click="MainMenuClick" <ul id="bjui-doc-tree-base" class="ztree ztree_main" data-toggle="ztree" data-on-click="MainMenuClick"
data-expand-all="true" data-faicon="star-o" data-tit="基本设置"> data-expand-all="true" data-faicon="star-o" data-tit="控制台">
<li data-id="99" data-pid="0" data-faicon="folder-open-o" data-faicon-close="folder-o">系统设置</li>
<li data-id="100" data-pid="99" data-url="OrgManager/Index" data-tabid="orgManager" data-faicon="caret-right">机构管理</li> @foreach (var module in Model)
<li data-id="101" data-pid="99" data-url="UserManager/Index" data-tabid="userManager" data-faicon="caret-right">用户管理</li> {
<li data-id="102" data-pid="99" data-url="RoleManager/Index" data-tabid="roleManager" data-faicon="caret-right">角色管理</li> <li data-id="@module.Id" data-pid="0" data-faicon="folder-open-o" data-faicon-close="folder-o">@module.Name</li>
<li data-id="103" data-pid="99" data-url="ModuleManager/Index" data-tabid="moduleManager" data-faicon="caret-right">模块管理</li>
foreach (var child in module.Childern)
{
<li data-id="@child.Id" data-pid="@child.ParentId"
data-faicon="caret-right"
data-url="@child.Url" data-tabid="@child.Id">@child.Name</li>
}
}
</ul> </ul>
</div> </div>
</li> </li>
<li class="dropdown"> <li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-cog"></i> 系统设置 <span class="caret"></span></a> <a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-cog"></i> 系统设置 <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu"> <ul class="dropdown-menu" role="menu">
<li><a href="#">角色权限</a></li> <li><a href="#">角色权限</a></li>
<li><a href="#">用户列表</a></li> <li><a href="#">用户列表</a></li>

View File

@ -45,8 +45,7 @@
<tr> <tr>
<td> <td>
<label for="IconName" class="control-label x105">节点图标文件名称:</label> <label for="IconName" class="control-label x105">节点图标文件名称:</label>
<input type="text" name="IconName" id="IconName" value="@Model.IconName" <input type="text" name="IconName" id="IconName" value="@Model.IconName" size="20">
data-rule="required" size="20">
</td> </td>
</tr> </tr>
<tr> <tr>
@ -62,8 +61,7 @@
<tr> <tr>
<td> <td>
<label for="Vector" class="control-label x105">矢量图标:</label> <label for="Vector" class="control-label x105">矢量图标:</label>
<input type="text" name="Vector" id="Vector" value="@Model.Vector" <input type="text" name="Vector" id="Vector" value="@Model.Vector" size="20">
data-rule="required" size="20">
</td> </td>
</tr> </tr>
<tr> <tr>

View File

@ -82,6 +82,17 @@ namespace OpenAuth.UnitTest
} }
[TestMethod]
public void TestLoad()
{
var modules = _app.LoadByParent(0);
foreach (var module in modules)
{
Console.WriteLine(module.Name);
}
}
public Module Add(int parent = 0) public Module Add(int parent = 0)
{ {
var module = new Module() var module = new Module()