You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.3 KiB
82 lines
2.3 KiB
/*
|
|
全局共享实用方法 shared.js
|
|
*/
|
|
|
|
// 判断对错/是否显示,万能校验
|
|
export function isRight(obj) {
|
|
if (isValueType(obj) === 'string') {
|
|
obj = obj.trim();
|
|
if (obj === 'null' || obj === 'undefined') {
|
|
return false;
|
|
}
|
|
} else if (isValueType(obj) === 'number' && (isValueType(obj) === "number" && !isNaN(obj)) && obj !== 0) {
|
|
return true;
|
|
} else if (isValueType(obj) === 'boolean') {
|
|
return obj
|
|
}
|
|
for (var key in obj) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// 判断一个值所属的类型,返回一个字符串
|
|
export function isValueType(value) {
|
|
let str = Object.prototype.toString.call(value);
|
|
return str.match(/\[object (.*?)\]/)[1].toLowerCase();
|
|
}
|
|
|
|
// 解析时间戳,参数非必传,不传参时默认显示当前最新日期+时间;
|
|
// 第一个参数为当前日期时间戳,第二个日期分隔符,第三个不传参或传all显示日期+时间,传date显示日期,time显示时间
|
|
export function recordTime(time = new Date(), separator = "-", swf = 'all'){
|
|
let year = time.getFullYear();
|
|
let month = time.getMonth() + 1;
|
|
let day = time.getDate();
|
|
let hour = time.getHours();
|
|
let min = time.getMinutes();
|
|
let ppn = time.getSeconds();
|
|
if(swf === "time"){
|
|
return [hour, min, ppn].map(cover).join(":");
|
|
}else if(swf === "date"){
|
|
return [year, month, day].map(cover).join(String(separator));
|
|
}else{
|
|
return [year, month, day].map(cover).join(String(separator)) +" "+ [hour, min, ppn].map(cover).join(":");
|
|
}
|
|
}
|
|
|
|
export const timer = (value, fmt) => {
|
|
if(!value) return;
|
|
let newTime = new Date(value)
|
|
if(!fmt){
|
|
fmt = 'yyyy-MM-dd hh:mm';
|
|
}
|
|
if(/(y+)/.test(fmt)) {
|
|
fmt = fmt.replace(RegExp.$1, (newTime.getFullYear() + '').substr(4 - RegExp.$1.length));
|
|
}
|
|
let o = {
|
|
'M+': newTime.getMonth() + 1,
|
|
'd+': newTime.getDate(),
|
|
'h+': newTime.getHours(),
|
|
'm+': newTime.getMinutes(),
|
|
's+': newTime.getSeconds()
|
|
};
|
|
|
|
function padLeftZero(str) {
|
|
return ('00'+str).substr(str.length);
|
|
}
|
|
|
|
// 遍历这个对象
|
|
for (let k in o) {
|
|
if (new RegExp(`(${k})`).test(fmt)) {
|
|
// console.log(`${k}`)
|
|
let str = o[k] + '';
|
|
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
|
|
}
|
|
}
|
|
return fmt;
|
|
}
|
|
// 位数不足2,前面补0
|
|
var cover = function(par) {
|
|
par = par.toString()[1] ? par : "0" + par;
|
|
return par;
|
|
};
|