js 常用工具类
/**
* 存储sessionStorage
*/
const setStore = (name, content) => {
window.sessionStorage.setItem(name, content);
}
/**
* 获取localStorage
*/
const getStore = (params) => {
let name = params;
let content = window.sessionStorage.getItem(name);
return content;
}
/**
* 删除localStorage
*/
const removeStore = params => {
window.sessionStorage.removeItem(params);
}
/**
* @param {Number} timeStamp 传入的时间戳
* @param {Number} startType 要返回的时间字符串的格式类型
* @description 获取当前日期/时间/星期
*/
const getDate = (startType) => {
const d = new Date()
const year = d.getFullYear()
const month = getHandledValue(d.getMonth() + 1)
const date = getHandledValue(d.getDate())
const hours = getHandledValue(d.getHours())
const minutes = getHandledValue(d.getMinutes())
const second = getHandledValue(d.getSeconds())
let resStr = ''
if (startType === 'date') {
resStr = year + '-' + month + '-' + date
}else if(startType === 'time'){
resStr = hours + ':' + minutes + ':' + second
}else if(startType === 'week'){
resStr= '星期'+'日一二三四五六'.charAt(new Date().getDay());
}else{
resStr = month + '-' + date + ' ' + hours + ':' + minutes
}
return resStr
}
/**
* @param {Number} num 数值
* @returns {String} 处理后的字符串
* @description 如果传入的数值小于10,即位数只有1位,则在前面补充0
*/
const getHandledValue = num => {
return num < 10 ? '0' + num : num
}
/**
* 判断是否为空
*/
function validatenull(val) {
if (typeof val == 'boolean') {
return false;
}
if (typeof val == 'number') {
return false;
}
if (val instanceof Array) {
if (val.length == 0) return true;
} else if (val instanceof Object) {
if (JSON.stringify(val) === '{}') return true;
} else {
if (val == 'null' || val == null || val == 'undefined' || val == undefined || val == '') return true;
return false;
}
return false;
}