王佳豪
2021-06-25 eb3f8587aa7466fd6d2d3ca712d0f9baf4e27dcb
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/**
 * 
 */
 
$(function(){
    formValidator();
    $("#test-table").bootstrapTable('destroy');
    $('#test-table').bootstrapTable({
        method : 'GET', //默认是post,不允许对静态文件访问
        url: "role/rolePages.action",
        cache : false,
        striped : true,// 隔行加亮
        pagination : true, //开启分页功能    在表格底部显示分页工具栏
        pageSize : 5, //默认每页条数
        pageNumber : 1, //默认分页
        pageList : [ 10, 20, 50, 100, 200, 500 ],//分页数
        showColumns : true, //显示隐藏列
        showRefresh : false, //显示刷新按钮
        toolbar:"#toolbar",
        singleselect : true,
         clickToSelect: true, // 单击行即可以选中
        search : false,//显示搜素表单
        silent : true, //刷新事件必须设置
        sidePagination : "server", //表示服务端请求  
        columns : [ {
            field : "role_id",
            title : "角色编号",
            class : 'col-md-1',
            align : "center",
            valign : "middle",
            sortable : "true"
        }, {
            field : "role_name",
            title : "角色名称",
            align : "center",
            valign : "middle",
            sortable : "true"
        }, {
            field : "role_desc",
            title : "角色描述",
            align : "center",
            valign : "middle",
            sortable : "true"
        },{
            field : "pid",
            title : "上级角色ID",
            align : "center",
            valign : "middle",
            sortable : "true"
        }, {
            field: 'operate',
            title: '操作',
           class : 'col-md-3',
            align: 'center',
            valign: 'middle',
           formatter: operateFormatter,
        }],
        queryParamsType: "undefined",
        queryParams: function queryParams(params) {   //设置查询参数
            var param = {
                pageNumber: params.pageNumber,
                pageSize: params.pageSize,
                // searchText: params.searchText
            };
            return param;
        },
        formatLoadingMessage : function() {
            return "请稍等,正在加载中...";
        },
 
        formatNoMatches : function() {
            return '无符合条件的记录';
        }
    });
}); 
 
function operateFormatter(value, row, index) {
    return ['<button type="button" style="display:'+authorityEnable+'" class=" btn btn-warning" onclick="getAuth('+row.role_id+')">权限</button>&nbsp;&nbsp;&nbsp;',
        '<button type="button" style="display:'+updateEnable+'" class=" btn btn-info" onclick="getvalue('+row.role_id+')">修改</button>',
        '&nbsp;&nbsp;&nbsp;<button style="display:'+deleteEnable+'" class=" btn btn-danger" type="button" onclick="delRole('+row.role_id+')">删除</button>'
        ].join('');
}
 
//保存权限
function saveAuth(){
    var id=$("#rid").val();
    var ids=[];
    var auths=[];
    var obj=$('#tree').treeview('getChecked');
    $.each(obj,function(index,items){
        if(parseInt(items.id)>10000){
            auths.push(items.id);
        }else{
            ids.push(items.id);
        }
    });
    console.log(ids);
    auths.push(10000);
    if(auths==null){
        console.log(auths);
    }
    $.ajax({
        url:'role/saveRoleMenu.action',
        dataType:'json',
        type:'post',
        traditional:true,
        data:{
            rid:id,
            mid:ids,
            auths:auths
            },
        success:function(data){
            if(data){
                alert("保存成功!");
            }else{
                alert("保存失败!");
            }
            $("#test-table").bootstrapTable("refresh");
            closeDlg();
        },
        error:function(){
            alert("请求失败!");
        }
    });
    
}
 
//权限
function getAuth(id){
    $("#rid").val(id);
    $.ajax({
        url:'role/viewTree.action',
        dataType:'json',
        type:'post',
        data:{rid:id},
        success:function(data){
             $('#tree').treeview({
                 data: data,         // 数据源
                 showCheckbox: true,   //是否显示复选框
                 highlightSelected: false,    //是否高亮选中
                 //nodeIcon: 'glyphicon glyphicon-user',    //节点上的图标
                    //expandIcon: 'glyphicon glyphicon-chevron-right',//展开图标 
                   //  collapseIcon: 'glyphicon glyphicon-chevron-down',//合并图标 
                   //  nodeIcon: 'glyphicon glyphicon-bookmark',//无节点图标 
                  backColor: "purple",//背景色 
                     onhoverColor: "#F5F5DC",//鼠标悬浮颜色 
                     borderColor: "red",//边框颜色 
                     highlightSelected: true,//高亮选中 
                     selectedColor: "red",//选中颜色 
                     selectedBackColor: "#D3D3D3",//选中背景色 
                  color: "#00BFFF", 
                   selectable: false,
                 multiSelect: false,    //多选
                  state: {
                         checked: true,
                         disabled: true,
                         expanded: true,
                         selected: true
                       },
                 onNodeChecked: function (event,node) {
                    // 父级节点被选中,那么子级节点都要选中
                      if (node.nodes != null) {
                       $.each(node.nodes, function(index, value) {
                         $('#tree').treeview('checkNode', value.nodeId, {
                         silent : true
                        });
                       });
                      } else {
 
                           // 子级节点选中的时候,要根据情况判断父节点是否要全部选中
                           // 父节点
                           var parentNode =  $('#tree').treeview('getParent', node.nodeId);
                           var isAllchecked =  $('#tree'); // 是否全部选中
                           // 当前子级节点的所有兄弟节点,也就是获取父下面的所有子
                           var siblings =  $('#tree').treeview('getSiblings', node.nodeId);
                           for ( var i in siblings) {
                            // 有一个没选中,则不是全选
                            if (!siblings[i].state.checked) {
                             isAllchecked = false;
                             break;
                            }
                           }
                           // 全选,则打钩
                           if (isAllchecked) {
                             $('#tree').treeview('checkNode', parentNode.nodeId, {
                             silent : true
                            });
                           } else {// 非全选,则变红
                             $('#tree').treeview('checkNode', node.nodeId, {
                             silent : true
                            });
                           }
                          
                     }
                 },
                 onNodeUnchecked : function(event, node) {
                     silentByChild = true;
                      // 选中的是父级节点
                      if (node.nodes != null) {
                       // 这里需要控制,判断是否是因为子级节点引起的父节点被取消选中
                       // 如果是,则只管取消父节点就行了
                       // 如果不是,则子节点需要被取消选中
                    // var silentByChild=false;
                       if (silentByChild) {
                        $.each(node.nodes, function(index, value) {
                         $('#tree').treeview('uncheckNode', value.nodeId, {
                          silent : true
                         });
                        });
                       }
                      } else {/*
                       // 子级节点被取消选中
                       var parentNode = $('#tree').treeview('getParent', node.nodeId);
                       var isAllUnchecked = true; // 是否全部取消选中
                       // 子级节点有一个选中,那么就不是全部取消选中
                       var siblings = $('#tree').treeview('getSiblings', node.nodeId);
                       for ( var i in siblings) {
                        if (siblings[i].state.checked) {
                         isAllUnchecked = false;
                         break;
                        }
                       }
                       // 全部取消选中,那么省级节点恢复到默认状态
                       if (isAllUnchecked) {
                        $('#tree').treeview('unselectNode', parentNode.nodeId, {
                         silent : true,
                        });
                        $('#tree').treeview('uncheckNode', parentNode.nodeId, {
                         silent : true,
                        });
                       } else {
                        silentByChild = false;
                        $('#tree').treeview('selectNode', parentNode.nodeId, {
                         silent : true,
                        });
                        $('#tree').treeview('uncheckNode', parentNode.nodeId, {
                         silent : true,
                        });
                       }
                      */}
                      
                     },
                 onNodeSelected: function (event, data) {
                    
                 }
             });
 
        },
        error:function(){
            alert("请求失败!");
        }
    });
    $("#authDlg").modal('show');
}
 
//修改,打开模态框
function getvalue(id){
    $.ajax({
        url:'role/queryRole.action',
        dataType:'json',
        type:'post',
        data:{rid:id},
        success:function(data){
            $("#role_id1").val(data.role.role_id);
            $("#role_name1").val(data.role.role_name);
            $("#role_desc1").val(data.role.role_desc);
            $("#pid1").empty();
            $("#pid1").append("<option value='0'>请选择上级角色</option>");
            $.each(data.rlist,function(index,items){
                if(data.role.pid==items.role_id){
                    $("#pid1").append("<option selected value='"+items.role_id+"'>"+items.role_name+"</option>");
                }else{
                    $("#pid1").append("<option value='"+items.role_id+"'>"+items.role_name+"</option>");
                }
            });
        },
        error:function(){
            alert("请求失败!");
        }
    });
    $("#mydlg").modal('show');
}
 
//修改
function upRole(){
    if($("#infos").text()=="名称可用"||$("#infos").text()==""){
        if($("#myform").data('bootstrapValidator').validate().isValid()){
            $.ajax({
                url:'role/upRole.action',
                dataType:'json',
                type:'post',
                data:$("#myform").serialize(),
                success:function(data){
                    if(data>0){
                        alert("修改成功!");
                    }else{
                        alert("修改失败!");
                    }
                    $("#test-table").bootstrapTable("refresh");
                    closeDlg();
                },
                error:function(){
                    alert("请求失败!");
                }
            });
        }
    }else{
        alert("请正确填写信息!");
    }
 
}
//删除
function delRole(id){
    if(confirm("您确定要删除这条数据吗?")){
        $.ajax({
            url:'role/delRole.action',
            dataType:'json',
            type:'post',
            data:{rid:id},
            success:function(data){
                if(data>0){
                    alert("删除成功!");
                }else{
                    alert("删除失败!");
                }
                $("#test-table").bootstrapTable("refresh");
            }
        });
    }else{
        return false;
    }
}
 
//添加,打开模态框
function addRole(){
    $.ajax({
        url:'role/roleList.action',
        dataType:'json',
        type:'post',
        success:function(data){
            $("#pid").empty();
            $("#pid").append("<option value='0'>请选择上级角色</option>");
            $.each(data,function(){
                $("#pid").append("<option value='"+this.role_id+"'>"+this.role_name+"</option>");
            });
        },
        error:function(){
            alert("请求失败!");
        }
    });
    $("#addDlg").modal('show');
}
 
//添加
function insertRole(){
    if($("#info").text()=="名称可用"){
        if($("#addForm").data('bootstrapValidator').validate().isValid()){
            $.ajax({
                url:'role/saveRole.action',
                dataType:'json',
                type:'post',
                data:$("#addForm").serialize(),
                success:function(data){
                    if(data>0){
                        alert("添加成功!");
                    }else{
                        alert("添加失败!");
                    }
                    $("#test-table").bootstrapTable("refresh");
                    closeDlg();
                },
                error:function(){
                    alert("请求失败!");
                }
            });
        }
    }else{
        alert("请正确填写信息!");
    }
}
 
 
//添加 角色名称验证
function checkRole(){
    if($("#role_name").val()!=""){
        $.ajax({
            url:'role/checkRole.action',
            dataType:'json',
            type:'post',
            data:{
                roleName:$("#role_name").val()
            },
            success:function(data){
                $("#info").text(data.msg);
            },
            error:function(){
                alert("请求失败!");
            }
        });
    }else{
        return false;
    }
}
 
//修改 角色名称验证
function checkRole1(){
    var id=$("#role_id1").val();
    if($("#role_name1").val()!=""){
        $.ajax({
            url:'role/checkRole.action',
            dataType:'json',
            type:'post',
            data:{
                roleName:$("#role_name1").val(),
                rid:id
            },
            success:function(data){
                $("#infos").text(data.msg);
            },
            error:function(){
                alert("请求失败!");
            }
        });
    }else{
        return false;
    }
}
 
//关闭模态框
function closeDlg(){
    $("#authDlg").modal('hide');
    $("#addDlg").modal('hide');
    $("#mydlg").modal('hide');
    $("#info").text(null);
    $("#infos").text(null);
    $("input[type=reset]").trigger("click");
    $('#myform').data('bootstrapValidator', null);
    $('#addForm').data('bootstrapValidator', null);
    formValidator();
}
 
//表单验证
function formValidator(){
    $("#addForm").bootstrapValidator({
        fields:{
            role_name:{
                validators:{
                    notEmpty:{
                        message:"角色名称不能为空"
                    },
                    stringLength:{
                        max:20,
                        message:'不能超过20个字符长度'
                    },
                }
            },
            role_desc:{
                validators:{
                    notEmpty:{
                        message:'角色描述不能为空',
                    },
                    stringLength:{
                        max:200,
                        message:'字符长度不能超过200'
                    }
                }
            },
            pid:{
                validators:{
                    notEmpty:{
                        message:'不能为空'
                    }
                }
            }
        }
    });
    
 
    $("#myform").bootstrapValidator({
        fields:{
            role_name:{
                validators:{
                    notEmpty:{
                        message:"角色名称不能为空"
                    },
                    stringLength:{
                        max:20,
                        message:'不能超过20个字符长度'
                    },
                }
            },
            role_desc:{
                validators:{
                    notEmpty:{
                        message:'角色描述不能为空',
                    },
                    stringLength:{
                        max:200,
                        message:'字符长度不能超过200'
                    }
                }
            },
            pid:{
                validators:{
                    notEmpty:{
                        message:'不能为空'
                    }
                }
            }
        }
    });
 
}