| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- // utils.js - 通用工具函数
- // 保存数据到localStorage
- function setLocalStorage(name, value) {
- try {
- localStorage.setItem(name, value);
- } catch (e) {
- console.error('保存数据失败:', e);
- }
- }
- // 从localStorage获取数据
- function getLocalStorage(name) {
- try {
- return localStorage.getItem(name);
- } catch (e) {
- console.error('获取数据失败:', e);
- return null;
- }
- }
- // 从localStorage删除数据
- function removeLocalStorage(name) {
- try {
- localStorage.removeItem(name);
- } catch (e) {
- console.error('删除数据失败:', e);
- }
- }
- // 更新当前时间
- function updateCurrentTime() {
- const now = new Date();
- const hours = now.getHours().toString().padStart(2, '0');
- const minutes = now.getMinutes().toString().padStart(2, '0');
- const seconds = now.getSeconds().toString().padStart(2, '0');
- const timeString = `${hours}:${minutes}:${seconds}`;
- document.getElementById('current-time').textContent = timeString;
- }
- function parseUnixTime(unixTimestamp) {
- // 将Unix时间戳转换为毫秒
- const timestamp = parseInt(unixTimestamp);
- const date = new Date(timestamp);
- // 格式化日期和时间
- const year = date.getFullYear();
- const month = (date.getMonth() + 1).toString().padStart(2, '0');
- const day = date.getDate().toString().padStart(2, '0');
- const hours = date.getHours().toString().padStart(2, '0');
- const minutes = date.getMinutes().toString().padStart(2, '0');
- const seconds = date.getSeconds().toString().padStart(2, '0');
- // 返回格式化后的时间字符串
- return `${hours}:${minutes}:${seconds}`;
- // return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
- }
- // 显示提示信息
- // 跟踪当前显示的toast数量
- let activeToasts = 0;
- const MAX_TOASTS = 3;
- function showToast(message, type = 'info', duration = 3000) {
- // 如果已经达到最大数量,移除最早的toast
- const existingToasts = document.querySelectorAll('.toast');
- if (existingToasts.length >= MAX_TOASTS) {
- // 不移除最早的,而是等待它自动消失
- // 只是不再增加新的位置
- }
- // 创建toast元素
- const toast = document.createElement('div');
- toast.className = `toast toast-${type}`;
- toast.textContent = message;
-
- // 计算当前toast应该的位置
- const position = Math.min(existingToasts.length, MAX_TOASTS - 1);
- const topPosition = 20 + position * 60; // 每个toast高度约50px,加上间距10px
- // 设置样式
- toast.style.cssText = `
- position: fixed;
- top: ${topPosition}px;
- left: 50%;
- transform: translateX(-50%);
- padding: 10px 20px;
- border-radius: 4px;
- color: #fff;
- font-size: 14px;
- z-index: 9999;
- opacity: 0;
- transition: opacity 0.3s ease-in-out;
- `;
- // 根据类型设置背景色
- const colors = {
- success: '#67C23A',
- error: '#F56C6C',
- warning: '#E6A23C',
- info: '#909399'
- };
- toast.style.backgroundColor = colors[type] || colors.info;
- // 添加到文档
- document.body.appendChild(toast);
- activeToasts++;
- // 显示动画
- setTimeout(() => {
- toast.style.opacity = '1';
- }, 10);
- // 自动移除
- setTimeout(() => {
- toast.style.opacity = '0';
- setTimeout(() => {
- document.body.removeChild(toast);
- activeToasts--;
-
- // 当一个toast被移除时,重新调整其他toast的位置
- repositionToasts();
- }, 300);
- }, duration);
- }
- // 重新调整所有toast的位置
- function repositionToasts() {
- const toasts = document.querySelectorAll('.toast');
- toasts.forEach((toast, index) => {
- if (index < MAX_TOASTS) {
- const topPosition = 20 + index * 60;
- toast.style.top = `${topPosition}px`;
- }
- });
- }
|