Web应用程序系统的多用户权限控制设计及实现-栏目模块【8】
前五章均是从整体上讲述了Web应用程序的多用户权限控制实现流程,本章讲述Web权限管理系统的基本模块-栏目模块。栏目模块涉及到的数据表为目录表。
1.1栏目域
为了更规范和方便后期系统的二次开发和维护,对应特定的业务模块采用Area(域)的方式开发,栏目模块的开发域如下图所示:
由于在Areas下还建立了一个新的目录SystemManage,故需要改变原来的路由。栏目模块的路由文件名称为pageGroupAreaRegistration。改变路由代码的文件名称为如下:
using System.Web.Mvc;
namespace CodeForMvcTest.Areas.pageGroup
{
public class pageGroupAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "SystemManage/pageGroup";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"SystemManage_pageGroup_default",
"SystemManage/pageGroup/{controller}/{action}/{id}", //"{controller}/{action}/{id}"
new { action = "PageGroupManage", id = UrlParameter.Optional }
);
}
}
}
1.2Model
栏目模块的Model可参看第三章项目架构的系统共有类,对应model为Catalog.cs。文件路径为Areas/SystemManage/Models。
1.3视图
栏目模块的视图包含在栏目域中,文件路径为Areas/SystemManage/OperatorManage/Views/PageGroupManage,视图名称为PageGroupMange.cshtml。视图的完整代码如下:
1 @{ 2 ViewBag.Title = "栏目管理"; 3 Layout = "~/Views/Shared/_BaseLayout.cshtml"; 4 } 5 6 <div class="easyui-layout" data-options="fit:true"> 7 8 <div data-options="region:'north',split:true" style="height: 50px;"> 9 @using (Ajax.BeginForm("PageGroupManage", "PageGroupManage", new AjaxOptions 10 { 11 HttpMethod = "POST", 12 OnSuccess = "selectNode", 13 OnBegin = "searchStart", 14 OnFailure = "searchFailure" 15 })) 16 { 17 <!--属性组筛选栏--> 18 <table style="margin-left: 5px; margin-top: 5px;"> 19 <tr> 20 <td><span style="margin-left: 10px;">栏目名称:</span></td> 21 <td> 22 <input id="groupName" name="groupName" /> 23 </td> 24 <td> 25 <input type="submit" value="查找" id="btn_submit" style="margin-left: 10px; margin-right: 10px;" /> 26 </td> 27 </tr> 28 </table> 29 } 30 </div> 31 32 <div data-options="region:'center',split:true" style="padding-bottom: 10px;"> 33 <ul id="catalogTree"> 34 </ul> 35 <br /> 36 <br /> 37 <div id="mm" class="easyui-menu" style="width: 120px;"> 38 <div onclick="openCategoryEditWin()" iconcls="icon-edit">修改节点</div> 39 <div onclick="openCategoryAddWin()" iconcls="icon-add">添加节点</div> 40 <div onclick="deleteCatalog()" iconcls="icon-remove">删除节点</div> 41 </div> 42 </div> 43 </div> 44 45 46 <!--目录编辑窗体--> 47 <div id="categoryWin" title="修改栏目信息" style="width: 480px; height: 380px; padding: 20px; text-align: center;"> 48 <form id="categoryForm" method="POST" action="@Url.Action("UpdateCategory", "PageGroupManage")"> 49 <table style="margin: auto;"> 50 <tr> 51 <td style="text-align: right;"><span>名称:</span></td> 52 <td style="text-align: left;"> 53 <input class="easyui-validatebox" id="e_categoryName" name="CatalogName" data-options="required:true" /> 54 </td> 55 </tr> 56 <tr style="height: 40px;"> 57 <td style="text-align: right;"><span>图标:</span></td> 58 <td style="text-align: left;"> 59 <input class="easyui-validatebox" id="e_categoryPicurl" name="PictureUrl" /> 60 </td> 61 </tr> 62 <tr style="height: 40px;"> 63 <td style="text-align: right;"><span>备注:</span></td> 64 <td> 65 <input class="easyui-validatebox" id="e_categoryRemark" name="Remark" /> 66 </td> 67 </tr> 68 <tr style="height: 40px;"> 69 <td style="text-align: right;"><span>排序值:</span></td> 70 <td style="text-align: left;"> 71 72 <input class="easyui-numberbox" data-options="required:true" id="e_categoryShownum" name="ShowNo" /> 73 </td> 74 </tr> 75 <tr style="height: 40px;"> 76 <td style="text-align: right;"><span>状态:</span></td> 77 <td style="text-align: left;"> 78 <select class="easyui-combobox" name="IsAvailable" id="e_categoryState" style="width: 150px;" 79 data-options="editable:false"> 80 <option value="0">禁用</option> 81 <option value="1">启用</option> 82 </select> 83 </td> 84 </tr> 85 <tr style="height: 50px;"> 86 <td colspan="2" style="text-align: center;"> 87 <input type="hidden" id="parentId" name="ParentId" /> 88 <input type="hidden" id="categoryId" name="CatalogId" /> 89 <input type="reset" style="display: none" /> 90 <input type="submit" value="提交" id="btn_editsubmit" style="margin-left: 10px; margin-right: 10px;" /> 91 <input type="button" value="取消" id="btn_editCancel" onclick="javascript: return $('#categoryWin').window('close');" 92 style="margin-left: 10px; margin-right: 10px;" /> 93 </td> 94 </tr> 95 </table> 96 </form> 97 </div> 98 99 @section scripts 100 { 101 <script type="text/javascript" src="/Areas/SystemManage/SystemJS/pageGroupManage.js"></script> 102 }
1.4JS文件
栏目模块相关的JS文件路径为Areas/SystemManage/SystemJS,JS文件名称为pageGruopManage.js。JS的完整代码如下:
1 //打开目录添加窗体 2 function openCategoryAddWin() { 3 var selectedNode = $("#catalogTree").tree('getSelected'); 4 if (selectedNode != null) { 5 $("#parentId").val(selectedNode.id); 6 } else { 7 $("#parentId").val(0); 8 } 9 10 $("#e_categoryName").val(""); 11 $("#e_categoryRemark").val(""); 12 $("#e_categoryPicurl").val(""); 13 $("#categoryId").val("-1"); 14 $('#categoryWin').window('open'); 15 }; 16 17 //打开目录编辑窗体 18 function openCategoryEditWin() { 19 var selectedNode = $("#catalogTree").tree('getSelected'); //获取选中的节点 20 if (selectedNode != null) { 21 var parentNode = $("#catalogTree").tree('getParent', selectedNode); 22 if (parentNode != null) { 23 $("#parentId").val(parentNode.id); 24 } else { 25 $("#parentId").val(0); 26 } 27 $("#e_categoryName").val(selectedNode.text); 28 $("#e_categoryPicurl").val(selectedNode.attributes.picurl); 29 $("#e_categoryRemark").val(selectedNode.attributes.remark); 30 $("#e_categoryShownum").numberbox("setValue", selectedNode.attributes.showno); 31 $("#e_categoryState").combobox("setValue", selectedNode.attributes.isavailable); 32 $("#categoryId").val(selectedNode.id); 33 } 34 $('#categoryWin').window('open'); 35 }; 36 37 38 function selectNode(data) { 39 if (data.indexOf("/Login/Login") >= 0) { 40 window.parent.location.href = '/Login/Login'; 41 } 42 43 if (data != "1") { 44 var node = $('#catalogTree').tree('find', data); 45 if (node) { 46 $('#catalogTree').tree('select', node.target); 47 } else { 48 alert("未找到相关栏目!"); 49 } 50 } else { 51 alert("查找栏目出错!"); 52 } 53 HideMask(); 54 }; 55 56 57 function searchStart() { 58 LoadMask("正在查找,请稍等... ..."); 59 }; 60 61 62 function searchFailure(data) { 63 if (data.responseText.indexOf("/Login/Login") >= 0) { 64 window.parent.location.href = '/Login/Login'; 65 } 66 HideMask(); 67 alert("操作失败!"); 68 }; 69 70 //删除目录 71 function deleteCatalog() { 72 var node = $('#catalogTree').tree('getSelected'); 73 if (node.id == 0) { 74 alert("不能删除根节点!"); 75 } else { 76 if (window.confirm("删除该目录,将删除该目录下的所有子目录以及包含的页面,确定删除吗?")) { 77 LoadMask("正在删除,请稍等... ..."); 78 79 $.ajax({ 80 url: "/SystemManage/pageGroup/PageGroupManage/DeleteCategory", 81 type: 'POST', 82 data: { categoryId: node.id }, 83 dataType: 'TEXT', 84 success: function (data) { 85 if (data.indexOf("/Login/Login") >= 0) { 86 window.parent.location.href = '/Login/Login'; 87 } 88 InitTree(); 89 alert(data); 90 HideMask(); 91 }, 92 error: function (data) { 93 if (data.responseText.indexOf("/Login/Login") >= 0) { 94 window.parent.location.href = '/Login/Login'; 95 } 96 HideMask(); 97 alert("操作失败!"); 98 } 99 }); 100 } 101 102 } 103 }; 104 105 106 //添加节点 107 function appendNode(newNode) { 108 var parentNode = $('#catalogTree').tree('getSelected'); 109 if (parentNode != null) { 110 $('#catalogTree').tree('append', { 111 parent: parentNode, 112 data: [newNode] 113 }); 114 } 115 }; 116 117 //修改节点信息 118 function updateNode(newNode) { 119 var node = $('#catalogTree').tree('getSelected'); 120 if (node) { 121 node.text = newNode.text; 122 node.attributes.picurl = newNode.attributes.picurl; 123 node.attributes.remark = newNode.attributes.remark; 124 node.attributes.showno = newNode.attributes.showno; 125 node.attributes.isavailable = newNode.attributes.isavailable; 126 $('#catalogTree').tree('update', node); 127 } 128 }; 129 130 131 function InitTree() { 132 $('#catalogTree').tree({ 133 url: '/SystemManage/pageGroup/PageGroupManage/InitCatalogTree', 134 method: 'GET', 135 onContextMenu: function (e, node) { 136 e.preventDefault(); 137 $('#catalogTree').tree('select', node.target); 138 $('#mm').menu('show', { 139 left: e.pageX, 140 top: e.pageY 141 }); 142 } 143 }); 144 }; 145 146 147 $(function () { 148 //初始化栏目树 149 InitTree(); 150 151 //目录编辑窗体初始化 152 $('#categoryWin').window({ 153 modal: true, 154 collapsible: false, 155 minimizable: false, 156 maximizable: false, 157 draggable: true, 158 resizable: false, 159 closable: false, 160 closed: true 161 }); 162 163 164 $("#categoryForm").submit(function (event) { 165 //中断当前的提交事件 166 event.preventDefault(); 167 //数据验证 168 var categoryName = $("#e_categoryName").val(); 169 if (categoryName == "") { 170 alert("请输入名称!"); 171 return false; 172 } 173 var showNum = $("#e_categoryShownum").numberbox("getValue"); 174 if (showNum == "") { 175 alert("请输入排序值!"); 176 return false; 177 } 178 179 $('#categoryWin').window('close'); 180 LoadMask("正在操作,请稍等... ..."); 181 182 //提交的URL,默认为属性组修改或添加路径 183 var url = "/SystemManage/pageGroup/PageGroupManage/UpdateCategory"; 184 var categoryId = $("#categoryId").val(); 185 if (categoryId == "-1") { 186 url = "/SystemManage/pageGroup/PageGroupManage/AddCategory"; 187 } 188 189 //表单序列化数据 190 var formData = $("#categoryForm").serializeJson(); 191 192 $.ajax({ 193 url: url, 194 type: 'POST', 195 data: { categoryJsonStr: JSON.stringify(formData) }, 196 dataType: 'JSON', 197 traditional: true, 198 success: function (data) { 199 if (data.text != "-1") { 200 InitTree(); 201 alert(data.text); 202 } else { 203 alert("后台操作出错!"); 204 } 205 HideMask(); 206 207 }, 208 error: function (data) { 209 if (data.responseText.indexOf("/Login/Login") >= 0) { 210 window.parent.location.href = '/Login/Login'; 211 } 212 HideMask(); 213 alert("操作失败!"); 214 } 215 }); 216 217 }); 218 219 }); 220 221 //显示遮罩层 222 function LoadMask(msg) { 223 var panel = $("body"); 224 if (msg == undefined) { 225 msg = "正在加载,请稍候..."; 226 } 227 $("<div class=\"datagrid-mask\"></div>").css({ display: "block", width: panel.width(), height: panel.height() }).appendTo(panel); 228 $("<div class=\"datagrid-mask-msg\"></div>").html(msg).appendTo(panel).css({ display: "block", left: (panel.width() - $("div.datagrid-mask-msg", panel).outerWidth()) / 2, top: (panel.height() - $("div.datagrid-mask-msg", panel).outerHeight()) / 2 }); 229 }; 230 231 //隐藏遮罩层 232 function HideMask() { 233 var panel = $("body"); 234 panel.find("div.datagrid-mask-msg").remove(); 235 panel.find("div.datagrid-mask").remove(); 236 };
1.5控制器
栏目模块的控制器包含在栏目域中,文件路径为Areas/SystemManage/PageGroup/Controllers,控制器名称为PageGroupManageController.cs。控制器的完整代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using Session; 7 using OdbcDbAccess; 8 using System.Data; 9 using Models; 10 using Controllers; 11 using System.Data.SqlClient; 12 using Newtonsoft.Json; 13 using LogInfo; 14 15 namespace CodeForMvcTest.Areas.pageGroup.Controllers 16 { 17 //|++++++++++++++++++++++++++++++ 18 /// 功能:栏目信息类 19 /// 作者:王令 20 /// 时间:2015-7-20 21 /// 邮箱:1129137758@qq.com 22 //|+++++++++++++++++++++++++++++ 23 24 public class PageGroupManageController : BaseController 25 { 26 public ActionResult PageGroupManage() 27 { 28 return View(); 29 } 30 31 /// <summary> 32 /// 初始化栏目树 33 /// </summary> 34 /// <returns></returns> 35 public ActionResult InitCatalogTree() 36 { 37 IList<TreeModel> resultList = new List<TreeModel>(); 38 TreeModel rootNode=new TreeModel(); 39 try 40 { 41 //获取栏目信息 42 string strSql = "select * from catalog order by catalogid,showno"; 43 DataSet categoryDt = SqlHelper.ExecuteQuery(SqlSeverConnectionName, strSql); 44 if (categoryDt != null && categoryDt.Tables.Count > 0) 45 { 46 DataTable table = categoryDt.Tables[0]; 47 resultList = TreeModel.BuildTreeNodeList(table.Rows, "catalogid", "catalogname", 48 "parentid", true); 49 } 50 } 51 catch (Exception) 52 { 53 } 54 55 return Json(resultList, JsonRequestBehavior.AllowGet); 56 } 57 58 /// <summary> 59 /// 通过栏目名称返回栏目列表 60 /// </summary> 61 /// <param name="groupName">栏目名称</param> 62 /// <returns></returns> 63 [HttpPost] 64 public ActionResult PageGroupManage(string groupName) 65 { 66 try 67 { 68 string sql = "select top 1 catalogid from catalog where catalogname='" + groupName + "'"; 69 int catalogId = SqlHelper.ExecuteScalar<int>(SqlSeverConnectionName, sql); 70 return Content(catalogId.ToString()); 71 } 72 catch (Exception ex) 73 { 74 Log.SaveErrorLog(ex.ToString(), "栏目筛选出错"); 75 return Content("-1"); ; 76 } 77 78 } 79 80 /// <summary> 81 /// 修改一级目录 82 /// </summary> 83 /// <param name="categoryJsonStr">ReportClassify对应json字符串</param> 84 /// <returns></returns> 85 [HttpPost] 86 public ActionResult UpdateCategory(string categoryJsonStr) 87 { 88 var category = JsonConvert.DeserializeObject<Catalog>(categoryJsonStr); 89 string sql = "update catalog set catalogname='" + category.CatalogName + "',picurl='" + category.PictureUrl + "',remark='" + category.Remark + "',isavailable='" + category.IsAvailable + "',showno='" + category.ShowNo + "' where catalogid='" + category.CatalogId + "'"; 90 try 91 { 92 SqlHelper.ExecuteNonQuery(SqlSeverConnectionName , sql); 93 var node = new TreeModel 94 { 95 id = category.CatalogId, 96 text = category.CatalogName, 97 state = "open", 98 attributes = new CatalogAttributes 99 { 100 isavailable = category.IsAvailable, 101 remark = category.Remark, 102 showno = category.ShowNo 103 } 104 }; 105 Log.SaveOperatorLog(sql, 1, "修改目录信息"); 106 object result = new 107 { 108 text = "修改成功!", 109 node = node 110 }; 111 return Json(result); 112 } 113 catch (Exception ex) 114 { 115 Log.SaveErrorLog(ex.ToString(), "修改目录出错"); 116 object result = new 117 { 118 text = "-1" 119 }; 120 return Json(result); 121 } 122 } 123 124 125 126 /// <summary> 127 /// 添加目录 128 /// </summary> 129 /// <param name="categoryJsonStr">ReportClassify对应json字符串</param> 130 /// <returns></returns> 131 [HttpPost] 132 public ActionResult AddCategory(string categoryJsonStr) 133 { 134 var category = JsonConvert.DeserializeObject<Catalog>(categoryJsonStr); 135 string sql = "insert into catalog (parentid,catalogname,picurl,isavailable,showno,remark)" 136 + " values ('" + category.ParentId + "','" + category.CatalogName + "','" + category.PictureUrl + "','" + category.IsAvailable + "','" + category.ShowNo + "','" + category.Remark + "')"; 137 try 138 { 139 SqlHelper.ExecuteNonQuery(SqlSeverConnectionName, sql); 140 var node = new TreeModel 141 { 142 id = category.CatalogId, 143 text = category.CatalogName, 144 state = "open", 145 attributes = new CatalogAttributes 146 { 147 isavailable = category.IsAvailable, 148 remark = category.Remark, 149 showno = category.ShowNo 150 } 151 }; 152 153 Log.SaveOperatorLog(sql, 1, "添加目录信息"); 154 155 object result = new 156 { 157 text = "添加成功!", 158 node = node 159 }; 160 161 return Json(result); 162 } 163 catch (Exception ex) 164 { 165 Log.SaveErrorLog(ex.ToString(), "添加目录出错"); 166 object result = new 167 { 168 text = "-1" 169 }; 170 171 return Json(result); 172 } 173 } 174 175 176 /// <summary> 177 /// 删除目录 178 /// </summary> 179 /// <param name="categoryId">目录ID</param> 180 /// <returns></returns> 181 [HttpPost] 182 public ActionResult DeleteCategory(int categoryId) 183 { 184 try 185 { // 删除需要执行事务操作 186 string sql = "delete from pageinfo where catalogid in (select catalogid from catalog where catalogid={0} or parentid={0})"; 187 sql += ";delete from rightlist where categoryid in (select catalogid from catalog where catalogid={0} or parentid={0})"; 188 sql += ";delete from catalog where catalogid={0} or parentid={0}"; 189 sql = string.Format(sql, categoryId); 190 int result= SqlHelper.ExecuteTran(SqlSeverConnectionName,sql); 191 if (result > 0) 192 return Content("删除成功!"); 193 else 194 { 195 return Content("删除失败!"); 196 } 197 } 198 catch (Exception ex) 199 { 200 Log.SaveErrorLog(ex.ToString(), "删除目录出错!"); 201 return Content("未能成功删除目录!"); 202 } 203 204 } 205 206 207 } 208 }
1.6界面运行截图
栏目管理主界面如下图所示:
编辑栏目界面如下图所示:
添加栏目界面如下图所示:
喜欢请赞赏一下啦^_^
微信赞赏
支付宝赞赏