1
81041
2019-06-20 ab3c4acf83f54f8449ca8664c4a2bb79bd30f297
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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
//获取连接中的指定参数
function getQueryString(name) { 
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
    var r = window.location.search.substr(1).match(reg); 
    if (r != null) {
        return decodeURI(r[2]);
    }     
    return null; 
}
 
// 生成特定的select列表
function createLayuiSelect(ele, list) {
    // 清空内容
    ele.text('');
    // 遍历list结果集生成下拉
    for(var i=0; i<list.length; i++) {
        var _list = list[i];
        var option = $('<option></option>');    // option元素
        // 设置option的文本/属性值/data值
        option.text(_list.txt);
        option.val(_list.val);
        option.attr('selected', _list.selected);
        option.data('data', _list.data);
        Object.keys(_list.attr).forEach(function(key){
            var val = _list.attr[key];
            option.attr(key, val);
        });
        ele.append(option);
    }
}
 
// 关闭iframe弹出层
function hideParentIframe() {
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index); //再执行关闭 
}
 
// 检测下拉框的必要条件是否存在
function checkLayuiParent(parent) {
    var status = false;
    var pVal = parent.val();
    status = pVal?true:false;        // 根据pVal的值返回必要条件是否存在
    return status;
}
 
// 构造生成select列表的对象数组
function getLayuiSelect(txt, val, attr, data, isSelected) {
    var obj = {
        val: '',
        txt: '',
        attr: {},
        data: {}
    };
    
    // 设置内容的值
    obj.val = val;        // option的value值
    obj.txt = txt;        // option的文本值    
    obj.attr = attr;    // option的自定义的属性
    obj.data = data;    // option的data值
    obj.selected = isSelected?true:false;
    
    // 返回构造的对象
    return obj;
}
 
//格式化时间
Date.prototype.format =function(format)
{
    var o = {
    "M+" : this.getMonth()+1, //month
    "d+" : this.getDate(),    //day
    "h+" : this.getHours(),   //hour
    "m+" : this.getMinutes(), //minute
    "s+" : this.getSeconds(), //second
    "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
    "S" : this.getMilliseconds() //millisecond
    };
    if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
    (this.getFullYear()+"").substr(4- RegExp.$1.length));
    for(var k in o)if(new RegExp("("+ k +")").test(format))
    format = format.replace(RegExp.$1,
    RegExp.$1.length==1? o[k] :
    ("00"+ o[k]).substr((""+ o[k]).length));
    return format;
};
 
// 页面中分页信息对象
var TblPage = function() {
    this.size = 10;
    this.curr = 1;
    this.all = 0;
    this.num = 1;
};
 
// 初始化分页信息对象
TblPage.prototype.init = function() {
    this.size = 10;
    this.curr = 1;
    this.all = 0;
    this._setNum();
};
 
// 设置分页信息对象
TblPage.prototype.set = function(curr, all, size) {
    this.size = size;
    this.curr = curr;
    this.all = all;
    this._setNum();
};
TblPage.prototype.getPage = function() {
    var temp = {
        pageSize: this.size
        ,pageCurr: this.curr
        ,pageAll: this.all
    };
    
    return temp;
};
// 设置分页信息的当前页
TblPage.prototype.setCurr = function(curr) {
    this.curr = curr;
};
 
//设置分页信息的每页显示的条数
TblPage.prototype.setSize = function(size) {
    this.size = size;
    this._setNum();
};
 
//设置分页信息的总条数
TblPage.prototype.setAll = function(all) {
    this.all = all;
    this._setNum();
};
 
// 设置共有多少页
TblPage.prototype._setNum = function() {
    this.num = Math.ceil(this.all/this.size);
}
 
// 定义页面中分页元素的对象
var PagePage = function(opts) {
    this.search = '';        // 查询
    this.home = '';                // 首页
    this.pre = '';                // 上一页
    this.next = '';                // 下一页
    this.last = '';                // 尾页
    this.size = '';                // 每页显示条数
    this.num = '';                // 跳转的页数
    this.go = '';                // 页面跳转
    this.current = '';            // 当前页/总页数
    this.total = '';            // 数据总量
    this.page = opts.page;        // 分页信息
    this._initDom(opts);
    this.callback = "";
}
 
// 设置PagePage的方法
PagePage.prototype = {
    _initDom: function(opts) {
        this.search = this._getEle(opts.search);
        this.home = this._getEle(opts.home);
        this.pre = this._getEle(opts.pre);
        this.next = this._getEle(opts.next);
        this.last = this._getEle(opts.last);
        this.num = this._getEle(opts.num);
        this.go = this._getEle(opts.go);
        this.current = this._getEle(opts.current);
        this.total = this._getEle(opts.total);
        this.size = this._getEle(opts.size);
        //console.log(this.search);
        this.addEvent();
    }
    ,_getEle: function(ele) {
        //console.log(ele);
        var len = ele?ele.length:0;
        //console.log(len);
        var rsEle = len?ele:$('#PagePageTmp');
        return rsEle;
    }
    ,addEvent: function() {
        var _this = this;
        
        //  点击查询
        this.search.off('click.PagePage.event').on('click.PagePage.event', function() {
            if(_this._checkCallback()) {
                _this.callback();
            } 
        });
        
        // 点击首页
        this.home.off('click.PagePage.event').on('click.PagePage.event', function() {
            var Page = _this.page;
            if(Page.curr != 1){
                Page.setCurr(1);
                _this.callback();
            }
        });
        
        // 点击上一页
        this.pre.off('click.PagePage.event').on('click.PagePage.event', function() {
            var Page = _this.page;
            if(Page.curr > 1){
                Page.setCurr(Page.curr-1);
                _this.callback();
            }
        });
        
        // 点击下一页
        this.next.off('click.PagePage.event').on('click.PagePage.event', function() {
            var Page = _this.page;
            if(Page.num > Page.curr){
                Page.setCurr(Page.curr+1);
                _this.callback();
            }
        });
        
        // 设置每页显示条数
        this.size.off('blur.PagePage.event').on('blur.PagePage.event', function() {
            var Page = _this.page;
            var value= $(this).val();
            //当输入的数大于0时
            if(value>0){
                if(value != Page.size){
                    Page.setSize(parseInt(value));
                    Page.setCurr(1);
                }
            }else{
                //当输入非法数字时
                alert("请输入合法的数字");                        /* 请输入合法的整数 */
                $(this).val(Page.size);                    
            }
        });
        
        // 尾页
        this.last.off('click.PagePage.event').on('click.PagePage.event', function() {
            var Page = _this.page;
            if(Page.curr < Page.num){
                Page.setCurr(Page.num);
                _this.callback();
            }
        });
        
        // 跳转
        this.go.off('click.PagePage.event').on('click.PagePage.event', function() {
            var Page = _this.page;
            var tarpage= _this.num.val();
            if(tarpage > Page.num){
                Page.setCurr(Page.num);
            }else if(tarpage > 0 && tarpage!= Page.curr){
                Page.setCurr(parseInt(tarpage));
            }
            _this.callback();
        });
        
    }
    ,setCallback:function(callback) {
        this.callback = callback;
    }
    ,setPage: function(page) {
        this.page = page;
    }
    ,init:function(page, callback) {
        this.setPage(page);
        this.setCallback(callback);
        this.setVal();
    }
    ,setVal: function() {
        this.current.text(this.page.curr+'/'+this.page.num);
        this.total.text(this.page.all);
        this.size.val(this.page.size);
    }
    ,_checkCallback: function() {
        return typeof this.callback === 'function';
    }
};
 
// 省-市-区联动
var LinkAge = function(url, province, city, county, callback) {
    this.province = province;
    this.city = city;
    this.county = county;
    this.url = url;
    this.callback= callback;
    this.data = {};
    this._init();
};
LinkAge.prototype = {
    _init: function() {
        // 设置回调函数
        if(typeof this.callback === 'function') {
            this.callback = this.callback;
        }else {
            this.callback = function() {};
        }
        
        this._setData();
        this._updateDom();
        // this._addEvent();
    }
    ,_setData: function() {
        var _this = this;
        // 请求json数据
        $.ajax({
            type: 'post'
            ,async: false
            ,url: this.url
            ,data: null
            ,dataType: 'json'
            ,success: function(res) {
                _this.data = res;
            },
            error: function(res) {
                console.log(res)
            }
        });
    }
    ,_getProvinces: function() {
        var provinces = this._analyseData(this.data);
        return provinces;
    }
    ,_getCities: function(province) {
        var cities = this.data[province].child;
        var rsCities = this._analyseData(cities);
        return rsCities;
    }
    ,_getCounty: function(province, city) {
        var cities = this.data[province].child;
        var counties = cities[city].child;
        var rsCounties = this._analyseData(counties);
        return rsCounties;
    }
    ,_analyseData: function(data) {
        var rsData = [];
        // 解析数据
        Object.keys(data).forEach(function(key) {
            var _data = data[key];
            if(_data)
            var tmp = {
                id: key
                ,name: _data.name
            };
 
            if(!_data.child && _data == '市辖区') {
 
            }else {
                tmp.id = key;
                tmp.name = _data.child?_data.name:_data;
                rsData.push(tmp);
            }
            
        });
 
        return rsData;
    }
    ,_updateDom: function() {
        // 省
        var proData = this._getProvinces();
        var proOptions = this._createDom(proData);
        this.province.html(proOptions);
        
        // 市
        var citiesData = this._getCities(this.province.find('option:selected').attr('num'));
        var citiesOptions = this._createDom(citiesData);
        this.city.html(citiesOptions);
 
        // 区/县
        var countiesData = this._getCounty(this.province.find('option:selected').attr('num'), this.city.find('option:selected').attr('num'));
        var countiesOptions = this._createDom(countiesData);
        this.county.html(countiesOptions);
        
        this.callback();
    }
    ,_createDom: function(data) {
        var options = '';
        for(var i=0; i<data.length; i++) {
            var _data = data[i];
            options += '<option num="'+_data.id+'" value="'+_data.name+'">'+_data.name+'</option>';
        }
 
        return options;
    }
    ,_addEvent: function() {
        var _this = this;
        // 切换省调整市和区/县
        this.province.off('change.LinkAge.event').on('change.LinkAge.event', function() {
            var val = $(this).find('option:selected').attr('num');
            console.log(val);
            // 市
            var citiesData = _this._getCities(val);
            var citiesOptions = _this._createDom(citiesData);
            _this.city.html(citiesOptions);
 
 
            // 区/县
            var countiesData = _this._getCounty(val, _this.city.find('option:selected').attr('num'));
            var countiesOptions = _this._createDom(countiesData);
            _this.county.html(countiesOptions);
            
            _this.callback();
        });
 
        // 切换市
        this.city.off('change.LinkAge.event').on('change.LinkAge.event', function() {
            var val = $(this).find('option:selected').attr('num');
            console.log(val);
            // 区/县
            var countiesData = _this._getCounty(_this.province.find('option:selected').attr('num'), val);
            var countiesOptions = _this._createDom(countiesData);
            _this.county.html(countiesOptions);
            _this.callback();
        });
    }
};
 
//layui表格内容更新
var LayuiTbl = function(options, layui, laytpl, cache) {
    this.opts = options;
    this.cache = cache;
    this.layui = layui;
    this.tpl = laytpl;
    this.ele = options.elem;
    this.col = options.cols[0];
};
 
// 设置原型函数
LayuiTbl.prototype = {
    updateTr: function(index, data) {
        var layui = this.layui;
        var preData = this.cache[index];
        var _data = $.extend(preData||{}, data||{});
        var tds = this._getBodyTds(index);
        var tdRs = this._getFixedRTds(index);
        //console.log(_data);
        // 遍历
        var _this = this;
        layui.each(_data, function(key, value) {
            // 遍历tds的值
            tds.each(function(k) {
                var _field = $(this).data('field');
                if(key == _field) {
                    var cell = $(this).find('.layui-table-cell');
                    var content = _this._getContent(key, value, _data);
                    cell.html(content);
                }
            });
        });
        
        // 修改浮动
        layui.each(_data, function(key, value) {
            // 遍历tds的值
            tdRs.each(function(k) {
                var _field = $(this).data('field');
                if(key == _field) {
                    var cell = $(this).find('.layui-table-cell');
                    var content = _this._getContent(key, value, _data);
                    cell.html(content);
                }
            });
        });
    }
    ,updateCol: function(major, field, data) {            // 更新列的值
        // 判断当前属性是否是data的属性
        if(field in data) {
            var index = this.getIndex(major, data[major]);        // 获取需要更新的行
            if(index != undefined) {                            // 获取到
                this.updateTr(index, data);                        // 更新表格的数据
            }
        }
        
    }
    ,update: function(field, data) {
        for(var i=0; i<data.length; i++) {
            var _data = data[i];
            if(field in _data) {                                // 判断对象中是否存在标识位
                var _field = _data[field];                        // 获取标识位的值
                var index = this.getIndex(field, _field);        // 获取对象所在的行
                if(index != undefined) {                        // 判断是否获取到了行号
                    this.updateTr(index, _data);                // 更新指定的行
                }else {
                    console.log(' 第'+i+'个对象没有对象的表格行');
                    console.log(_data);
                }
            }else {
                console.log(' 第'+i+'个对象的'+field+'主键标识不存在');
            }
        }
    }
    ,_getBodyTds: function(index) {
        var layuiTableView = $(this.ele).next('.layui-table-view');
        var layuiTableBody = layuiTableView.find('.layui-table-body .layui-table tbody');
        var trList = layuiTableBody.find('tr');
 
        return trList.eq(index).find('td');
    }
    ,_getFixedRTds: function(index) {
        var layuiTableView = $(this.ele).next('.layui-table-view');
        var layuiFixedR = layuiTableView.find('.layui-table-fixed.layui-table-fixed-r .layui-table tbody');
        var trList = layuiFixedR.find('tr');
        return trList.eq(index).find('td');
    }
    ,_getContent: function(key, value, data) {
        var col = this.col;             //  所有的列
        var laytpl = this.tpl;
        var rs = '';
        var templet = '';
        // 遍历所有的列
        for(var i=0; i<col.length; i++) {
            var _col = col[i];
            // 判断当前内容是否为对应的需要更新的数据
            if(key == _col.field) {
                // 判断是否有模板
                // console.log(key+'***'+_col.field);
                // console.log(_col.templet);
                templet = _col.templet?$(_col.templet).html():_col.toolbar?$(_col.toolbar).html():typeof value == 'number'?value.toString():value;
                //console.log(templet);
                templet = templet?templet:typeof value == 'number'?value.toString():value;
                // 根据模板构成content
                laytpl(templet).render(data, function(html) {
                    rs = html;
                });
 
                return rs;
            }
        }
 
        return false;
    }
    ,getIndex: function(field, value) {        // 获取tr的下标
        // 遍历cache的值
        var cache = this.cache;
        var index = undefined;
        for(var i=0; i<cache.length; i++) {
            var _cache = cache[i];
            // console.log(_cache)
            if(field in _cache && _cache[field] == value) {
                index = i;
                break;
            }
        }
        
        return index;
    }
    ,getTr: function(field, value) {        // 获取Tr的数据
        // 遍历cache的值
        var cache = this.cache;
        var rs = {
            code: 0
            ,data: {}
            ,msg: '获取标识位'+field+',值为'+value+'对应的tr的数据失败'
        };
        // 遍历cache的值
        for(var i=0; i<cache.length; i++) {
            var _cache = cache[i];
            if(field in _cache && _cache[field] == value) {
                rs.code = 1;
                rs.data = _cache;
                rs.msg = '获取标识位'+field+',值为'+value+'对应的tr的数据成功'
                break;
            }
        }
        
        return rs;
    }
    ,setCache: function(cache) {
        this.cache = cache;
    }
};
 
//获取layui的form表单的值(支持input和select)
var LayuiForm = function(ele) {
    this.ele = ele;
};
// 定义原型方法
LayuiForm.prototype = {
    get: function(callback) {
        var ele = $('#'+this.ele);
        var isGood = this._checkForm(ele);
        if(isGood) {
            var iptsVals = this._getIpts(ele);
            var selVals = this._getSels(ele);
            //  获取所有的form值
            var rs = $.extend({}, iptsVals, selVals);
            callback(rs);
        }
    }
    ,_getIpts: function(ele) {
        var ipts = ele.find('input');
        var iptVals = {};
        ipts.each(function() {
            var name = $(this).attr('name');
            if(name) {
                iptVals[name] = $(this).val();
            }
        });
 
        return iptVals;
    }
    ,_getSels: function(ele) {
        var sels = ele.find('select');
        var selVals = {};
        sels.each(function() {
            var name = $(this).attr('name');
            if(name) {
                selVals[name] = $(this).val();
            }
        });
 
        return selVals;
    }
 
    ,_checkForm: function(ele) {
        var isGood = true;
        var ipts = ele.find('input');
        var sels = ele.find('select');
        //  遍历所有的input查询是否有数据不正确的
        ipts.each(function() {
            if($(this).hasClass('layui-form-danger')) {
                isGood = false;
            }
        });
 
        //  遍历所有的select查询是否有数据不正确的
        sels.each(function() {
            if($(this).hasClass('layui-form-danger')) {
                isGood = false;
            }
        });
 
        // 返回结果
        return isGood;
    }
};
 
// 统计数据选中个数/总个数
var QuantNumber = function(ele) {
    this.ele = ele;
    this.mol = 0;
    this.den = 0;
    this._init();
}
// 设置原型方法
QuantNumber.prototype = {
    _init: function() {
        this._setEleTxt();
    }
    ,set: function(mol, den) {
        this.mol = mol;
        this.den = den;
        this._setEleTxt();
    }
    ,setMol: function(val) {
        this.mol = val;
        this._setEleTxt();
    }
    ,setDen: function(val) {
        this.den = val;
        this._setEleTxt();
    }
    ,_setEleTxt: function() {
        var str = this.mol+'/'+this.den;
        this.ele.text(str)
    }
};
 
 
//获取电池品牌列表
function getBattProducers() {
    var producers = [];
    // 请求后台查询电池品牌
    $.ajax({
        type: 'post'
        ,async: false
        ,url: 'BattInfAction!serchByBattProducer'
        ,data: null
        ,dataType: 'json'
        ,success: function(res) {
            var rs = JSON.parse(res.result);
            // 判断查询结果
            if(rs.code == 1) {
                var data = rs.data;
                // 遍历查询结果
                for(var i=0; i<data.length; i++) {
                    var _data = data[i];
                    if(_data.BattProducer) {        // 处理为空的品牌
                        producers.push(_data.BattProducer);
                    }
                }
                producers.push('其他');
            }
        }
        ,complete: function() {
            
        }
    });
    
    return producers;
}
 
// 设置样式
var CarouselStyle = function(container) {
    this.container = container;
    this.contWd = 100;
    this.contHt = 100;
    this.ratio = 1;
    this.init();
    this._resize();
};
// 定义原型函数
CarouselStyle.prototype = {
    init: function() {
        this.contWd = this.container.width();
        this.contHt = this.container.height();
        this.ratio = this._getRatio(this.contWd, this.contHt);
        this._setStyle();
    }
    ,_setStyle() {
        var _this = this;
        var imgs = this.container.find('img');
        console.log(imgs.length);
        imgs.each(function() {
            var size = $(this).data();
            // 判断宽高是否存在
            if(size.width && size.height) {
                var imgRatio = _this._getRatio(size.width, size.height);
                // 容器的比例小于当前图片的比例
                if(_this.ratio<imgRatio) {
                    var mrt = _this._getMrt(_this.contWd, imgRatio);
                    $(this).css({
                        width: '100%'
                        ,height: 'auto'
                        ,'margin-top': mrt+'px'
                    });
                }else {
                    $(this).css({
                        width: 'auto'
                        ,height: '100%'
                    });
                }
            }
        });
    }
    ,_getRatio(w, h) {
        var ratio = 0;
        if(w && h) {
            ratio = Number((w/h).toFixed(2));
        }
 
        return ratio;
    }
    ,_getMrt: function(w, ratio) {
        var halfHt = this.contHt/2;
        var halfImgHt = w/ratio/2;
        var mrt = halfHt -halfImgHt;
 
        return mrt;
    }
    ,_resize: function() {
        var _this = this;
        $(window).resize(function() {
            _this.init();
        });
    }
};
 
//获取当前用户的权限
function getterUserPermits(){
    var _permits = [];
    $.ajax({
        type: 'post',
        async: false,
        url: 'User_permitgroup_dataAction!FromSessiongetPermits',
        data: null,
        dataType: 'json',
        success:function(data) {
            data = JSON.parse(data.result);
            data = JSON.parse(data);
            _permits = data;
        }
    });
    
    return _permits;
}
 
//根据权限名判断是否具有某个权限
function isHasPermit(_permitName,_permitList)
{
    //console.info(_permitList);
    var _isHasPermit=false;
    //遍历_permitList根据_permitName找到该权限是否具有
    for(var i=0;i<_permitList.length;i++)
    {
        //console.info(_permitList[i].permit_item_name);
        //console.info(_permitList[i].permit_item_value);
        if(_permitList[i].permit_item_name == _permitName && _permitList[i].permit_item_value == 1)
        {
            _isHasPermit = true;
            return _isHasPermit;
        }
    }
    return _isHasPermit;
}
 
// 给表格添加的行添加data属性
function addDataToTblTr(container, allData) {
    var tr = container.find("tbody tr");
    var len = allData.length;
    // 遍历tr
    tr.each(function(i){
        if(i<len) {
            $(this).data('data', allData[i]);
        }
    });
}
 
/**
 * 对象依赖layui.form, layui.laytpl, layui.laydate
 */ 
 var OptionsList = function(form, laytpl, laydate) {
     this.list=[];       // 存储条件选择集合
     this.form= form;    // layui的form
     this.laytpl = laytpl; // layui的laytpl
     this.laydate = laydate;    // layui的laydate
     this.formFilter=""; // 条件列表所在容器form的lay-filter
     this.select="";     // 条件列表所在容器的lay-filter
     this.content="";    // 条件详细内容容器所在的DOM
     this.isInit=false;  // 标识是否已经被初始化
 };
 OptionsList.prototype={
     /**
     * 初始化内容
     * @param array list 条件集合
     * @param string formFilter 条件列表所在容器form的lay-filter
     * @param string select 条件列表所在容器的lay-filter(id)
     * @param string content 条件详细内容所在的DOM的id
     */ 
     init: function(list, formFilter, select, content) {
         
         this.list = list;
         this.acList;
         this.formFilter = formFilter;
         this.select = select;
         this.content = content;
 
         // 生成内容
         this._onCreateList();
 
         // 添加绑定事件
         if(!this.isInit) {
             this.isInit=true;       // 设置已经对容器进行了初始化操作
             this._onEvent();        // 给生成内容绑定事件
         }
 
     },
     // 设置下拉框的值
     setItemAttr: function(id, attr, data) {
         var list = this.list;
         for(var i=0; i<list.length;i++) {
             var _list = list[i];
             if(_list.id == id) {
                 _list[attr] = data;
                 
             }
         }
     },
     // 获取列表的值
     getList: function() {
         return this.list;
     },
     // 获取当前选中的选项
     getAcList: function() {
         return this.acList;
     },
     // 根据数据生成列表内容
     _onCreateList: function() {
         // 生成条件列表
         var list = this.list;
         var select = $("#"+this.select);
         // 清空select
         select.text("");
         // 遍历list集合生成下拉内容
         for(var i=0; i<list.length; i++) {
             var _list = list[i];
             var option = $("<option></option>");
             option.val(_list.value);
             option.text(_list.text);
             option.data("data", _list);
             select.append(option);
         }
         // 使用layui进行渲染
         this.form.render("select", this.formFilter);
         this._onCreateContent(list[0]);
     },
     // 根据数据详细信息
     _onCreateContent: function(data) {
         this.acList = data;    // 设置被选中的值
         var _this = this;
         var laytpl = this.laytpl;
         var form = this.form;
         var contentId = this.content;
         // 更具选中的筛选条件生成内容
         var tpl = $('#'+data.id).html();
         if(tpl) {
             //console.log(data);
             laytpl(tpl).render(data, function(html) {
                 $("#"+contentId).html(html);
                 form.render("select", data.filter);
                 // 判断当前是否为时间控件
                 if(data.timeRange) {
                     _this._onCreateTime(data);
                 }
             });
         }else {
             $("#"+contentId).html("");
             console.log("没有找到#"+data.id+"模板");
         }
     },
     _onCreateTime:function(data) {
         var laydate = this.laydate;
         switch(data.type) {
             case "month":
                 laydate.render({
                     elem: '#'+data.timeId
                     ,type: "month"
                     ,range: true
                 });
                 break;
             default:
                 laydate.render({
                     elem: '#'+data.timeId
                     ,range: true
                 });
                 break;
         }
     },
     // 添加绑定事件
     _onEvent: function() {
         var _this = this;
         var laytpl = this.laytpl;
         var form = this.form;
         var contentId = this.content;
         // 给条件列表下拉框添加事件
         this.form.on("select("+this.select+")", function(data) {
             var content = _this.content;
             var ele = $(data.elem);
             var data = ele.find("option:selected").data("data");
             _this._onCreateContent(data);
         });
     }
 };
 
 /**
  * 自定义日期
  * @author hdw
  */
 var HdwDate = function() {};
 
 /**
  * 设置MyDate的原型函数
  */
 HdwDate.prototype = {
    /**
     * 获取输入的年份是闰年还是普通年份
     * @return 返回是否为闰年
     */
    isLeapYear: function(year) {
        var isLeap = false;
        // 输入的年份能够被4整除
        if(year%4==0) {
            // 输入的年份不能被100整除
            if(year%100 != 0) {
                isLeap = true;
            }else if(year%400 == 0){
                isLeap = true;
            }
        }
        
        return isLeap;
    },
    /**
     * 根据字符串yyyy-MM获取当前月有几天
     * @return 返回当前月有多少天
     */
    getMonthDay: function(date) {
        var dates = date.split("-");
        var year = Number(dates[0]);
        var month = Number(dates[1]);
        var day = 30;
        // 判断是否为2月
        switch(month) {
            case 2:
                var isLead = this.isLeapYear(year);
                day = isLead?29:28;
                break;
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            default:
                day = 30;
                break;
        }
        return day;
    }
    
 };
 // 实例化对象
 var hdwDate = new HdwDate();
 
 /**
  *    根据配置生成请求WebSocket的方法
  * @param opts
  * @param opts.ws 地址
  * @param ops.data 请求后台的参数
  * @param opts.message 返回结果函数
  * @param opts.open opts.error opts.close 回到函数
  */
  var MyWebSocket = function(opts) {
      this.opts = opts;
      this.websocket;
      this._init();
  };
  MyWebSocket.prototype={
      _init: function() {
          var opts = this.opts;
            var ws = opts.ws;
            //判断当前浏览器是否支持WebSocket
            if ('WebSocket' in window) {
                this.websocket = new WebSocket(ws);
            }
            else {
                alert('当前浏览器 Not support websocket')
            }
            
            var msg = this._checkCallback(this.opts.message);
            var onopen = this._checkCallback(this.opts.open);
            var error = this._checkCallback(this.opts.error);
            var close = this._checkCallback(this.opts.close);
            var data = this.opts.data;
            this._setWebSocket(this.websocket, msg, open, error, close, data);
            
            //监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
            window.onbeforeunload = function () {
                this.websocket.close();
            }
      },
      _setWebSocket: function(websocket, msg, open, error, close, data) {
          
              //连接发生错误的回调方法
            websocket.onerror = function () {
                error();
            };
        
            //连接成功建立的回调方法
            websocket.onopen = function () {
                websocket.send(data);
            }
        
            //接收到消息的回调方法
            websocket.onmessage = function (event) {
                msg(event.data);
            }
        
            //连接关闭的回调方法
            websocket.onclose = function () {
                close();
            }
      },
      _checkCallback: function(callback) {
          var rs =function() {};
          if(typeof callback == "function") {
              rs = callback;
          }
          return rs;
      }
  };
 
// 向layui的下拉框中添加清空按钮
function ClearSelectVal(sel) {
    this.sel = sel;
}
ClearSelectVal.prototype.init = function() {
    var self = this;
    var sel = this.sel.next('.layui-form-select');
    var $input = sel.find('.layui-input');
    var selTitle = sel.find('.layui-select-title');
    var clear = selTitle.find('.my-layui-clear');
    var $clear = $('<i class="fa fa-close my-layui-clear"></i>');
    if(clear.length == 0) {
        selTitle.append($clear);
    }
    $clear.click(function() {
        $input.click();
        $input.focus();
        $input.val('');
    });
    
};