// 历史记录相关方法
|
export function getHistoryFromStorage() {
|
const data = localStorage.getItem('loginHistory');
|
return data ? JSON.parse(data) : [];
|
}
|
|
export function saveHistoryToStorage(history) {
|
localStorage.setItem('loginHistory', JSON.stringify(history));
|
}
|
|
// 日期时间格式化
|
export function formatDateTime(date) {
|
const year = date.getFullYear();
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
const day = String(date.getDate()).padStart(2, '0');
|
const hours = String(date.getHours()).padStart(2, '0');
|
const minutes = String(date.getMinutes()).padStart(2, '0');
|
return `${year}-${month}-${day} ${hours}:${minutes}`;
|
}
|
|
// 检查密码是否有效
|
export function isValid(expiryTime) {
|
return new Date(expiryTime) > new Date();
|
}
|
|
// 计算剩余时间
|
export function getRemainingTime(expiryTime) {
|
const now = new Date();
|
const expiry = new Date(expiryTime);
|
const diff = expiry - now;
|
|
if (diff <= 0) {
|
return '已过期';
|
}
|
|
const hours = Math.floor(diff / (1000 * 60 * 60));
|
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
|
|
if (hours > 0) {
|
return `${hours}小时${minutes}分钟`;
|
} else {
|
return `${minutes}分钟`;
|
}
|
}
|
|
// 生成验证码
|
export function generateCode() {
|
return Math.floor(Math.random() * 1000000).toString().padStart(6, '0');
|
}
|
|
// 生成密码(6位数字)
|
export function generatePassword() {
|
return Math.floor(Math.random() * 1000000).toString().padStart(6, '0');
|
}
|
|
// 显示消息提示
|
export function showMessage(message, type = 'error') {
|
const messageDiv = document.createElement('div');
|
messageDiv.className = 'message-toast';
|
messageDiv.textContent = message;
|
messageDiv.style.cssText = `
|
position: fixed;
|
top: 50%;
|
left: 50%;
|
transform: translate(-50%, -50%);
|
background: ${type === 'success' ? '#4CAF50' : '#f44336'};
|
color: white;
|
padding: 16px 24px;
|
border-radius: 8px;
|
font-size: 14px;
|
z-index: 9999;
|
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
animation: slideDown 0.3s ease-out;
|
`;
|
|
// 添加样式
|
if (!document.getElementById('toast-styles')) {
|
const style = document.createElement('style');
|
style.id = 'toast-styles';
|
style.textContent = `
|
@keyframes slideDown {
|
from {
|
opacity: 0;
|
transform: translate(-50%, -60%);
|
}
|
to {
|
opacity: 1;
|
transform: translate(-50%, -50%);
|
}
|
}
|
`;
|
document.head.appendChild(style);
|
}
|
|
document.body.appendChild(messageDiv);
|
|
setTimeout(() => {
|
messageDiv.style.animation = 'slideDown 0.3s ease-out reverse';
|
setTimeout(() => {
|
document.body.removeChild(messageDiv);
|
}, 300);
|
}, 2000);
|
}
|
|
// 复制到剪贴板
|
export function copyToClipboard(text) {
|
return navigator.clipboard.writeText(text).catch(() => {
|
// 备用方案
|
const textarea = document.createElement('textarea');
|
textarea.value = text;
|
document.body.appendChild(textarea);
|
textarea.select();
|
document.execCommand('copy');
|
document.body.removeChild(textarea);
|
});
|
}
|