Files

97 lines
2.8 KiB
JavaScript

// admin.js - 后台管理公共功能模块
/**
* 侧边栏切换
*/
function toggleSidebar() {
const sidebar = document.getElementById('sidebar');
const mainArea = document.querySelector('.main-area');
const toggleIcon = document.querySelector('.sidebar-toggle i');
if (sidebar && mainArea && toggleIcon) {
if (sidebar.classList.contains('collapsed')) {
sidebar.classList.remove('collapsed');
mainArea.style.marginLeft = '256px';
toggleIcon.className = 'fas fa-chevron-left';
} else {
sidebar.classList.add('collapsed');
mainArea.style.marginLeft = '0';
toggleIcon.className = 'fas fa-chevron-right';
}
}
}
/**
* 自定义弹窗提示
* @param {string} type - 类型: success, error, info, warning
* @param {string} title - 标题
* @param {string} message - 消息内容
*/
function showCustomAlert(type, title, message) {
const modal = document.getElementById('customAlertModal');
const icon = document.getElementById('alertIcon');
const alertTitle = document.getElementById('alertTitle');
const alertMessage = document.getElementById('alertMessage');
if (!modal || !icon || !alertTitle || !alertMessage) return;
const icons = {
success: '<i class="fas fa-check-circle" style="color: #10b981;"></i>',
error: '<i class="fas fa-exclamation-circle" style="color: #ef4444;"></i>',
info: '<i class="fas fa-info-circle" style="color: #3b82f6;"></i>',
warning: '<i class="fas fa-exclamation-triangle" style="color: #f59e0b;"></i>'
};
icon.innerHTML = icons[type] || icons.info;
alertTitle.textContent = title;
alertMessage.textContent = message;
modal.style.display = 'flex';
}
/**
* 关闭自定义弹窗
*/
function closeCustomAlert() {
const modal = document.getElementById('customAlertModal');
if (modal) modal.style.display = 'none';
}
/**
* HTML转义,防止XSS攻击
* @param {string} text - 需要转义的文本
* @returns {string} 转义后的文本
*/
function escapeHtml(text) {
if (!text) return '';
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
/**
* 验证邮箱格式
* @param {string} email - 邮箱地址
* @returns {boolean} 是否有效
*/
function isValidEmail(email) {
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(email);
}
/**
* 验证手机号格式
* @param {string} phone - 手机号码
* @returns {boolean} 是否有效
*/
function isValidPhone(phone) {
const re = /^1[3-9]\d{9}$/;
return re.test(phone);
}
// 点击模态框外部关闭弹窗
document.addEventListener('click', function(e) {
const alertModal = document.getElementById('customAlertModal');
if (alertModal && e.target === alertModal) {
closeCustomAlert();
}
});