最近想研究权限管理,看群里有人发了ZTrees模板,我看了下,觉得笔easyUI操作起来更灵活些,于是就开始研究了。
刚开始从网上找了找了个Demo,当然这个并没有实现权限啥的,但实现了前台调用Ajax给后台传递数据,这就有思路了,那个我单独整理了一片博文,可以在看这篇博文前先看看http://www.cnblogs.com/shuai7boy/p/5645888.html
下面就是添加数据库交互和权限管理这块了。
我是这么设计的:首先设计权限就设计到了用户登陆,所以有一个登陆页面,然后用户登陆后根据不同的用户展示不同的后台信息。
然后问题又来了,在我运行项目时,首先打开的就是后台界面,这就不对了,所以还得控制它不登录就不能访问后台页面。
这就是大概思路:下面先放张图片说说细节~
从上面可以看到一共设计了三张表,一个存放登陆数据,一个存放信息展示数据,一个中间表控制权限,将登陆数据和展示数据表链接起来。
当用户登陆成功后,根据登陆用户名获取主键id,然后根据id获取具有的权限,也就是获取中间表对应的数据,然后取得一个List集合,然后要传递参数,根据参数筛选要展示哪些数据。注意传参时,不能直接传递List集合,所以得弄成想办法弄成字符串类型,然后传递过去后再弄成数组类型。
---------以上是设计思路,下面是代码分析-----------
这个项目我用的是MVC框架做的,一共写了三个控制器,分别是BaseController(基类控制)、HomeController(信息展示控制)、LoginController(登陆控制)。
BaseController代码展示:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Mvc;namespacezTreeDemo.Controllers
{public classBaseController : Controller
{//
//GET: /Base/
protected override voidOnActionExecuting(ActionExecutingContext filterContext)
{if (filterContext.HttpContext.Session["LoginUser"]==null)
{
filterContext.HttpContext.Response.Redirect("Login/Index");
}
}
}
}
View Code
LoginController代码展示:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Mvc;usingwww.zTree.BLL;namespacezTreeDemo.Controllers
{public classLoginController : Controller
{//
//GET: /Login/
publicActionResult Index()
{returnView();
}publicActionResult LoginCheck()
{string strName = Request["loginName"];string strPwd = Request["loginPwd"];
TreeBLL bll= newTreeBLL();bool b=bll.LoginCheck(strName, strPwd);if(b)
{
Session["LoginUser"] =strName;//根据登录名查询主键id 然后根据主键id获得对应哪些树 然后将参数传递过去 登陆跳转
int id=bll.GetIdByLoginName(strName);
List listN =bll.GetLogForTable(id);//Response.Redirect(Url.Action("GetData","Home"));//Console.Write(listN);
string str = null;for (int i = 0; i < listN.Count; i++)
{
str+= listN[i].ToString()+",";
}int n=str.LastIndexOf(',');
str=str.Substring(0,n);returnContent(str);
}return Content("您输入的用户名或者密码错误!");
}
}
}
View Code
LoginController-Index.cshtml代码展示:
@{
Layout= null;
}
Index$('form').submit(function () {var rec = $(this).serialize();
$.get("/Login/LoginCheck", rec, function (_data) {if (_data == "您输入的用户名或者密码错误!") {
alert(_data);return;
}
document.location.href= "/Home/Index?data=" +_data;
})return false;
})
});
登录名: | |
密码: | |
View Code
HomeController代码展示:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Mvc;usingwww.zTree.BLL;usingwww.zTree.Model;namespacezTreeDemo.Controllers
{public classHomeController : BaseController
{//
//GET: /Home/
publicActionResult Index()
{if (Request["data"]!=null)
{
System.Web.Caching.Cache cache=HttpRuntime.Cache;//Session["data"] = Request["data"];
cache.Insert("data", Request["data"]);
}returnView();
}publicActionResult Edit()
{var list =GetData();returnJson(list, JsonRequestBehavior.AllowGet);
}public ListGetData()
{//获取发送过来的数据
System.Web.Caching.Cache cache =HttpRuntime.Cache;//将数据分割
string recStr = cache.Get("data").ToString();string[] recN=recStr.Split(',');
List tree = new List();//从数据库获取数据拼接返回前台
TreeBLL bll = newTreeBLL();//根据登陆用户判读是哪个用户 然后根据用户决定传递哪个id过去//循环遍历字符串 转换为int类型 然后开始查询
for (int i = 0; i < recN.Length; i++)
{int n = int.Parse(recN[i]);
List rec =bll.GetTreeList(n);for (int j = 0; j < rec.Count; j++)
{
tree.Add(new Tree { id = rec[j].id, pId = rec[j].pId, name = rec[j].name, icon =rec[j].icon });
}
}//tree.Add(new Tree { id = 1, pId = 0, name = "蔬菜", icon = "../Script/css/zTreeStyle/img/diy/1_open.png" });//tree.Add(new Tree { id = 2, pId = 0, name = "动物", icon = "../Script/css/zTreeStyle/img/diy/1_open.png" });//tree.Add(new Tree { id = 3, pId = 0, name = "人类", icon = "../Script/css/zTreeStyle/img/diy/1_open.png" });//tree.Add(new Tree { id = 4, pId = 1, name = "茄子", icon = "../Script/css/zTreeStyle/img/diy/1_open.png" });//tree.Add(new Tree { id = 5, pId = 2, name = "熊猫", icon = "../Script/css/zTreeStyle/img/diy/1_open.png" });//tree.Add(new Tree { id = 6, pId = 3, name = "胡歌", icon = "../Script/css/zTreeStyle/img/diy/1_open.png" });
returntree;
}//public class Tree//{//public int id { get; set; }//public int pId { get; set; }//public string name { get; set; }//public string icon { get; set; }//}
}
}
View Code
HomeController-Index.cshtml代码展示:
@{
Layout= null;
}
zTreeTestvartree;
$.ajax({
type:"Get",
url:"@Url.Action("Edit","Home")",//async: false,
success: function (data) {
tree=data;
$.fn.zTree.init($("#treeDemo"), setting, tree);
}
});var setting ={
data: {
simpleData: {
enable:true}
}
};
自定义图标--icon属性
[文件路径:core/custom_icon.html]
1、setting 配置信息说明
- 自定义图标不需要对 setting 进行特殊配置
2、treeNode 节点数据说明
- 利用 节点数据的 icon / iconOpen / iconClose 属性实现自定义图标
- 详细请参见 API 文档中的相关内容
3、其他说明
- 由于时间关系,例子直接采用 png 图片,如果需要解决 ie6 下 png 图片的透明问题,请针对 ie6 制作特殊的 gif 图片或者利用 css filter 解决
View Code
学习心得:当控制器中遇到使用全局变量时,这时因为每次请求都会重新执行,那么上次全局变量赋的值,下次就可能请求不到了,及请求的是空值,那么这时可以考虑用Cookie(建议用Cookie,因为这样不会访问数据库)或Session实现保存获取数据。