mirror of
https://github.com/imsyy/home.git
synced 2025-05-19 12:40:13 +09:00
145 lines
4.1 KiB
JavaScript
145 lines
4.1 KiB
JavaScript
import { h } from "vue";
|
|
import { SpaCandle } from "@icon-park/vue-next";
|
|
import dayjs from "dayjs";
|
|
|
|
// 时钟
|
|
export const getCurrentTime = () => {
|
|
let time = new Date();
|
|
let year = time.getFullYear();
|
|
let month = time.getMonth() + 1 < 10 ? "0" + (time.getMonth() + 1) : time.getMonth() + 1;
|
|
let day = time.getDate() < 10 ? "0" + time.getDate() : time.getDate();
|
|
let hour = time.getHours() < 10 ? "0" + time.getHours() : time.getHours();
|
|
let minute = time.getMinutes() < 10 ? "0" + time.getMinutes() : time.getMinutes();
|
|
let second = time.getSeconds() < 10 ? "0" + time.getSeconds() : time.getSeconds();
|
|
let weekday = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
|
|
let currentTime = {
|
|
year,
|
|
month,
|
|
day,
|
|
hour,
|
|
minute,
|
|
second,
|
|
weekday: weekday[time.getDay()],
|
|
};
|
|
return currentTime;
|
|
};
|
|
|
|
// 时光胶囊
|
|
export const getTimeCapsule = () => {
|
|
const now = dayjs();
|
|
const dayText = {
|
|
day: "今日",
|
|
week: "本周",
|
|
month: "本月",
|
|
year: "本年",
|
|
};
|
|
/**
|
|
* 计算时间差的函数
|
|
* @param {String} unit 时间单位,可以是 'day', 'week', 'month', 'year'
|
|
*/
|
|
const getDifference = (unit) => {
|
|
// 获取当前时间单位的开始时间
|
|
const start = now.startOf(unit);
|
|
// 获取当前时间单位的结束时间
|
|
const end = now.endOf(unit);
|
|
// 计算总的天数或小时数
|
|
const total = end.diff(start, unit === "day" ? "hour" : "day") + 1;
|
|
// 计算已经过去的天数或小时数
|
|
let passed = now.diff(start, unit === "day" ? "hour" : "day");
|
|
if (unit === "week") {
|
|
passed = (passed + 6) % 7;
|
|
}
|
|
const remaining = total - passed;
|
|
const percentage = (passed / total) * 100;
|
|
// 返回数据
|
|
return {
|
|
name: dayText[unit],
|
|
total: total,
|
|
passed: passed,
|
|
remaining: remaining,
|
|
percentage: percentage.toFixed(2),
|
|
};
|
|
};
|
|
return {
|
|
day: getDifference("day"),
|
|
week: getDifference("week"),
|
|
month: getDifference("month"),
|
|
year: getDifference("year"),
|
|
};
|
|
};
|
|
|
|
// 欢迎提示
|
|
export const helloInit = () => {
|
|
const hour = new Date().getHours();
|
|
let hello = null;
|
|
if (hour < 6) {
|
|
hello = "凌晨好";
|
|
} else if (hour < 9) {
|
|
hello = "早上好";
|
|
} else if (hour < 12) {
|
|
hello = "上午好";
|
|
} else if (hour < 14) {
|
|
hello = "中午好";
|
|
} else if (hour < 17) {
|
|
hello = "下午好";
|
|
} else if (hour < 19) {
|
|
hello = "傍晚好";
|
|
} else if (hour < 22) {
|
|
hello = "晚上好";
|
|
} else {
|
|
hello = "夜深了";
|
|
}
|
|
ElMessage({
|
|
dangerouslyUseHTMLString: true,
|
|
message: `<strong>${hello}</strong> 欢迎来到我的主页`,
|
|
});
|
|
};
|
|
|
|
// 默哀模式
|
|
const anniversaries = {
|
|
4.4: "清明节",
|
|
5.12: "汶川大地震纪念日",
|
|
7.7: "中国人民抗日战争纪念日",
|
|
9.18: "九·一八事变纪念日",
|
|
12.13: "南京大屠杀死难者国家公祭日",
|
|
};
|
|
export const checkDays = () => {
|
|
const myDate = new Date();
|
|
const mon = myDate.getMonth() + 1;
|
|
const date = myDate.getDate();
|
|
const key = `${mon}.${date}`;
|
|
if (Object.prototype.hasOwnProperty.call(anniversaries, key)) {
|
|
console.log(`今天是${anniversaries[key]}`);
|
|
const gray = document.createElement("style");
|
|
gray.innerHTML = "html{filter: grayscale(100%)}";
|
|
document.head.appendChild(gray);
|
|
ElMessage({
|
|
message: `今天是${anniversaries[key]}`,
|
|
duration: 14000,
|
|
icon: h(SpaCandle, { theme: "filled", fill: "#efefef" }),
|
|
});
|
|
}
|
|
};
|
|
|
|
// 建站日期统计
|
|
export const siteDateStatistics = (startDate) => {
|
|
const currentDate = new Date();
|
|
let years = currentDate.getFullYear() - startDate.getFullYear();
|
|
let months = currentDate.getMonth() - startDate.getMonth();
|
|
let days = currentDate.getDate() - startDate.getDate();
|
|
|
|
// 如果天数或月份为负数,则调整天数和月份
|
|
if (days < 0) {
|
|
months--;
|
|
const lastMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() - 1, 0);
|
|
days += lastMonth.getDate();
|
|
}
|
|
|
|
if (months < 0) {
|
|
years--;
|
|
months += 12;
|
|
}
|
|
|
|
return `本站已经苟活了 ${years} 年 ${months} 月 ${days} 天`;
|
|
};
|