common.js 7.28 KB
   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
function timeoutThread(fn,timeout){
var nfn=fn;
window.setTimeout(nfn ,timeout || 20);
}

function msg(content){
layer.msg(content);
}

function offsetMsg(content,style,offset){
if(!offset){
offset = 'rb';
}
if(!style){
style = 'width:100%;height:100%;color:#fff;line-height:150px;font-size:18px;background-color:#0073d5;text-align:center;';
}
layer.open({
type: 1,
title: false,
closeBtn: 0,
shade: [0],
shadeClose : true,
area: ['300px','150px'],
offset: offset,
time: 2000, //2秒后自动关闭
shift: 2,
content: '<div style="' + style + '" >' +content + '</div>'
});
}
//content可以是dom,string
function pop(title,content,maxmin){
if(!maxmin) maxmin = true;
layer.open({
type: 1,
title: title,
content: content,
maxmin: maxmin
});
}
function pop(title,content,width,height,maxmin){
layer.open({
type: 1,
title: title,
area: [width + 'px', height + 'px'],
content: content,
maxmin : true
});
}

//title:false不显示,&nbsp;空title
function popWindow(title,url,width,height){
layer.open({
type: 2,
title: title,
maxmin: true,
area : [width+"px" , height+"px"],
content: url
});
}
//全屏window
function fullWindow(title,url,maxmin){
var index = layer.open({
type: 2,
title: title,
content: url,
maxmin: maxmin
});
layer.full(index);
}

//关闭所有layer
function closeAll(){
layer.closeAll() ;
}




//ajax,从formid获取数据后提交数据
function ajaxForm(url,formid,successfn,dataType){
if(!dataType)dataType = "json";
var loadIndex = layer.load(0,{shade:0.3});
$.ajax({
type: "post",
data: $('#' + formid).serialize(),
url: url,
dataType: dataType,
success: function(r){
layer.close(loadIndex);
if(successfn){
successfn(r);
}else{
msg(r.message);
}
},
error: function(e){
layer.close(loadIndex);
msg("操作数据发生异常,请稍后再试或联系管理员");
}
});
}
//ajaxPost
function ajaxPost(url, data, successfn,dataType) {
console.log(data);
data = (data==null || data=="" || typeof(data)=="undefined")? {"clientDate": new Date().getTime()} : data;
if(!dataType)dataType = "json";
var loadIndex = layer.load(0,{shade:0.3});
$.ajax({
type: "post",
data: data,
url: url,
dataType: dataType,
success: function(r){
layer.close(loadIndex);
if(successfn){
successfn(r);
}else{
msg(r.message);
}
},
error: function(e){
layer.close(loadIndex);
msg("操作数据发生异常,请稍后再试或联系管理员");
}
});
}

//刷新grid
function reloadGrid(gridId,timeout){
timeout = timeout || 400;
timeoutThread(reloadGrid_inner,timeout);
function reloadGrid_inner(){
$('#'+gridId).bootstrapTable('refresh');
}
}
//根据datatable选中的数据获取field的值进行删除
function goDelete(gridId,field,url,callback){
var val = getSelectedValue(gridId,field);
if(val){
layer.confirm('确定删除选中的数据', {
btn: ['确定','取消'] //按钮
}, function(){
if(url.indexOf("@value@")){
url = url.replace("@value@",val)
}
if(callback)
{
ajaxPost(url,null,callback);
}else{
ajaxPost(url,null,function(r){
if(r.success){
reloadGrid(gridId);
}
msg(r.message);
});
}
}, function(){
});
}else{
msg("请选择需要删除的数据");
}
}
//获取datatable选中单行的指定field的值
function getSingleSelectedValue(gridId,field){
var selects = $('#'+gridId).bootstrapTable('getSelections');
if( selects && selects.length == 1 ){
return selects[0][field];
}else if(selects.length > 0 ){
msg("只能选择单条数据");
return null;
}else{
msg("请选中数据");
return null;
}
}
//获取datatable选中多行的指定field的值
function getSelectedValue(gridId,field){
var selects = $('#'+gridId).bootstrapTable('getSelections');
if( selects && selects.length > 0 ){
var values = [];
for (var i = 0; i < selects.length; i++) {
var cellValue = selects[i][field];
if(cellValue && (""+cellValue).length > 0){
values.push(cellValue);
}
}
return values.join(',');
}else{
return null;
}
}

//获取datatable单选行的数据
function getSingleSelectedRow(gridId){
var rows = $('#'+gridId).bootstrapTable.bootstrapTable('getSelections');
var num = rows.length;
if (num == 1) {
return rows[0];
} else {
return null;
}
}

//数据表格启用状态格式化
function enableFormatter(value,row,index){
if(value == 1){
return '<i class="fa fa-check-square-o" aria-hidden="true"></i>'
}else{
return '<i class="fa fa-remove" aria-hidden="true"></i>';
}
}

//自定义高精度浮点数运算
//对象格式写法
//float_caculator.add(1.11444,23.45674231);
var float_calculator={
/**
* 1.记录两个运算数小数点后的位数
* 2.将其转化为整数类型进行运算
* 3.移动小数点的位置
**/
//加
add:function(arg1,arg2){
var r1,r2,m;
try{
//取小数位长度
r1=arg1.toString().split(".")[1].length;
r2=arg2.toString().split(".")[1].length;
}catch(e){
r1=0;r2=0;
}
m=Math.pow(10,Math.max(r1,r2)); //计算因子

return (arg1*m+arg2*m)/m;
},
//减
minus:function(arg1,arg2){
return this.add(arg1,-arg2);
},
//乘
mul:function(arg1,arg2){
var r1,r2,m;
try{
//取小数位长度
r1=arg1.toString().split(".")[1].length;
r2=arg2.toString().split(".")[1].length;
}catch(e){
r1=0;r2=0;
}
m=Math.pow(10,Math.max(r1,r2)); //计算因子

return (arg1*m)*(arg2*m)/(m*m);
},
//除
div:function(arg1,arg2){
if( arg2 == 0 ) return 0;
var t1=0,t2=0,r1,r2;
try{
t1=arg1.toString().split(".")[1].length;
t2=arg2.toString().split(".")[1].length;
}catch(e){}

with(Math){
r1=Number(arg1.toString().replace(".",""));
r2=Number(arg2.toString().replace(".",""));
}

return (r1/r2)*Math.pow(10,t2-t1);
}
};
function Scroll(w) {
var win = w || window;
var d = win.document;
if (d.documentElement && !(d.documentElement.scrollTop == undefined || d.documentElement.scrollTop == 0)) {
return {
'top': win.document.documentElement.scrollTop,
'left': win.document.documentElement.scrollLeft
};
} else if (win.document.body) {
return {
'top': win.document.body.scrollTop,
'left': win.document.body.scrollLeft
};
}
}
function BoundingClientRect(obj) {
if (navigator.product == 'Gecko') {
var objWin = null;
var top = obj.offsetTop;
var left = obj.offsetLeft;
var right = obj.offsetWidth;
var bottom = obj.offsetHeight;
while (obj = obj.offsetParent) {
top += obj.offsetTop;
left += obj.offsetLeft;
if (obj.tagName.toLowerCase() == 'body') {
objWin = obj.ownerDocument.defaultView;
}
}
var theScroll = Scroll(objWin);
left -= theScroll.left;
top -= theScroll.top;
right += left;
bottom += top;
return {
'left': left,
'top': top,
'right': right,
'bottom': bottom
};
} else {
return obj.getBoundingClientRect();
}
}