/** * 工具函数模块 * 包含弹窗、消息提示等通用功能 */ // ============ 自定义消息弹窗 ============ window.showMessage = function(title, message, type = 'info', onConfirm) { const modal = document.getElementById('customModal'); const modalTitle = document.getElementById('modalTitle'); const modalMessage = document.getElementById('modalMessage'); const modalIcon = document.getElementById('modalIcon'); const modalConfirmBtn = document.getElementById('modalConfirmBtn'); const modalConfirmBtn2 = document.getElementById('modalConfirmBtn2'); const modalBtnGroup = document.getElementById('modalBtnGroup'); const modalSingleBtnGroup = document.getElementById('modalSingleBtnGroup'); // 设置标题 modalTitle.textContent = title; modalTitle.className = 'text-lg font-medium text-gray-800 mb-2'; // 设置消息 modalMessage.innerHTML = message; // 设置图标和按钮颜色 if (type === 'success') { modalIcon.innerHTML = '
'; } else if (type === 'error') { modalIcon.innerHTML = '
'; } else if (type === 'warning') { modalIcon.innerHTML = '
'; } else { modalIcon.innerHTML = '
'; } // 单按钮模式 modalBtnGroup.classList.add('hidden'); modalSingleBtnGroup.classList.remove('hidden'); const confirmBtn = modalConfirmBtn2; if (type === 'error') { confirmBtn.className = 'px-6 py-2 bg-red-500 text-white rounded-lg hover:bg-red-600 transition-colors'; } else { confirmBtn.className = 'px-6 py-2 bg-primary text-white rounded-lg hover:bg-primary/90 transition-colors'; } // 显示弹窗 modal.classList.remove('hidden'); document.body.style.overflow = 'hidden'; // 保存回调到按钮属性 confirmBtn._onConfirm = onConfirm; // 使用 function 而不是箭头函数 confirmBtn.onclick = function() { closeModal(); const callback = this._onConfirm; if (typeof callback === 'function') { callback(); } this._onConfirm = null; }; }; // 关闭消息弹窗 function closeModal() { const modal = document.getElementById('customModal'); modal.classList.add('hidden'); document.body.style.overflow = ''; } // 确认弹窗(两个按钮) window.showConfirm = function(title, message, onConfirm, onCancel, type = 'info') { const modal = document.getElementById('customModal'); const modalTitle = document.getElementById('modalTitle'); const modalMessage = document.getElementById('modalMessage'); const modalIcon = document.getElementById('modalIcon'); const modalConfirmBtn = document.getElementById('modalConfirmBtn'); const modalCancelBtn = document.getElementById('modalCancelBtn'); const modalBtnGroup = document.getElementById('modalBtnGroup'); const modalSingleBtnGroup = document.getElementById('modalSingleBtnGroup'); if (!modalConfirmBtn) { console.error('modalConfirmBtn not found'); return; } modalTitle.textContent = title; modalMessage.innerHTML = message; // 根据类型设置图标 if (type === 'warning') { modalIcon.innerHTML = '
'; } else { modalIcon.innerHTML = '
'; } // 显示双按钮组,隐藏单按钮组 modalBtnGroup.classList.remove('hidden'); modalSingleBtnGroup.classList.add('hidden'); modalConfirmBtn.textContent = '确定'; modalConfirmBtn.className = 'px-6 py-2 bg-primary text-white rounded-lg hover:bg-primary/90 transition-colors'; modal.classList.remove('hidden'); document.body.style.overflow = 'hidden'; // 保存回调到按钮属性 modalConfirmBtn._onConfirm = onConfirm; modalConfirmBtn._onCancel = onCancel; // 使用 function 而不是箭头函数,确保 this 指向正确 modalConfirmBtn.onclick = function() { closeModal(); const callback = this._onConfirm; if (typeof callback === 'function') { callback(); } this._onConfirm = null; this._onCancel = null; }; modalCancelBtn.onclick = function() { closeModal(); const callback = this._onCancel || modalConfirmBtn._onCancel; if (typeof callback === 'function') { callback(); } modalConfirmBtn._onConfirm = null; modalConfirmBtn._onCancel = null; }; }; // ============ Web日志系统 ============ const webLogger = { _currentPage: 'main', // 初始化当前页面名称 init: function(pageName) { this._currentPage = pageName || 'unknown'; }, // 发送日志到服务器 _sendLog: async function(level, message) { try { await fetch(`${window.API_BASE}/web-log`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ level: level, message: message, page: this._currentPage, timestamp: new Date().toISOString() }) }); } catch (e) { // 发送失败时只记录到控制台 console.warn('日志发送失败:', e); } }, info: function(message) { console.log(`[INFO] ${message}`); this._sendLog('info', message); }, error: function(message) { console.error(`[ERROR] ${message}`); this._sendLog('error', message); }, warning: function(message) { console.warn(`[WARNING] ${message}`); this._sendLog('warning', message); }, debug: function(message) { console.debug(`[DEBUG] ${message}`); this._sendLog('debug', message); } }; // 导出 webLogger 到全局 window.webLogger = webLogger;