wxw
2025-05-12 c7c2f7aa20427204944ba80a2704232b2f281582
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
<!DOCTYPE html>
<html style="background-color: white;">
    <head>
        <meta charset="utf-8">
        <title>分配权限</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
        <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
        <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
    </head>
    <body style="height: 98%;">
        <div class="layui-tab">
            <!-- <ul class="layui-tab-title">
              <li class="layui-this">功能</li>
              <li>业务</li>
            </ul> -->
            <div class="layui-tab-content" >
                <!-- 业务 -->
                <div class="layui-tab-item layui-show">
                    <div class="layui-card">
                        <div  class="layui-form layui-card-header layuiadmin-card-header-auto" style="padding: 0;">
                            <div class="layui-form-item" >
                                <div class="layui-inline">
                                    <label class="layui-form-label">模块菜单</label>
                                    <div class="layui-input-block">
                                        <select name="MenuNo" id="MenuNo" lay-filter="lay-filter-selectMenuNo" >
                                            <!-- <option value=""></option> -->
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn layui-btn-sm" lay-submit lay-filter="btn" style="width: 60px;">保存</button>
                                    <!-- <button class="layui-btn layui-btn-sm" lay-submit lay-filter="close">取消</button> -->
                                </div>
                            </div>
                        </div>
                        <div class="layui-card-body" >
                            <div style="margin-bottom: 15px;">
                                <table id="Layappcontentlist"  lay-filter="Layappcontentlist"></table>
                            </div>
                            <div >
                                <table id="Layappcontentlist1" lay-filter="Layappcontentlist1"></table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
          </div>
        
        <script src="../../layuiadmin/layui/layui.js"></script>
        <script src="../../js/public.js"></script>
        <script src="../../js/jquery-3.5.1.min.js"></script>
        <script src="../../js/jquery.cookie.js"></script>
        <script>
            var uid = $.cookie('userId');
            layui.config({
                base: '../../layuiadmin/' //静态资源所在路径
            }).extend({
                index: 'lib/index' //主入口模块
            }).use(['index', 'table', 'laypage', 'layer'], function() {
                var table = layui.table,
                    form = layui.form,
                    laypage = layui.laypage,
                    layer = layui.layer;
                var ids = getQueryString("id");
                var pno1="";//当前模块菜单下拉框选择的值
                var pno2="";//当前页面表格点击行的值
                var pno3="";//当前按钮表格点击行的值
                var rightListStr="";//已有权限菜单MenuNo集合json
                
                //模块菜单下拉框
                sendData(IP + "/Sys/GetMenuListForRight", {level:'0'}, 'get', function(res) {
                    console.log(res)
                    if (res.code == 0) {//成功
                        for (var i = 0; i < res.data.length; i++) {
                            $("#MenuNo").append('<option value =' + res.data[i].MenuNo + '>' + 
                                    res.data[i].MenuName +'</option>');
                        }
                        $("#MenuNo option[value='" + res.data[0].MenuNo + "']").attr("selected","selected");
                        form.render('select');
                        pno1=res.data[0].MenuNo;
                        getHaveRight();
                    } 
                    else{ //不成功
                        layer.msg('获取模块菜单失败', {
                            icon: 2,
                            time: 2000 //2秒关闭(如果不配置,默认是3秒)
                        }, function() {
                            //回调
                            form.render('select');
                        });
                    }
                });
 
                //获取初始权限
                function getHaveRight(){
                    sendData(IP + "/Basis/GetRoleRightOneListById?Id="+ids,{},'get',function(res){
                        for (var i=0;i<res.data.length;i++) {
                            rightListStr += res.data[i].MenuNo + ',';
                        }
                        //渲染页面模块table数据
                        refreshTablePage(pno1);
                        console.log('rightListStr:'+rightListStr)
                    });                    
                }
 
                //监听模块菜单下拉
                form.on('select(lay-filter-selectMenuNo)', function(data){
                    pno1 = data.value;
                    //渲染页面模块table数据
                    refreshTablePage(pno1);                
                });
 
                //监听页面模块表格单击事件
                table.on('row(Layappcontentlist)', function(obj){
                    refreshTableBtn(obj.data.MenuNo);
                    pno2=obj.data.MenuNo;
                });
                //监听页面模块表格复选框单击事件
                table.on('checkbox(Layappcontentlist)', function(obj){
                    //全选
                    if(String(obj.type) == "all")
                    {
                        //选中
                        if(obj.checked == true)
                        {
                            //添加模块
                            if(rightListStr.indexOf(pno1)<=-1){
                                rightListStr=rightListStr+','+pno1
                            }    
                            //添加父级选中状态    
                            var menu = layui.table.cache.Layappcontentlist;
                            for(var i =0;i<menu.length;i++)
                            {
                                if(rightListStr.indexOf(menu[i].MenuNo)<=-1)
                                {
                                    rightListStr=rightListStr+','+menu[i].MenuNo
                                }                                    
                                //添加子级选中状态
                                sendData(IP + "/Basis/GetRoleRightRBAC?MenuNo="+menu[i].MenuNo,{},'get',function(res2){
                                    for(var j=0;j<res2.data.length;j++)
                                    {
                                        if(rightListStr.indexOf(res2.data[j].MenuNo)<=-1){
                                            rightListStr=rightListStr+','+res2.data[j].MenuNo
                                        }                                            
                                    }                    
                                })
                            }
                            //渲染按钮模块当前table数据
                            var menu2 = layui.table.cache.Layappcontentlist1;
                            for(var i =0;i<menu2.length;i++){
                                menu2[i].LAY_CHECKED=true;
                            }
                            $.extend(infoOptions1, {
                                data: menu2,
                            });
                            table.render(infoOptions1);                
                        }
                        //取消选中
                        if(obj.checked == false){
                            //取消模块
                            rightListStr=rightListStr.replace(pno1,'');
                            //取消父级选中状态    
                            var menu = layui.table.cache.Layappcontentlist;
                            for(var i =0;i<menu.length;i++)
                            {
                                rightListStr=rightListStr.replace(menu[i].MenuNo,'');                                
                                //添加子级选中状态
                                sendData(IP + "/Basis/GetRoleRightRBAC?MenuNo="+menu[i].MenuNo,{},'get',function(res2){
                                    for(var j=0;j<res2.data.length;j++)
                                    {
                                        rightListStr=rightListStr.replace(res2.data[j].MenuNo,'');                                            
                                    }                    
                                })
                            }
                            //渲染按钮模块当前table数据
                            var menu2 = layui.table.cache.Layappcontentlist1;
                            for(var i =0;i<menu2.length;i++){
                                menu2[i].LAY_CHECKED=false;
                            }
                            $.extend(infoOptions1, {
                                data: menu2,
                            });
                            table.render(infoOptions1);    
                        }
                    }
                    else if(String(obj.type) == "one")//单选
                    {
                        //选中
                        if(obj.checked == true)
                        {
                            //添加模块
                            if(rightListStr.indexOf(pno1)<=-1){
                                rightListStr=rightListStr+','+pno1
                            }    
                            //添加父级选中状态
                            rightListStr=rightListStr+','+pno2
 
                            //添加子级选中状态    
                            sendData(IP + "/Basis/GetRoleRightRBAC?MenuNo="+pno2,{},'get',function(res){
                                for(var j=0;j<res.data.length;j++)
                                {
                                    rightListStr=rightListStr+','+res.data[j].MenuNo;
                                }
                                //渲染按钮模块table数据
                                var menu2 = layui.table.cache.Layappcontentlist1;
                                for(var i =0;i<menu2.length;i++){
                                    menu2[i].LAY_CHECKED=true;
                                }
                                $.extend(infoOptions1, {
                                    data: menu2,
                                });
                                table.render(infoOptions1);    
                            })
                        }
                        //取消选中
                        if(obj.checked == false)
                        {
                            //判断取消后还有没有选中的
                            var menuCheck=false;
                            var menu = layui.table.cache.Layappcontentlist;
                            for(var i=0;i<menu.length;i++)
                            {
                                if(menu[i].LAY_CHECKED == true)
                                {
                                    menuCheck=true;
                                    continue;
                                }
                            }
                            if(!menuCheck){
                                //取消模块
                                rightListStr=rightListStr.replace(pno1,'');
                            }
                            //取消父级选中状态    
                            rightListStr=rightListStr.replace(pno2,'');        
 
                            //取消子级选中状态
                            sendData(IP + "/Basis/GetRoleRightRBAC?MenuNo="+pno2,{},'get',function(res){
                                for(var j=0;j<res.data.length;j++)
                                {
                                    rightListStr=rightListStr.replace(res.data[j].MenuNo,'');                                
                                }
                                //渲染按钮模块table数据    
                                var menu2 = layui.table.cache.Layappcontentlist1;
                                for(var i =0;i<menu2.length;i++){
                                    menu2[i].LAY_CHECKED=false;
                                }
                                $.extend(infoOptions1, {
                                    data: menu2,
                                });
                                table.render(infoOptions1);                    
                            })
                        }
                    }                    
                });
 
                //监听按钮模块表格单击事件
                table.on('row(Layappcontentlist1)', function(obj){
                    pno3=obj.data.MenuNo;
                });
                //监听按钮模块表格复选框单击事件
                table.on('checkbox(Layappcontentlist1)', function(obj){
                    //全选
                    if(String(obj.type) == "all")
                    {
                        //选中
                        if(obj.checked == true)
                        {
                            //添加模块
                            if(rightListStr.indexOf(pno1)<=-1){
                                rightListStr=rightListStr+','+pno1
                            }
                            //添加父级选中状态
                            if(rightListStr.indexOf(pno2)<=-1){
                                rightListStr=rightListStr+','+pno2
 
                                var menu = layui.table.cache.Layappcontentlist;
                                for(var aa =0;aa<menu.length;aa++){
                                    if(menu[aa].MenuNo==pno2){
                                        menu[aa].LAY_CHECKED=true;
                                        break;
                                    }
                                }
                                $.extend(infoOptions, {
                                    data: menu,
                                });
                                table.render(infoOptions);
                            }
                            //添加子级选中状态
                            var menu2 = layui.table.cache.Layappcontentlist1;
                            for(var aa =0;aa<menu2.length;aa++){
                                if(rightListStr.indexOf(menu2[aa].MenuNo)<=-1){
                                    rightListStr=rightListStr+','+menu2[aa].MenuNo
                                }
                            }                            
                        }
                        //取消选中
                        if(obj.checked == false){
                            //取消子级选中状态
                            var menu2 = layui.table.cache.Layappcontentlist1;
                            for(var aa =0;aa<menu2.length;aa++){
                                rightListStr=rightListStr.replace(menu2[aa].MenuNo,'');
                            }
                        }
                    }
                    else if(String(obj.type) == "one")//单选
                    {
                        //选中
                        if(obj.checked == true)
                        {
                            //添加模块
                            if(rightListStr.indexOf(pno1)<=-1){
                                rightListStr=rightListStr+','+pno1
                            }
                            //添加父级选中状态
                            if(rightListStr.indexOf(pno2)<=-1){
                                rightListStr=rightListStr+','+pno2
 
                                var menu = layui.table.cache.Layappcontentlist;
                                for(var aa =0;aa<menu.length;aa++){
                                    if(menu[aa].MenuNo==pno2){
                                        menu[aa].LAY_CHECKED=true;
                                        break;
                                    }
                                }
                                $.extend(infoOptions, {
                                    data: menu,
                                });
                                table.render(infoOptions);
                            }
                            //添加子级选中状态
                            rightListStr=rightListStr+','+pno3
                        }
                        //取消选中
                        if(obj.checked == false)
                        {
                            //取消子级选中状态
                            rightListStr=rightListStr.replace(pno3,'');
                        }
                    }                    
                });
                
 
                var infoOptions = {
                    elem: '#Layappcontentlist',
                    height: 300,
                    id: 'Layappcontentlist',
                    page: false,
                    limit: Number.MAX_VALUE,
                    cols: 
                    [[
                        {
                            title: '复选框', field: 'Lay_Checked', fixed: 'left', type: 'checkbox', width:65
                        },
                        // {
                        //     title: '序号', type: 'numbers', fixed: 'left'
                        // },
                        {
                            field: 'MenuNo', title: '菜单号', align: 'center'
                        },
                        {
                            field: 'MenuName', title: '菜单名称', align: 'center'
                        }
                    ]]
                };
                //渲染页面模块table数据
                function refreshTablePage(MenuNo){                    
                    sendData(IP + "/Basis/GetRoleRightRBAC?MenuNo="+MenuNo,{},'get',function(res){
                        //将权限进行勾选
                        var arr = rightListStr.split(',');
                        for(var a=0;a<arr.length;a++)
                        {                            
                            for(var i=0;i<res.data.length;i++)
                            {
                                if(arr[a] == res.data[i].MenuNo)
                                {
                                    res.data[i].LAY_CHECKED = true;
                                    continue;
                                }
                            }
                        }                    
                        $.extend(infoOptions, {
                            data: res.data,
                        });
                        table.render(infoOptions);
                        //渲染按钮模块table数据
                        refreshTableBtn(res.data[0].MenuNo);    
                        pno2=res.data[0].MenuNo;                        
                    })
                }
                
                var infoOptions1 = {
                    elem: '#Layappcontentlist1',
                    height: 260,
                    id: 'Layappcontentlist1',
                    page: false,
                    limit: Number.MAX_VALUE,
                    cols: 
                    [[
                        {
                            title: '复选框', field: 'Lay_Checked', fixed: 'left', type: 'checkbox', width:65
                        },
                        // {
                        //     title: '序号', type: 'numbers', fixed: 'left'
                        // },
                        {
                            field: 'MenuNo', title: '菜单号', align: 'center'
                        },
                        {
                            field: 'MenuName', title: '菜单名称', align: 'center'
                        }
                    ]]
                };
                //渲染按钮模块table数据
                function refreshTableBtn(MenuNo){
                    
                    sendData(IP + "/Basis/GetRoleRightRBAC?MenuNo="+MenuNo,{},'get',function(res){
                        //将权限进行勾选
                        var arr = rightListStr.split(',');
                        for(var a=0;a<arr.length;a++)
                        {                            
                            for(var i=0;i<res.data.length;i++)
                            {
                                if(arr[a] == res.data[i].MenuNo)
                                {
                                    res.data[i].LAY_CHECKED = true;
                                    continue;
                                }
                            }
                        }
                        $.extend(infoOptions1, {
                            data: res.data,
                        });
                        table.render(infoOptions1);                        
                    })
                }
 
                //监听保存
                form.on('submit(btn)', function(){
                    //去重
                    var arr = rightListStr.split(',')
                    var formArr = arr.sort()
                    var newArr=[formArr[0]]
                    for (let i = 1; i < formArr.length; i++)
                    {
                        if (formArr[i]!=formArr[i-1]) 
                        {
                            newArr.push(formArr[i])
                        }
                    }
                    //去重后重新赋值
                    rightListStr = "";
                    for(var c=0;c<newArr.length;c++)
                    {
                        if (rightListStr == "")
                        {
                            rightListStr = newArr[c];
                        }
                        else
                        {
                            rightListStr = rightListStr + ',' + newArr[c];
                        }
                    }
                    synData(IP +"/basis/InsertRoleRight?MenuNo="+rightListStr+"&&Id="+ids+"",{},"post",function (res) {
                        if (res.code == 0)
                        {
                            //成功
                            layer.msg(res.msg,{
                                icon: 1,
                                time: 2000, //2秒关闭(如果不配置,默认是3秒)
                            },function () {
                                var index = parent.layer.getFrameIndex(window.name)
                                parent.layer.close(index); //关闭弹层
                            });
                        }
                        else
                        {
                            //不成功
                            layer.msg(res.msg,{
                                    icon: 2,
                                    time: 2000, //2秒关闭(如果不配置,默认是3秒)
                                },function () {
                                    this.doing = true;
                                }
                            );
                        }
                    });
 
                });
 
                //获取浏览器参数
                function getQueryString(name) {
                    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
                    var r = window.location.search.substr(1).match(reg);
                    if (r != null) return unescape(r[2]);
                    return null;
                }
            });
        </script>
    </body>
</html>