/**
封装常用扩展
**/
layui.define(['admin', 'layer', 'form'], function (exports) { //提示:模块也可以依赖其它模块,如:layui.define('layer', callback);
var $ = layui.jquery,
layer = layui.layer,
admin = layui.admin,
setter = layui.setter;
var obj = {
log: function (str) {
console.log('coreHelper执行:' + str);
},
Post: function (url, data, success, cache, alone) {
coreAjax(url, 'post', 'json', data, success, false, cache, alone, false, false);
},
PostForm: function (url, data, success, cache, alone) {
coreAjax(url, 'post', 'json', data, success, false, cache, alone, false, true);
},
Get: function (url, success, cache, alone) {
coreAjax(url, 'get', 'json', {}, success, false, alone, false, false);
},
jsonp: function (url, success, cache, alone) {
coreAjax(url, 'get', 'jsonp', {}, success, false, cache, alone, false, false);
},
viewImage: function (imgUrl) {
layer.open({
type: 1,
title: false,
closeBtn: 0,
scrollbar: false,
skin: 'layui-layer-nobg', //没有背景色
shadeClose: true,
content: "
"
});
},
//转数组
stringToArray: function (str) {
var arr = [];
if (str.indexOf(',') != -1) {
arr = str.split(',');
} else {
arr.push(str);
}
return arr;
},
//获取数组长度
stringToArrayLength: function (str) {
var arr = [];
if (str.indexOf(',') != -1) {
arr = str.split(',');
} else {
arr.push(str);
}
return arr.length;
},
//获取随机编码
getSN: function (str) {
//声明一个随机数变量,默认为1
var GetRandomn = 1;
//js生成时间戳
var timestamp = new Date().getTime();
//获取随机范围内数值的函数
function GetRandom(n) {
//由随机数+时间戳+1组成
GetRandomn = Math.floor(Math.random() * n + timestamp + 1);
}
//开始调用,获得一个1-100的随机数
GetRandom("30");
return str + GetRandomn;
},
//获取带色label
getLabelStyle: function (str) {
var outStr = "";
switch (EXPR) {
case 'red':
outStr = "";
break;
case 'green':
outStr = "layui-bg-green";
break;
case 'orange':
outStr = "layui-bg-orange";
break;
case 'blue':
outStr = "layui-bg-blue";
break;
default:
outStr = "";
}
return outStr;
},
//时间格式化
getFullTime: function (date) {
let Y = date.getFullYear() + '',
M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1),
D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()),
h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()),
m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()),
s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s;
},
//保存和刷新token
saveRefreshtime: function () {
let nowtime = new Date();
let lastRefreshtime = window.localStorage.refreshtime ? new Date(window.localStorage.refreshtime) : new Date(-1);
let expiretime = new Date(Date.parse(window.localStorage.TokenExpire));
let refreshCount = 1;//滑动系数
if (lastRefreshtime >= nowtime) {
lastRefreshtime = nowtime > expiretime ? nowtime : expiretime;
lastRefreshtime.setMinutes(lastRefreshtime.getMinutes() + refreshCount);
window.localStorage.refreshtime = lastRefreshtime;
} else {
window.localStorage.refreshtime = new Date(-1);
}
},
//获取浏览器get参数
getQueryValue: function () {
var url = location.href; //获取url中"?"符后的字串
console.log(url);
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
}
}
return theRequest;
},
getLabel: function (labels) {
var html = '';
if (labels && labels != null && typeof labels != 'undefined') {
var label_style = '';
if (typeof labels == 'object') {
$.each(labels, function (i, obj) {
label_style = '';
switch (obj.style) {
case 'red':
label_style = "";
break;
case 'green':
label_style = "layui-bg-green";
break;
case 'orange':
label_style = "layui-bg-orange";
break;
case 'blue':
label_style = "layui-bg-blue";
break;
default:
label_style = '';
}
html += '' + obj.name + ' ';
});
}
}
return html;
},
//清除富文本内容自定义参数
formatRichText: function (html) {
let newContent = html.replace(/
]*>/gi, function (match, capture) {
match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
return match;
});
newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
return match;
});
newContent = newContent.replace(/
]*\/>/gi, '');
newContent = newContent.replace(/\![]()