scui
Version:
smart campus ui 2.0
101 lines (93 loc) • 2.97 kB
JavaScript
;
let getCheck = {
checkEmail(val) {
var filter = /^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (filter.test(val)) {
return true;
} else {
return false;
}
},
checkPhone(val) {
var filter = /^1\d{10}$/;
if (filter.test(val)) {
return true;
} else {
return false;
}
}
};
/**
* 对Date的扩展,将 Date 转化为指定格式的String
* 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
* 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
* 例子:
* (new Date()).Format('yyyy-MM-dd hh:mm:ss.S') ==> 2006-07-02 08:09:04.423
* (new Date()).Format('yyyy-M-d h:m:s.S') ==> 2006-7-2 8:9:4.18
*/
const fmtDate = (date, fmt) => { // author: meizz
var o = {
'M+': date.getMonth() + 1, // 月份
'd+': date.getDate(), // 日
'h+': date.getHours(), // 小时
'm+': date.getMinutes(), // 分
's+': date.getSeconds(), // 秒
'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
'S': date.getMilliseconds() // 毫秒
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp('(' + k + ')').test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
}
}
return fmt;
};
/**
* 格式化日期或时间
* @param {string} time 需要格式化的时间
* @param {bool} friendly 是否是fromNow
*/
exports.getLastTimeStr = (time, friendly) => {
if (friendly) {
return MillisecondToDate(time);
} else {
return fmtDate(new Date(time), 'yyyy-MM-dd hh:mm');
}
};
/**
* 调用Timeago库显示到现在的时间 临时测试
*/
const MillisecondToDate = (time) => {
return time;
};
/**
* 配置节流函数
* @param {[Function]} fn [要执行的函数]
* @param {[Number]} delay [延迟执行的毫秒数]
* @param {[Number]} mustRun [至少多久执行一次]
* @return {[Function]} [节流函数]
*/
exports.throttle = (fn, wait, mustRun) => {
let timeout;
let startTime = new Date();
return function() {
let context = this;
let args = arguments;
let curTime = new Date();
clearTimeout(timeout);
// 如果达到了规定的触发时间间隔,触发 handler
if (curTime - startTime >= mustRun) {
fn.apply(context, args);
startTime = curTime;
// 没达到触发间隔,重新设定定时器
} else {
timeout = setTimeout(fn, wait);
}
};
};
exports.getCheck = getCheck;
exports.fmtDate = fmtDate;
exports.MillisecondToDate = MillisecondToDate;