OA项目第五天

T4模板生成相关表(ActionInfo,RoleInfo,RoleInfoActionInfo等)的实体类,数据接口类,业务类。权限控制器

OA项目第五天

T4模板

要是两个表有关联(无论之间隔了多少表,只要有关联),那么生成的modle类是会生成对应的关联的modle类对象(简单来说就是类的对象可以点出相应的表)。这就是导航属性,顺便复习一下

关于权限的流程

最简单的我们就是权限-用户,需要创建3张表(用户-权限用户-权限)。那么在这更上一层的是用户,角色,权限需要创建这几张表(用户-用户角色-角色-角色权限-权限)。此外我们这里按要求还多出了一个部门表来方便我们

2nRYLN.png

由于这么多的表,但是干的都是重复的代码,因此我们还是使用T4模板来进行生成

RoleInfoController

无论哪个表都是做的增删改查,这个当热也不例外,当热这代码跟之前的没有任何差别,因此这里我们就只做一个增加的就算了。这里额外需要给角色用户分配权限这个功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
 <script type="text/javascript">
$(function () {
loadData();
});
function loadData(pars) {
$('#tt').datagrid({
url: '/RoleInfo/GetRoleInfoList',
title: '角色数据表格',
width: 700,
height: 400,
fitColumns: true, //列自适应
nowrap: false,
idField: 'ID',//主键列的列明
loadMsg: '正在加载用户的信息...',
pagination: true,//是否有分页
singleSelect: false,//是否单行选择
pageSize: 5,//页大小,一页多少条数据
pageNumber: 1,//当前页,默认的
pageList: [5, 10, 15],//可以让你选择煤业显示多少?
queryParams: pars,//往后台传递参数
columns: [[//c.UserName, c.UserPass, c.Email, c.RegTime
{ field: 'ck', checkbox: true, align: 'left', width: 50 },//checkbox: true可选复选框
{ field: 'ID', title: '编号', width: 80 },// ID返回JSon数据ID属性,这个不能随便改,包括下面
{ field: 'RoleName', title: '姓名', width: 120 },
{
field: 'ModifiedOn', title: '上次修改时间', width: 120, align: 'right',
formatter: function (value, row, index) {
return (eval(value.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"))).pattern("yyyy-M-d");
}
},
{ field: 'Remark', title: '备注', width: 120 },
{
field: 'SubTime', title: '创建时间', width: 80, align: 'right',
formatter: function (value, row, index) {
return (eval(value.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"))).pattern("yyyy-M-d");
}
}
]],
toolbar: [{
id: 'btnDelete',
text: '删除',
iconCls: 'icon-remove',
handler: function () {
deleteInfo();
}
}, {
id: 'btnAdd',
text: '添加',
iconCls: 'icon-add',
handler: function () {
addInfo();
}
}, {
id: 'btnEidt',
text: '编辑',
iconCls: 'icon-edit',
handler: function () {

showEditInfo();
}
}, {
id: 'btnSetRoleAction',
text: '为角色分配权限',
iconCls: 'icon-edit',
handler: function () {

showRoleAction();
}
}]
});
}
//为角色分配权限
function showRoleAction() {
//判断是否选择了一个角色
var rows = $("#tt").datagrid("getSelections");//获取所选行
if (rows.length != 1) {
$.messager.alert("提示", "请选择要分配权限的角色", "error");
return;
}
//指定的iframe的src
$('#setActionFrame').attr("src", "/RoleInfo/ShowRoleAction/?id=" + rows[0].ID)
$("#setActionDiv").css("display", "block");
$('#setActionDiv').dialog({
title: '为角色分配权限数据',
width: 300,
height: 400,
collapsible: true,
maximizable: true,
resizable: true,
modal: true,
buttons: [{
text: 'Ok',
iconCls: 'icon-ok',
handler: function () {
//提交表单
//调用子窗口的方法.
var childWindow = $("#setActionFrame")[0].contentWindow;//表示获取了嵌入在iframe中的子窗体的window对象。
childWindow.subForm();//调用子窗体中的方法,完成表单的提交。
}
}, {
text: 'Cancel',
handler: function () {
$('#setActionDiv').dialog('close');
}
}]
});
}
//添加
function addInfo() {
$("#addFrame").attr("src", "/RoleInfo/ShowAddRoleInfo");
$("#addDiv").css("display", "block");
$('#addDiv').dialog({
title: '添加角色数据',
width: 300,
height: 400,
collapsible: true,
maximizable: true,
resizable: true,
modal: true,
buttons: [{
text: 'Ok',
iconCls: 'icon-ok',
handler: function () {
//提交表单
//调用子窗口的方法.
//var childWindow = $("#addFrame")[0].contentWindow;//表示获取了嵌入在iframe中的子窗体的window对象。
//childWindow.subForm();//调用子窗体中的方法,完成表单的提交。
var childWindow = $("#addFrame")[0].contentWindow;
childWindow.subForm();

}
}, {
text: 'Cancel',
handler: function () {
$("#addRoleForm textbox").val('');
$('#addDiv').dialog('close');
}
}]
});
}
//添加完成后调用该方法。
function afterSet(data) {
if (data == "ok") {
$('#setActionDiv').dialog('close');
$('#tt').datagrid('reload');//加载表格不会跳到第一页。
}
}
</script>
</head>
<body>
<div>
用户名:<input type="text" id="txtSearchName" />&nbsp;&nbsp;备注<input type="text" id="txtSearchRemark" /><a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" style="width:80px" id="btnSearch">Search</a>
<table id="tt" style="width: 700px;" title="标题,可以使用代码进行初始化,也可以使用这种属性的方式" iconcls="icon-edit"></table>
</div>
<!------------添加表单------------------>
<div id="addDiv">
<iframe id="addFrame" width="100%" height="100%" frameborder="0"></iframe>
</div>
<!------------添加表单------------------>
<div id="setActionDiv">
<iframe id="setActionFrame" width="100%" height="100%" frameborder="0"></iframe>
</div>
</body>

后端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
public class RoleInfoController : Controller
{
private IRoleInfoService bll { get; set; }
private IActionInfoService abll { get;set; }

// GET: RoleInfo,展示角色页面
public ActionResult Index()
{
return View();
}
//展示添加表单
public ActionResult ShowAddRoleInfo()
{
return View();
}

#region 获取角色列表

public ActionResult GetRoleInfoList()
{
string t = Request["page"];
string a = Request["rows"];
//page rows。分页数据
int pageIndex = Request["page"] != null ? int.Parse(Request["page"]) : 1;
int pageSize = Request["rows"] != null ? int.Parse(Request["rows"]) : 5;
int totalCount;
var roleList= bll.LoadPageEntities(pageIndex, pageSize,out totalCount,r=>r.DelFlag==1,r=>r.ID,true);
var temp = from r in roleList
select new { ID=r.ID,RoleName = r.RoleName,SubTime = r.SubTime, ModifiedOn = r.ModifiedOn, Remark = r.Remark};
return Json(new {rows= temp, total = totalCount});

}

#endregion

#region 完成角色信息添加
public ActionResult AddRoleInfo(RoleInfo roleInfo)
{
roleInfo.ModifiedOn = DateTime.Now;
roleInfo.SubTime = DateTime.Now;
roleInfo.DelFlag = 1;
bll.AddEntity(roleInfo);
return Content("ok");
}
#endregion

#region 展示要分配的权限

public ActionResult ShowRoleAction()
{
int id = int.Parse(Request["id"]);
var roleInfo = bll.LoadEntities(r => r.ID == id).FirstOrDefault();
ViewBag.RoleInfo = roleInfo;
//获取所有的权限
var actionList = abll.LoadEntities(a => a.DelFlag == 0).ToList();
//要分配权限的角色以前有哪些权限
var actionIdList = (from a in roleInfo.ActionInfo
select a.ID).ToList();
ViewBag.ActionInfoList = actionList;
ViewBag.ActionIdList = actionIdList;
return View();

}

#endregion

#region 角色的分配

public ActionResult SetRoleAction()
{
int roleId = int.Parse(Request["roleId"]);
string[] allkeys = Request.Form.AllKeys;//获取所有表单元素name的值
List<int> list = new List<int>();
foreach (string s in allkeys)
{
if (s.StartsWith("cba_"))
{
string k = s.Replace("cba_", "");
list.Add(Convert.ToInt32(k));
}
}
if (bll.SetRoleActionInfo(roleId,list))
{
return Content("ok");
}
else
{
return Content("no");
}
}
#endregion
}

ActionInfoController

注意这里我们用了Jquary.ajax,js这个插件的方法,因此一开始我们记得导进来,我们封装在MyAjaxForm中,ajaxSubmit方法进行提交

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<script src="~/Scripts/MyAjaxForm.js"></script>  
<script type="text/javascript">
$(function () {
$("#addDiv").css("display", "none");
loadData();
//为权限类别下拉框绑定change事件
$("#actionTypeEnumChange").change(function () {
if ($(this).val() == "1") {
$("#showTr").fadeIn();
} else {
$("#showTr").fadeOut();
}
});
//为上传按钮绑定单击事件。
$("#btnFileUp").click(function () {
$("#addForm").ajaxSubmit({
success: function (str) {
var data = str.split(':');
if (data[0] == "ok") {
var img = "<img src='" + data[1] + "' width='30' height='30'/>";

$("#showImage").append(img);
$("#hiddenMenuIcon").val(data[1]);//将上传成功的图片路径存储到隐藏域中。
} else {
alert(data[1]);
}
},
url: '/ActionInfo/GetFileUp',
type: "post",
dataType: "text"
});
});

});
function loadData(pars) {
$('#tt').datagrid({
url: '/ActionInfo/GetActionInfoList',
title: '权限数据表格',
width: 700,
height: 400,
fitColumns: true, //列自适应
nowrap: false,
idField: 'ID',//主键列的列明
loadMsg: '正在加载权限的信息...',
pagination: true,//是否有分页
singleSelect: false,//是否单行选择
pageSize: 5,//页大小,一页多少条数据
pageNumber: 1,//当前页,默认的
pageList: [5, 10, 15],
queryParams: pars,//往后台传递参数
columns: [[//c.UserName, c.UserPass, c.Email, c.RegTime
{ field: 'ck', checkbox: true, align: 'left', width: 50 },
{ field: 'ID', title: '编号', width: 80 },
{ field: 'ActionInfoName', title: '权限名称', width: 120 },
{ field: 'Sort', title: '排序', width: 120 },
{ field: 'Remark', title: '备注', width: 120 },
{ field: 'Url', title: 'Url', width: 120 },
{ field: 'HttpMethod', title: '请求方式', width: 120 },
{
field: 'ActionTypeEnum', title: '权限类别', width: 120,
formatter: function (value, row, index) {
if (value == "1") {
return "菜单权限";
} else {
return "普通权限";
}

}

},

{
field: 'SubTime', title: '操作', width: 80, align: 'right',
formatter: function (value, row, index) {
return (eval(value.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"))).pattern("yyyy-M-d");
}
},
{
field: 'Operator', title: '时间', width: 80, align: 'right',
formatter: function (value, row, index) {
var str = "<a href='javascript:void(0)' class='deletes' nId='"+row.ID+"'>删除</a>";
return str;
}

}
]],
toolbar: [{
id: 'btnDelete',
text: '删除',
iconCls: 'icon-remove',
handler: function () {

deleteInfo();
}
}, {
id: 'btnAdd',
text: '添加',
iconCls: 'icon-add',
handler: function () {

addInfo();
}
}, {
id: 'btnEidt',
text: '编辑',
iconCls: 'icon-edit',
handler: function () {

showEditInfo();
}
}],
onLoadSuccess: function () {
$(".deletes").click(function () {
alert($(this).attr("nId"));
});
}

});
}

function addInfo() {
$("#addDiv").css("display", "block");
$('#addDiv').dialog({
title: '添加权限数据',
width: 300,
height: 400,
collapsible: true,
maximizable: true,
resizable: true,
modal: true,
buttons: [{
text: 'Ok',
iconCls: 'icon-ok',
handler: function () {
//提交表单
//调用子窗口的方法.
//var childWindow = $("#addFrame")[0].contentWindow;//表示获取了嵌入在iframe中的子窗体的window对象。
//childWindow.subForm();//调用子窗体中的方法,完成表单的提交。
$("#addForm").submit();
}
}, {
text: 'Cancel',
handler: function () {
$('#addDiv').dialog('close');
}
}]
});
}
//添加完成后调用该方法。
function afterAdd(data) {
if (data == "ok") {
$('#addDiv').dialog('close');
}
}
</script>
</head>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class ActionInfoController : Controller
{
//
// GET: /ActionInfo/
IBLL.IActionInfoService ActionInfoService { get; set; }
public ActionResult Index()
{
return View();
}
#region 获取权限信息
public ActionResult GetActionInfoList()
{

int pageIndex = Request["page"] != null ? int.Parse(Request["page"]) : 1;
int pageSize = Request["rows"] != null ? int.Parse(Request["rows"]) : 5;
int totalCount;
short delFlag = (short)DeleteEnumType.Normarl;
var actionInfoList = ActionInfoService.LoadPageEntities<int>(pageIndex, pageSize, out totalCount, r => r.DelFlag == delFlag, r => r.ID, true);
var temp = from r in actionInfoList
select new { ID = r.ID, ActionInfoName = r.ActionInfoName, Sort = r.Sort, SubTime = r.SubTime, Remark = r.Remark, Url = r.Url, ActionTypeEnum = r.ActionTypeEnum, HttpMethod = r.HttpMethod };
return Json(new { rows = temp, total = totalCount }, JsonRequestBehavior.AllowGet);
}
#endregion

#region 获取上传的文件.
public ActionResult GetFileUp()
{
HttpPostedFileBase file=Request.Files["fileUp"];
string fileName = Path.GetFileName(file.FileName);
string fileExt = Path.GetExtension(fileName);
if (fileExt == ".jpg")
{
string dir = "/ImageIcon/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
Directory.CreateDirectory(Path.GetDirectoryName(Request.MapPath(dir)));
string newfileName = Guid.NewGuid().ToString();
string fullDir = dir + newfileName + fileExt;
file.SaveAs(Request.MapPath(fullDir));
//自己加上图片的缩略图
return Content("ok:" + fullDir);
}
else
{
return Content("no:文件类型错误!!");
}
}

#endregion

#region 完成权限添加
public ActionResult AddActionInfo(ActionInfo actionInfo)
{
actionInfo.DelFlag = 0;
actionInfo.ModifiedOn = DateTime.Now.ToString();
actionInfo.SubTime = DateTime.Now;
actionInfo.Url = actionInfo.Url.ToLower();
ActionInfoService.AddEntity(actionInfo);
return Content("ok");
}
#endregion

}

要是mecached报错,那就检查下modle类,一些不需要序列化的属性前面必须加上 [JsonIgnore]标签


OA项目第五天
http://www.kju147.com/2018/04/15/OA项目第五天/
作者
个人学习
发布于
2018年4月15日
许可协议