Helius
2021-06-16 5728be2af515b2200e782aa201ca5d4d67d9ea47
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
/*一些基础的js方法,基础的业务js方法在lib.js里*/
//日期格式化
Date.prototype.format=function(format){var d=this,o={"M+":d.getMonth()+1,"d+":d.getDate(),"H+":d.getHours(),"m+":d.getMinutes(),"s+":d.getSeconds(),w:["日","一","二","三","四","五","六"][d.getDay()]};if(/(y+)/.test(format)){format=format.replace(RegExp.$1,(d.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 Common = {
    ctxPath: "",
    version:"",
    log: function (info) {
        console.log(info);
    },
    alert: function (info, iconIndex) {
        parent.layer.msg(info, {
            icon: iconIndex
        });
        
        
    },
    info: function (info) {
        Common.alert(info, 0);
    },
    success: function (info) {
        Common.alert(info, 1);
    },
    error: function (info) {
            Common.openConfirm(info)
    },
    post: function (url, paras, next) {
            $.ajax({
                url:Common.ctxPath+url,
                type:"POST",
                data:paras,
                success:function(rsp){
                    if(rsp.code!=0){
                        Common.error(rsp.msg);
                        
                    }else{
                        //成功
                        if(next!=null){
                            next(rsp.data);
                        }else{
                            Common.success(rsp.responseJSON.msg);
                        }
                    }
                    
                },
                error:function(rsp){
                    Common.error(rsp.responseJSON.msg);
                }
            })
       
    },
    getOneFromTable:function(layuiTable,tableId){
            var checkStatus = layuiTable.checkStatus(tableId)
        ,data = checkStatus.data;
        if(data.length==0){
                Common.info("请选中一条记录");
        }else if(data.length>1){
                Common.info("只能选中一条记录")
        }else{
                return  data[0];
        }
    },
    getMoreDataFromTable:function(layuiTable,tableId){
        var checkStatus = layuiTable.checkStatus(tableId)
        ,data = checkStatus.data;
        if(data.length==0){
                Common.info("请选中记录");
        }else{
                return  data;
        }
    },
    openDlg:function(url,title){
            var index = layer.open({  
            type: 2,  
            content: Common.ctxPath+url,  
            title: title,  
            maxmin: false
        });  
        layer.full(index);  
    },
    topOpenDlg:function(url,title){
                           var index = top.layer.open({
                           type: 2,
                           content: Common.ctxPath+url,
                           title: title,
                           area:['100%', '100%'],
                           maxmin: false
                       });
                       layer.full(index);
                   },
    openConfirm:function(content,callback,callBackNo){
            var index = layer.confirm(content, {
              btn: ['确认','取消'] //按钮
            }, function(){
                if(callback!=null){
                    callback();
                }
                layer.close(index);
            }, function(){
                if(callBackNo!=null){
                    callBackNo()
                }
                layer.close(index);
            });
        
    },
    openPrompt:function(title,defaultValue,callback){
            layer.prompt({title: title, formType: 0,value:defaultValue}, function(value, index,elem){
              layer.close(index);
              callback(value);
            });
    },
    concatBatchId:function(data,idField){
            var ids = ""
            var name=idField==null?"id":idField;
            for(var i=0;i<data.length;i++){
                var item = data[i];
                ids=ids+item[name];
                if(i!=data.length-1){
                    ids=ids+","
                }
            }
            return ids;
    },
    sessionTimeoutRegistry: function () {
        $.ajaxSetup({
            contentType: "application/x-www-form-urlencoded;charset=utf-8",
            complete: function (XMLHttpRequest, textStatus) {
                //通过XMLHttpRequest取得响应头,sessionstatus,
                var sessionstatus = XMLHttpRequest.getResponseHeader("sessionstatus");
                if (sessionstatus == "timeout") {
                    //如果超时就处理 ,指定要跳转的页面
                    window.location = Common.ctxPath + "/global/sessionError";
                }
            }
        });
    },
    initValidator: function(formId,fields){
        $('#' + formId).bootstrapValidator({
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: fields,
            live: 'enabled',
            message: '该字段不能为空'
        });
    },
    getDate:function(date,pattern){
            if(date==null||date==''){
                return "";
            }else{
                if(pattern){
                    return new Date(date).format(pattern);
                }else{
                    return date.substring(0,10);
                }
                
            }
    }
    
};
 
 
 
// JQuery方法定制
(function($){
    
    
    $.ajaxSetup({
      type: 'POST',
      async: true,
      dataType : "json",
      timeout : 30000 
     });
    
    /**
     * 获取form表单数据
     */
    $.fn.getFormData = function (isValid) {
      var fieldElem = $(this).find('input,select,textarea'); //获取所有表单域
      var data ={};
      layui.each(fieldElem, function(index, item){
      if(!item.name) return;
      if(/^checkbox|radio$/.test(item.type) && !item.checked) return;
      var value = item.value;
      if(item.type == "checkbox"){//如果多选
          if(data[item.name]){
              value = data[item.name] + "," + value;
          }
      }
      if(isValid)
      {
         //如果为true,只需要处理有数据的值
         if(!$.isEmpty(value))
       {
             data[item.name] = value;
       }
      }
      else
      {
          data[item.name] = value;
      }
    });
    return data;
  };
  
  $.fn.serializeJson = function() {
      var serializeObj = {};
      var array = this.serializeArray();
      var str = this.serialize();
      $(array).each(
              function() {
                  if (serializeObj[this.name]) {
                      if ($.isArray(serializeObj[this.name])) {
                          serializeObj[this.name].push(this.value);
                      } else {
                          serializeObj[this.name] = [
                                  serializeObj[this.name], this.value ];
                      }
                  } else {
                      serializeObj[this.name] = this.value;
                  }
              });
      return serializeObj;
  };
 
  
  $.extend({
      //非空判断
      isEmpty: function(value) {
          if (value === null || value == undefined || value === '') { 
              return true;
          }
          return false;
    },
    //获取对象指
    result: function(object, path, defaultValue) {
        var value = "";
          if(!$.isEmpty(object) && $.isObject(object) && !$.isEmpty(path)){
              var paths = path.split('.');
              var length = paths.length;
              $.each(paths,function(i,v){
                  object = object[v];
                  if(length-1 == i){
                        value = object;
                    }
                  if(!$.isObject(object)){
                      return false;
                  }
              })
              
          }
          
          if($.isEmpty(value) && !$.isEmpty(defaultValue)){
              value = defaultValue;
          }
          return value;
    },
    //判断是否obj对象
    isObject : function(value) {
      var type = typeof value;
      return value != null && (type == 'object' || type == 'function');
    },
    //是否以某个字符开头
    startsWith : function(value,target){
        return value.indexOf(target) == 0;
    },
    //设置sessionStorage
    setSessionStorage:function(key, data){
        sessionStorage.setItem(key, data);
    },
    //获取sessionStorage
    getSessionStorage:function(key){
        return sessionStorage.getItem(key) == null ? "" : sessionStorage.getItem(key);
    },
    //删除sessionStorage
    removeSessionStorage:function(key){
        sessionStorage.removeItem(key);
    },
    //清除sessionStorage
    clearSessionStorage:function(){
        sessionStorage.clear();
    },
    uuid : function(){
          return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
            var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
            return v.toString(16);
          });
    }
  });
 
}(jQuery));