mysql 实现ztree_利用ZTree链接数据库实现 [权限管理]

本文介绍了如何结合ZTree和MySQL数据库实现一个权限管理系统。通过设计三张表进行数据存储,包括用户登录信息、信息展示数据和权限关联表。用户登录后,根据其权限查询数据库并转换数据为字符串传递给前端,实现不同用户展示不同后台信息。文章详细展示了MVC框架下的控制器代码,包括BaseController、LoginController和HomeController,以及如何处理登录检查、数据获取和权限判断。
摘要由CSDN通过智能技术生成

最近想研究权限管理,看群里有人发了ZTrees模板,我看了下,觉得笔easyUI操作起来更灵活些,于是就开始研究了。

刚开始从网上找了找了个Demo,当然这个并没有实现权限啥的,但实现了前台调用Ajax给后台传递数据,这就有思路了,那个我单独整理了一片博文,可以在看这篇博文前先看看http://www.cnblogs.com/shuai7boy/p/5645888.html

下面就是添加数据库交互和权限管理这块了。

我是这么设计的:首先设计权限就设计到了用户登陆,所以有一个登陆页面,然后用户登陆后根据不同的用户展示不同的后台信息。

然后问题又来了,在我运行项目时,首先打开的就是后台界面,这就不对了,所以还得控制它不登录就不能访问后台页面。

这就是大概思路:下面先放张图片说说细节~

9f0cd4bfe378f446005575f046b3d541.png

从上面可以看到一共设计了三张表,一个存放登陆数据,一个存放信息展示数据,一个中间表控制权限,将登陆数据和展示数据表链接起来。

当用户登陆成功后,根据登陆用户名获取主键id,然后根据id获取具有的权限,也就是获取中间表对应的数据,然后取得一个List集合,然后要传递参数,根据参数筛选要展示哪些数据。注意传参时,不能直接传递List集合,所以得弄成想办法弄成字符串类型,然后传递过去后再弄成数组类型。

---------以上是设计思路,下面是代码分析-----------

这个项目我用的是MVC框架做的,一共写了三个控制器,分别是BaseController(基类控制)、HomeController(信息展示控制)、LoginController(登陆控制)。

BaseController代码展示:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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代码展示:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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代码展示:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

@{

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代码展示:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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代码展示:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

@{

Layout= null;

}

zTreeTest

vartree;

$.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实现保存获取数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值