1、颜色16进制转raba
/**
* @description 16进制转换rgba
* @param {*} str 16进制颜色 #e6a23c
* @return {*} rgba 0.15
*/
function set16ToRgba(str, alpha = 1) {
// 十六进制颜色值的正则表达式
var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{4}|[0-9a-fA-f]{6}|[0-9a-fA-f]{8})$/
/* 16进制颜色转为RGB格式 */
var sColor = str.toLowerCase();
if (sColor && reg.test(sColor)) {
if (sColor.length === 4 || sColor.length === 5) {
var sColorNew = '#';
for (var i = 1; i < sColor.length; i += 1) {
sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
}
sColor = sColorNew;
}
// 如果有透明度再执行
if (sColor.length === 9) {
alpha = (parseInt('0x' + sColor.slice(7, 9)) / 255).toFixed(2);
}
// 处理六位的颜色值
var sColorChange = [];
for (var k = 1; k < 7; k += 2) {
sColorChange.push(parseInt('0x' + sColor.slice(k, k + 2)));
}
return 'rgba(' + sColorChange.join(',') + ',' + alpha + ')';
} else {
return sColor;
}
}
2、格式化日期
/**
* @description 格式化日期
* @param {*} [value=Date.now()] {string | number | Date} value 指定日期
* @param {string} [format="Y-M-D H:m:s"]
* @example formatDate(); formatDate(1603264465956); formatDate(1603264465956, "H:m:s"); formatDate(1603264465956, "Y年M月D日");
* @return {*} 'yyyy-MM-dd HH:mm:ss'
*/
formatDate(value = Date.now(), format = 'Y-M-D H:m:s') {
const formatNumber = n => `0${n}`.slice(-2);
const date = new Date(value);
const formatList = ['Y', 'M', 'D', 'H', 'm', 's'];
const resultList = [];
resultList.push(date.getFullYear().toString());
resultList.push(formatNumber(date.getMonth() + 1));
resultList.push(formatNumber(date.getDate()));
resultList.push(formatNumber(date.getHours()));
resultList.push(formatNumber(date.getMinutes()));
resultList.push(formatNumber(date.getSeconds()));
for (let i = 0; i < resultList.length; i++) {
format = format.replace(formatList[i], resultList[i]);
}
return format;
}
3、判断是否是字符串日期
/**
* @description 判断是否是字符串日期
* @param {*} times 'yyyy-MM-dd HH:mm:ss' || 'yyyy-MM-dd'
* @return {*} Boolean
*/
function isStrDate(times) {
if (typeof times !== 'string') return false;
var reg = /^[0-9,/:-\s]+$/;
if (!isNaN(Date.parse(new Date(times.replace(/-/g, '/')))) && isNaN(times) && reg.test(times)) {
return true;
}
return false;
}
4、朱利安时间转时间戳
/**
* @description 朱利安时间转时间戳
* @param {*} jlData 朱利安时间
* @return {*} 时间戳 s
*/
function julianDateToTimes(jlData) {
var trTimes = new Date(jlData.toString());
return trTimes.getTime();
}
5、对象处理await
/**
* @description await 对象处理
* @param {*} type 属性名
* @param {*} id 参数
* @return {*} response
*/
async function ObjAwait(type, id) {
if(!type) return;
// requestOne, requestTwo 引入的请求
let res = await {
testOne: requestOne,
testTwo: requestTwo
}[type](id);
return res;
}
6、判断dom是否包含
/**
* @description 根据类名判断dom是否包含
* @param {*} rootDomClass 根dom
* @param {*} targetDomClass 目标dom
* @return {*} Boolean
*/
function domIsContains(rootDomClass, targetDomClass) {
const rootDom = document.querySelector(`.${rootDomClass}`);
const targetDom = document.querySelector(`.${targetDomClass}`);
if (rootDom && targetDom) {
if (!rootDom.contains(targetDom)) {
return false;
}
}
return true;
}