王佳豪
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/**
 * 用户
 */
$(function (){
    getDeptLists();
    getRoleList();
    
//    //select2 多选
//    $("#rid").select2({
//        //minimumInputLength: 1, 
//        language: "zh-CN", //设置 提示语言
//        maximumSelectionLength: 3,  //设置最多可以选择多少项
//         //width: "100%", //设置下拉框的宽度
//         placeholder: "请选择",
//         tags: true,
//    });
    
    $("#test-table").bootstrapTable('destroy');
    $('#test-table').bootstrapTable({
        method : 'GET', //默认是post,不允许对静态文件访问
//        url: "user/userList.action",
        url: "user/getUserByCon.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,
        minimumCountColumns: 2,// 设置最少显示列个数
        clickToSelect: true, // 单击行即可以选中
        search : false,//显示搜素表单
        silent : true, //刷新事件必须设置
        sidePagination : "server", //表示服务端请求  
        columns : [ {
            checkbox:true
        }   ,{
            field : "user_id",
            title : "员工编号",
            class : 'col-md-1',
            align : "center",
            valign : "middle",
            sortable : "true"
        }, {
            field : "user_account",
            title : "登陆账号",
            align : "center",
            valign : "middle",
            sortable : "true"
        }, {
            field : "user_name",
            title : "姓名",
            align : "center",
            valign : "middle",
            sortable : "true"
//        }, {
//            field : "user_password",
//            title : "密码",
//            align : "center",
//            valign : "middle",
//            sortable : "true"
//        }, {
//            field : "user_age",
//            title : "年龄",
//            align : "center",
//            valign : "middle",
//            sortable : "true"
//        }, {
//            field : "user_sex",
//            title : "性别",
//            align : "center",
//            valign : "middle",
//            sortable : "true"
//        }, {
//            field : "dept_name",
//            title : "所在部门",
//            align : "center",
//            valign : "middle",
//            sortable : "true"
//        }, {
//            field : "user_address",
//            title : "地址",
//            align : "center",
//            valign : "middle",
//            sortable : "true"
//        }, {
//            field : "birthStr",
//            title : "出生日期",
//            align : "center",
//            valign : "middle",
//            sortable : "true"
//        }, {
//            field : "user_phone",
//            title : "联系电话",
//            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,
                userId:$("#userId").val(),
                userName:$("#userName").val(),
                deptId:$("#deptId").val()
                // searchText: params.searchText
            };
            return param;
        },
        formatLoadingMessage : function() {
            return "请稍等,正在加载中...";
        },
 
        formatNoMatches : function() {
            return '无符合条件的记录';
        }
    });
});
 
function operateFormatter(value, row, index) {
    return ['<button type="button" style="display:'+roleEnable+'" class=" btn btn-primary" onclick="getRole('+row.user_id+')">角色</button>&nbsp;&nbsp;&nbsp;',
        '<button type="button" style="display:'+updateEnable+'" class=" btn btn-info" onclick="getValue('+row.user_id+')">修改</button>',
        '&nbsp;&nbsp;&nbsp;<button style="display:'+deleteEnable+'" class=" btn btn-danger" type="button" onclick="delUser('+row.user_id+')">删除</button>'
        ].join('');
}
 
//条件查询
function getUserByCon(){
        $("#test-table").bootstrapTable('refreshOptions',{pageNumber:1}); //分页后重新搜索时,初始化页码为1
//        $("#test-table").bootstrapTable('refresh'); 
}
 
 
//查询所有部门 追加到 条件查询中
function getDeptLists(){
    $.ajax({
        url:'dept/getDeptList.action',
        dataType:'json',
        type:'post',
        success:function(data){
            $("#deptId").empty();
            $("#deptId").append("<option value='0'>请选择所属部门</option>");
            $.each(data,function(){
                $("#deptId").append("<option value='"+this.dept_id+"'>"+this.dept_name+"</option>");
            });
        },
        error:function(){
            alert("请求失败!");
        }
    });
}
 
function getRoleList(){
    $.ajax({
        url:'user/getRoleList.action',
        dataType:'json',
        type:'post',
        success:function(data){
            $("#rid").empty();
            if(data.rows.length>1){
                $("#rid").append("<option value=''>请选择堆角色</option>");
            }
            $.each(data.rows,function(){
                $("#rid").append("<option value='"+this.role_id+"'>"+this.role_name+"</option>");
            });
        },
        error:function(){
            alert("请求失败!");
        }
    });
}
 
//打开  导入 
function ImportExcel(){
    $("#importDlg").modal('show');
}
//导入
function importExcel(){
    if($("#importForm").data('bootstrapValidator').validate().isValid()){
        $.ajax({
            url:'user/import.action',
            dataType:'json',
            type:'post',
            data:new FormData($("#importForm")[0]),
            contentType: false, //必须false才会避开jQuery对 formdata 的默认处理 
            processData: false, //必须false才会自动加上正确的Content-Type
            success:function(data){
                if(data.flag){
                    alert("导入成功!"+data.msg);
                }else{
                    alert("导入失败!"+data.msg);
                }
                closeDlg();
                $("#test-table").bootstrapTable('refresh');
            },
            error:function(){
                alert("请求失败!");
            }
        });
    }else{
        return false;
    }
}
 
//导出
function ExportExcel(){
    if(confirm("您确定要导出所有用户信息吗?")){
        location.href="user/export.action";
    }else{
        return false;
    }
}
 
 
//角色分配
function getRole(uid){
    $("#hid").val(uid);
    $.ajax({
        url:'user/getRole.action',
        dataType:'json',
        type:'post',
        traditional:true,
        data:{
            uid:uid
        },
        success:function(data){
            console.log(data);
            $("#rid").val(data.userRole);
//            $("#rid").empty();
//            $.each(data.role,function(index,items){
//                $("#rid").append("<option value='"+items.role_id+"'>"+items.role_name+"</option>");
//            });
//            if((data.userRole!=null)){
//                $.each(data.userRole,function(index,items){
//                    $("#rid").val(data.userRole).trigger("change");//select2 选中
//                });
//            }else{
//                $("#rid").val(0).trigger("change");
//            }
        },
        error:function(){
            alert("请求失败!");
        }
    })
    $("#roleDlg").modal('show');
}
//保存修改角色
function saveRole(){
    var uid=$("#hid").val();
    //var rid=$("#rid option:selected").val();
    var rids=$("#rid").val();//select2 获取多选值
    if(rids==null || rids==""){
        alert('请选择角色');
        return;
    }
    $.ajax({
        url:'user/upUserRple.action',
        dataType:'json',
        type:'post',
        traditional:true,
        data:{
            uid:uid,
            rid:rids
        },
        success:function(data){
            if(data){
                alert("保存成功!");
            }else{
                alert("保存失败!");
            }
            closeDlg();
            $('#test-table').bootstrapTable('refresh'); 
        }
    })
};
 
//批量删除
function delMany(){
    var idstr=[];
    //获取选中的行
    var a= $('#test-table').bootstrapTable('getSelections');  
    if(a.length>0){  
        if(confirm("您确定要删除选中的数据吗?")){
            $.each(a,function(index){
                idstr.push(a[index].user_id);
            });
            if(idstr.indexOf(1)>=0){
                alert('系统默认用户,不能删除');
                return;
            }
            $.ajax({
                url:'user/delUsers.action',
                dataType:'json',
                type:'post',
                traditional:true,
                data:{
                    ids:idstr
                },
                success:function(data){
                    if(data>0){
                        alert("删除成功!");
                    }else{
                        alert("删除失败!");
                    }
                    $('#test-table').bootstrapTable('refresh'); 
                },
                error:function(){
                    alert("请求失败!");
                }
            });
        }else{
            return false;
        }
    }else{
        alert("请至少选中一行数据");
    }
}
 
//修改前,打开模态框
function getValue(id){
    $.ajax({
        url:'user/getUserById.action',
        dataType:'json',
        type:'post',
        data:{
            uid:id
        },
        success:function(data){
            $("#user_id1").val(data.user.user_id);
            $("#user_account1").val(data.user.user_account);
            $("#user_password1").val(data.user.user_password);
            $("#user_name1").val(data.user.user_name);
//            $("#user_age1").val(data.user.user_age);
//            $("#user_address1").val(data.user.user_address);
//            $("#user_birth1").val(data.user.birthStr);
//            $("#email1").val(data.user.email);
//            $("#user_phone1").val(data.user.user_phone);
//            
//            if("男"==data.user.user_sex){
//                $("#nan1").prop('checked',true);
//            }else{
//                $("#nv1").prop('checked',true);
//            }
//            
//            $("#sid1").empty();
//            $("#sid1").append("<option value='0'>请选择</option>");
//            $.each(data.dept,function(){
//                if(data.user.dept_id==this.dept_id){
//                    $("#sid1").append("<option selected value='"+this.dept_id+"'>"+this.dept_name+"</option>");
//                }else{
//                    $("#sid1").append("<option value='"+this.dept_id+"'>"+this.dept_name+"</option>");
//                }
//            });
            $("#mydlg1").modal('show');
        },
        error:function(){
            alert('请求失败!');
        }
    });
}
//修改用户
function upUser(){
        if($("#myform1").data("bootstrapValidator").validate().isValid()){
            $.ajax({
                url:'user/upUser.action',
                dataType:'json',
                type:'post',
                data:$("#myform1").serialize(),
                success:function(data){
                    if(data>0){
                        alert("修改成功!");
                    }else{
                        alert("修改失败!");
                    }
                    closeDlg();
                    $("#test-table").bootstrapTable('refresh');
                }
            });
        }else{
            alert("请按规则填写信息");
        }
}
 
//删除员工
function delUser(id){
    if(id=='1'){
        alert('超级管理员不允许删除');
    }
    return;
    if(confirm("您确定要删除这条数据吗?")){
        $.ajax({
            url:'user/delUser.action',
            dataType:'json',
            type:'post',
            data:{uid:id},
            success:function(data){
                if(data>0){
                    alert("删除成功!");
                }else{
                    alert("删除失败!");
                }
                $("#test-table").bootstrapTable('refresh');
            },
            error:function(){
                alert("请求失败!");
            }
        });
    }
}
//添加,打开模态框
function openDlg(){
    getDeptList();
    $("#mydlg").modal('show');
};
 
//关闭模态框
function closeDlg(){
    $("#importDlg").modal('hide');
    $("#roleDlg").modal('hide');
    $("#mydlg1").modal('hide');
    $("#mydlg").modal('hide');
    $("#mid").text(null);
    $("input[type=reset]").trigger("click");
    $('#importForm').data('bootstrapValidator', null);
    $('#myform').data('bootstrapValidator', null);
    $('#myform1').data('bootstrapValidator', null);
    formValidator();
};
 
//添加用户
function saveUser(){
    var msg=$("#mid").text();
    if(msg=="此账号可用"){
        if($("#myform").data('bootstrapValidator').validate().isValid()){
            $.ajax({
                url:'user/saveUser.action',
                type:'post',
                dataType:'json',
                data:$("#myform").serialize(),
                success:function(data){
                    if(data>0){
                        alert("添加成功!");
                    }else{
                        alert("添加失败!");
                    }
                    $("#test-table").bootstrapTable('refresh');
                    closeDlg();
                },
                error:function(){
                    alert("请求失败");
                }
            });
        }else{
            alert("请填写合法信息");
        }
    }else{
        alert("请填写合法信息");
    }
}
 
//添加用户时验证登录账号是否唯一
function validAccount(){
    var x=$("#user_account").val();
    if(x!=""){
        $.ajax({
            url:'user/validAccount.action',
            dataType:'json',
            type:'post',
            data:{
                account:$("#user_account").val()
            },
            success:function(data){
                $("#mid").text(data.msg);
            },
            error:function(){
                alert("请求失败");
            }
        });
    }else{
        return false;
    }
};
 
 
 
//查询所有部门
function getDeptList(){
    $.ajax({
        url:'dept/getDeptList.action',
        dataType:'json',
        type:'post',
        success:function(data){
            $("#sid").empty();
            $("#sid").append("<option value='0'>请选择</option>");
            $.each(data,function(){
                $("#sid").append("<option value='"+this.dept_id+"'>"+this.dept_name+"</option>");
            });
        },
        error:function(){
            alert("请求失败!");
        }
    });
}
//当修改密码后清空确认密码
function clearPass(){
    $("#againpass").val("");
    $('#myform').data('bootstrapValidator', null);
    formValidator();
}
 
 
$(document).ready(function(){
    formValidator();
});
function formValidator(){
    $("#myform").bootstrapValidator({
        fields:{
            user_account:{
                validators:{
                    notEmpty:{
                        message:"登陆账号不能为空"
                    },
                    stringLength:{
                        max:20,
                        message:"字符长度不能超过20个字符"
                    }
                }
            },
            user_name:{
                validators:{
                    notEmpty:{
                        message:'姓名不能为空'
                    },
                    stringLength:{
                        max:20,
                        message:'字符长度不能超过20个字符'
                    }
                }
            },
            user_password:{
                validators:{
                    notEmpty:{
                        message:'密码不能为空'
                    },
                    stringLength:{
                        min:6,
                        max:18,
                        message:'字符长度要在6~18之间'
                    }
                }
            },
            againpass:{
                validators:{
                    notEmpty:{
                        message:'密码不能为空'
                    },
                    identical:{
                        field:'user_password',
                        message:'两次输入密码不一致'
                    }
                }
            }//,
//            user_sex:{
//                validators:{
//                    notEmpty:{
//                        message:'性别不能为空'
//                    }
//                }
//            },
//            user_age:{
//                validators:{
//                    notEmpty:{
//                        message:'年龄不能为空'
//                    },
//                    digits:{
//                        message: '该值只能包含数字'
//                    },
//                    stringLength:{
//                        max:3,
//                        message:'年龄不能超过三位数'
//                    }
//                }
//            },
//            user_birth:{
//                validators:{
//                    notEmpty:{
//                        message:"出生日期不能为空"
//                    }
//                }
//            },
//            user_phone:{
//                validators:{
//                    notEmpty:{
//                        message:'电话不能为空'
//                    },
//                    digits:{
//                        message: '该值只能包含数字'
//                    },
//                    stringLength:{
//                        min:8,
//                        max:11,
//                        message:'长度在8~11之间'
//                    }
//                }
//            },
//            email:{
//                validators:{
//                    notEmpty:{
//                        message:"Email不能为空"
//                    },
//                    emailAddress: {
//                         message: 'Email格式不符合法'
//                    }
//                }
//            },
//            dept_id:{
//                validators:{
//                    notEmpty:{
//                        message:'部门不能为空'
//                    }
//                }
//            },
//            user_address:{
//                validators:{
//                    notEmpty:{
//                        message:'地址不能为空'
//                    },
//                    stringLength:{
//                        max:200,
//                        message:"字符长度不能超过200"
//                    }
//                }
//            }
        }
    });
    
 
    $("#myform1").bootstrapValidator({
        fields:{
            user_id:{
                validators:{
                    notEmpty:{
                        message:'用户编号不能为空'
                    },
                }
            },
            user_account:{
                validators:{
                    notEmpty:{
                        message:"登陆账号不能为空"
                    },
                    stringLength:{
                        max:20,
                        message:"字符长度不能超过20个字符"
                    }
                }
            },
            user_name:{
                validators:{
                    notEmpty:{
                        message:'姓名不能为空'
                    },
                    stringLength:{
                        max:20,
                        message:'字符长度不能超过20个字符'
                    }
                }
            },
            user_password:{
                validators:{
                    notEmpty:{
                        message:'密码不能为空'
                    },
                    stringLength:{
                        min:6,
                        max:18,
                        message:'字符长度要在6~18之间'
                    }
                }
            }//,
//            user_sex:{
//                validators:{
//                    notEmpty:{
//                        message:'性别不能为空'
//                    }
//                }
//            },
//            user_age:{
//                validators:{
//                    notEmpty:{
//                        message:'年龄不能为空'
//                    },
//                    digits:{
//                        message: '该值只能包含数字'
//                    },
//                    stringLength:{
//                        max:3,
//                        message:'年龄不能超过三位数'
//                    }
//                }
//            },
//            user_birth:{
//                validators:{
//                    notEmpty:{
//                        message:"出生日期不能为空"
//                    }
//                }
//            },
//            user_phone:{
//                validators:{
//                    notEmpty:{
//                        message:'电话不能为空'
//                    },
//                    digits:{
//                        message: '该值只能包含数字'
//                    },
//                    stringLength:{
//                        min:8,
//                        max:11,
//                        message:'长度在8~11之间'
//                    }
//                }
//            },
//            email:{
//                validators:{
//                    notEmpty:{
//                        message:"Email不能为空"
//                    },
//                    emailAddress: {
//                         message: 'Email格式不符合法'
//                    }
//                }
//            },
//            dept_id:{
//                validators:{
//                    notEmpty:{
//                        message:'部门不能为空'
//                    }
//                }
//            },
//            user_address:{
//                validators:{
//                    notEmpty:{
//                        message:'地址不能为空'
//                    },
//                    stringLength:{
//                        max:200,
//                        message:"字符长度不能超过200"
//                    }
//                }
//            }
        }
    });
    $("#importForm").bootstrapValidator({
        fields:{
            files:{
                validators:{
                    notEmpty:{
                        message:"导入文件不能为空"
                    },
                }
            },
        }
    });
    
}