//请尊重开发者劳动成果，勿将本程序发布到网上或倒卖，感谢您的支持！程序唯一联系QQ:6777107 如有违规内容发布，请自行删除！
layui.define(['layer'], function(exports) {
var $ = layui.jquery,
layer = layui.layer,
_modName = 'utils';
var utils = {
v: '1.0.3',
getBodyContent: function(content) {
var REG_BODY = /<body[^>]*>([\s\S]*)<\/body>/,
result = REG_BODY.exec(content);
if (result && result.length === 2)
return result[1];
return content;
},
loadHtml: function(url, callback) {
var result;
$.ajax({
url: url,
async: false,
dataType: 'html',
beforeSend: function(request) {
request.setRequestHeader("IsGetHtml", 'true');
},
error: function(xhr, err, msg) {
var m = ['<div style="padding: 20px;font-size: 20px;text-align:left;color:#009688;">',
'<p>{{msg}}  >>> ' + url + '</p>',
'</div>'
].join('');
if (xhr.status === 404) {
result = m.replace('{{msg}}', '<i class="layui-icon" style="font-size:70px;">&#xe61c;</i>  ' + msg);
return;
}
result = m.replace('{{msg}}', '<i class="layui-icon" style="font-size:70px;">&#xe69c;</i>  未知错误.');
},
success: function(res) {
result = res;
},
complete: function() {
typeof callback === 'function' && callback();
}
});
return result;
},
load: function(type) {
type = type || 0;
return layer.load(type, { shade: [0.3, '#333'] });
},
post: function(url, params, callback, options) {
var defaults = {
error: function(xhr, err, msg) {
console.log('发生错误了..');
console.log(xhr);
console.log(err);
console.log(msg);
},
complete: function(xhr, statusCode) {
console.log('complete.' + statusCode);
}
};
$.extend(true, defaults, options);
$.ajax({
url: url,
beforeSend: function(request) {
request.setRequestHeader("IsAjax", 'true');
},
dataType: 'json',
method: 'POST',
data: params,
error: function(xhr, err, msg) {
defaults.error(xhr, err, msg);
},
success: function(res) {
if (res.code === 101) {
layer.alert(res.msg, {
icon: 2,
title: '系统提示',
cancel: function(index, layero) {
top.location.href = location.origin + '/Account/Login';
}
}, function() {
top.location.href = location.origin + '/Account/Login';
});
} else {
callback(res);
}
},
complete: function(xhr, statusCode) {
defaults.complete(xhr, statusCode);
}
});
},
get: function(url, params, callback, options) {
var defaults = {
error: function(xhr, err, msg) {
console.log('发生错误了..');
console.log(xhr);
console.log(err);
console.log(msg);
},
complete: function(xhr, statusCode) {
console.log('complete.' + statusCode);
}
};
$.extend(true, defaults, options);
$.ajax({
url: url,
beforeSend: function(request) {
request.setRequestHeader("IsAjax", 'true');
},
dataType: 'json',
method: 'GET',
data: params,
error: function(xhr, err, msg) {
defaults.error(xhr, err, msg);
},
success: function(res) {
if (res.code === 101) {
layer.alert(res.msg, {
icon: 2,
title: '系统提示',
cancel: function(index, layero) {
top.location.href = location.origin + '/Account/Login';
}
}, function() {
top.location.href = location.origin + '/Account/Login';
});
} else {
callback(res);
}
},
complete: function(xhr, statusCode) {
defaults.complete(xhr, statusCode);
}
});
},
keyWordHighlight: function(o, flag, rndColor, url) {
var bgCor = fgCor = '';
if (rndColor) {
bgCor = fRndCor(10, 20);
fgCor = fRndCor(230, 255);
} else {
bgCor = 'transparent'; //背景色
fgCor = 'red'; //字体颜色
}
var re = new RegExp(flag, 'i');
for (var i = 0; i < o.childNodes.length; i++) {
var o_ = o.childNodes[i];
var o_p = o_.parentNode;
if (o_.nodeType == 1) {
this.keyWordHighlight(o_, flag, rndColor, url);
} else if (o_.nodeType == 3) {
if (!(o_p.nodeName == 'A')) {
if (o_.data.search(re) == -1) continue;
var temp = fEleA(o_.data, flag);
o_p.replaceChild(temp, o_);
}
}
}
function fEleA(text, flag) {
var style = ' style="background-color:' + bgCor + ';color:' + fgCor + ';" '
var o = document.createElement('span');
var str = '';
var re = new RegExp('(' + flag + ')', 'gi');
if (url) {
str = text.replace(re, '<a href="' + url +
'$1"' + style + '>$1</a>'); 
} else {
str = text.replace(re, '<span ' + style + '>$1</span>'); 
}
o.innerHTML = str;
return o;
}
function fRndCor(under, over) {
if (arguments.length == 1) {
var over = under;
under = 0;
} else if (arguments.length == 0) {
var under = 0;
var over = 255;
}
var r = fRandomBy(under, over).toString(16);
r = padNum(r, r, 2);
var g = fRandomBy(under, over).toString(16);
g = padNum(g, g, 2);
var b = fRandomBy(under, over).toString(16);
b = padNum(b, b, 2);
return '#' + r + g + b;
function fRandomBy(under, over) {
switch (arguments.length) {
case 1:
return parseInt(Math.random() * under + 1);
case 2:
return parseInt(Math.random() * (over - under + 1) + under);
default:
return 0;
}
}
function padNum(str, num, len) {
var temp = ''
for (var i = 0; i < len; temp += num, i++);
return temp = (temp += str).substr(temp.length - len);
}
}
}
};
exports('utils', utils);
});