222 lines
6.8 KiB
JavaScript
222 lines
6.8 KiB
JavaScript
const dateTimePicker = (startYear, endYear) => {
|
||
// 生成年份数组
|
||
const years = [];
|
||
for (let i = startYear; i <= endYear; i++) {
|
||
years.push(i.toString());
|
||
}
|
||
|
||
// 生成月份数组
|
||
const months = [];
|
||
for (let i = 1; i <= 12; i++) {
|
||
months.push(i < 10 ? `0${i}` : i.toString());
|
||
}
|
||
|
||
// 生成日期数组
|
||
const days = [];
|
||
for (let i = 1; i <= 31; i++) {
|
||
days.push(i < 10 ? `0${i}` : i.toString());
|
||
}
|
||
|
||
// 生成小时数组
|
||
const hours = [];
|
||
for (let i = 0; i < 24; i++) {
|
||
hours.push(i < 10 ? `0${i}` : i.toString());
|
||
}
|
||
|
||
// 生成分钟数组
|
||
const minutes = [];
|
||
for (let i = 0; i < 60; i++) {
|
||
minutes.push(i < 10 ? `0${i}` : i.toString());
|
||
}
|
||
|
||
// 生成秒数组
|
||
const seconds = [];
|
||
for (let i = 0; i < 60; i++) {
|
||
seconds.push(i < 10 ? `0${i}` : i.toString());
|
||
}
|
||
|
||
// 获取当前日期和时间
|
||
const now = new Date();
|
||
const currentYear = now.getFullYear().toString();
|
||
const currentMonth = (now.getMonth() + 1).toString().padStart(2, '0');
|
||
const currentDay = now.getDate().toString().padStart(2, '0');
|
||
const currentHour = now.getHours().toString().padStart(2, '0');
|
||
const currentMinute = now.getMinutes().toString().padStart(2, '0');
|
||
const currentSecond = now.getSeconds().toString().padStart(2, '0');
|
||
|
||
// 获取默认选中的索引
|
||
const dateTime = [
|
||
years.indexOf(currentYear),
|
||
months.indexOf(currentMonth),
|
||
days.indexOf(currentDay),
|
||
hours.indexOf(currentHour),
|
||
minutes.indexOf(currentMinute),
|
||
seconds.indexOf(currentSecond)
|
||
];
|
||
|
||
// 返回包含所有数组和默认选中索引的对象
|
||
const dateTimeArray = [years, months, days, hours, minutes, seconds];
|
||
return {
|
||
dateTimeArray,
|
||
dateTime
|
||
};
|
||
}
|
||
|
||
const pxToRpx = (pxValue, screenWidth) => {
|
||
console.log('转换Px', pxValue, '屏幕宽度', screenWidth)
|
||
// return pxValue * (750 / screenWidth);
|
||
const rpx = (750 / screenWidth) * Number(pxValue)
|
||
return Math.floor(rpx);
|
||
}
|
||
|
||
const rpxToPx = (rpxValue, screenWidth) => {
|
||
return rpxValue * screenWidth / 750;
|
||
}
|
||
|
||
|
||
const formatTime = date => {
|
||
const year = date.getFullYear()
|
||
const month = date.getMonth() + 1
|
||
const day = date.getDate()
|
||
const hour = date.getHours()
|
||
const minute = date.getMinutes()
|
||
const second = date.getSeconds()
|
||
|
||
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
|
||
}
|
||
|
||
/**
|
||
*
|
||
* @param {*} date
|
||
*/
|
||
const formatDate = date => {
|
||
const year = date.getFullYear()
|
||
const month = date.getMonth() + 1
|
||
const day = date.getDate()
|
||
|
||
return [year, month, day].map(formatNumber).join('-')
|
||
}
|
||
|
||
const formatNumber = n => {
|
||
n = n.toString()
|
||
return n[1] ? n : '0' + n
|
||
}
|
||
|
||
//随机生成暖色系
|
||
const randomLightColor = () => {
|
||
// 暖色系的范围通常是红色和黄色较多,所以红色分量范围从 128 到 255
|
||
let red = Math.floor(Math.random() * 128) + 128;
|
||
// 绿色分量范围从 64 到 255
|
||
let green = Math.floor(Math.random() * 192) + 64;
|
||
// 蓝色分量范围从 0 到 128
|
||
let blue = Math.floor(Math.random() * 128);
|
||
|
||
// 将十进制的 RGB 分量转换为十六进制,并确保长度为 2 位
|
||
let redHex = red.toString(16).padStart(2, '0');
|
||
let greenHex = green.toString(16).padStart(2, '0');
|
||
let blueHex = blue.toString(16).padStart(2, '0');
|
||
|
||
// 组合成十六进制颜色代码
|
||
return '#' + redHex + greenHex + blueHex;
|
||
}
|
||
//随机生成冷色系
|
||
const randomDarkColor = () => {
|
||
// 冷色系通常蓝色和绿色较多,所以蓝色分量范围从 128 到 255
|
||
let blue = Math.floor(Math.random() * 128) + 128;
|
||
// 绿色分量范围从 128 到 255
|
||
let green = Math.floor(Math.random() * 128) + 128;
|
||
// 红色分量范围从 0 到 128
|
||
let red = Math.floor(Math.random() * 128);
|
||
|
||
// 将十进制的 RGB 分量转换为十六进制,并确保长度为 2 位
|
||
let redHex = red.toString(16).padStart(2, '0');
|
||
let greenHex = green.toString(16).padStart(2, '0');
|
||
let blueHex = blue.toString(16).padStart(2, '0');
|
||
|
||
// 组合成十六进制颜色代码
|
||
return '#' + redHex + greenHex + blueHex;
|
||
}
|
||
|
||
const randomRgbColor = () => { // 随机生成RGB颜色
|
||
var r = Math.floor(Math.random() * 256); // 随机生成256以内r值
|
||
var g = Math.floor(Math.random() * 256); // 随机生成256以内g值
|
||
var b = Math.floor(Math.random() * 256); // 随机生成256以内b值
|
||
return `rgb(${r},${g},${b})`; // 返回rgb(r,g,b)格式颜色
|
||
}
|
||
const randomRgbaColor = () => { // 随机生成RGBA颜色
|
||
var r = Math.floor(Math.random() * 256); // 随机生成256以内r值
|
||
var g = Math.floor(Math.random() * 256); // 随机生成256以内g值
|
||
var b = Math.floor(Math.random() * 256); // 随机生成256以内b值
|
||
var alpha = Math.random(); // 随机生成1以内a值
|
||
return `rgba(${r},${g},${b},${alpha})`; // 返回rgba(r,g,b,a)格式颜色
|
||
}
|
||
|
||
const formatRichText = (html) => {
|
||
let newContent = html.replace(/<img(^>)*>/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(/<br(^>)*\/>/gi, '');
|
||
newContent = newContent.replace(/\<img/gi,
|
||
'<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
|
||
return newContent;
|
||
}
|
||
/**
|
||
* 去除富文本字符串中img的style
|
||
* richText 富文本字符串
|
||
*/
|
||
const removeImgStyleFromRichText = (richText) => {
|
||
// 匹配 <img> 标签
|
||
let imgTags = richText.match(/<img[^>]*>/g);
|
||
if (imgTags) {
|
||
imgTags.forEach((imgTag) => {
|
||
// 去除 style 属性
|
||
let newImgTag = imgTag.replace(/ style="[^"]*"/g, '');
|
||
richText = richText.replace(imgTag, newImgTag);
|
||
});
|
||
}
|
||
return richText;
|
||
}
|
||
//生成32位随机字符串
|
||
const random32Str = () => {
|
||
const charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||
let randomString = '';
|
||
for (let i = 0; i < 32; i++) {
|
||
const randomIndex = Math.floor(Math.random() * charSet.length);
|
||
randomString += charSet.charAt(randomIndex);
|
||
}
|
||
return randomString;
|
||
}
|
||
/**
|
||
* 获取当前日期
|
||
*/
|
||
const currentDate = () => {
|
||
const now = new Date();
|
||
const year = now.getFullYear();
|
||
const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份+1(0~11)
|
||
const day = String(now.getDate()).padStart(2, '0');
|
||
return `${year}-${month}-${day}`;
|
||
}
|
||
|
||
export {
|
||
formatTime,
|
||
formatDate,
|
||
randomRgbColor,
|
||
randomRgbaColor,
|
||
randomDarkColor,
|
||
randomLightColor,
|
||
formatRichText,
|
||
removeImgStyleFromRichText,
|
||
dateTimePicker,
|
||
pxToRpx,
|
||
random32Str,
|
||
rpxToPx,
|
||
currentDate
|
||
} |