'; const win = window.open('', '_blank', 'width=900,height=700,scrollbars=yes'); win.document.write(pageHTML); win.document.close(); } catch(e) { console.log('[VIP] 完整报告生成失败', e); } } // 加载密码状态 async function loadPasswordStatus() { const token = localStorage.getItem('sousou_token'); try { const res = await fetch('/api/user/password-status', { headers: { 'Authorization': 'Bearer ' + token } }); const data = await res.json(); if (data.code === 200) { hasPassword = data.data.has_password; updatePasswordUI(); } else { console.log('[USER] 密码状态查询失败:', data.message); document.getElementById('setting-password').textContent = '未设置'; document.getElementById('setting-password').style.color = '#888'; } } catch(e) { console.log('[USER] 密码状态加载失败', e); document.getElementById('setting-password').textContent = '未设置'; document.getElementById('setting-password').style.color = '#888'; // 显示设置按钮,让用户可以重试 document.getElementById('btn-set-password').style.display = 'inline-block'; } } // 更新密码UI function updatePasswordUI() { const statusEl = document.getElementById('setting-password'); const setBtn = document.getElementById('btn-set-password'); const changeBtn = document.getElementById('btn-change-password'); if (hasPassword) { statusEl.textContent = '已设置'; statusEl.style.color = '#00b386'; setBtn.style.display = 'none'; changeBtn.style.display = 'inline-block'; } else { statusEl.textContent = '未设置'; statusEl.style.color = '#FF5A1F'; setBtn.style.display = 'inline-block'; changeBtn.style.display = 'none'; } } // 打开密码弹窗 function openPasswordModal(mode) { passwordModalMode = mode; const modal = document.getElementById('password-modal'); const title = document.getElementById('password-modal-title'); const desc = document.getElementById('password-modal-desc'); const oldGroup = document.getElementById('old-password-group'); // 重置输入 document.getElementById('old-password-input').value = ''; document.getElementById('new-password-input').value = ''; document.getElementById('confirm-password-input').value = ''; document.getElementById('password-error').classList.remove('show'); if (mode === 'set') { title.textContent = '设置密码'; desc.textContent = '设置密码后可用手机号+密码登录'; oldGroup.style.display = 'none'; } else { title.textContent = '修改密码'; desc.textContent = '请输入旧密码验证身份'; oldGroup.style.display = 'block'; } modal.classList.add('show'); setTimeout(() => { if (mode === 'change') { document.getElementById('old-password-input').focus(); } else { document.getElementById('new-password-input').focus(); } }, 100); } // 关闭密码弹窗 function closePasswordModal() { document.getElementById('password-modal').classList.remove('show'); } // 保存密码 async function savePassword() { if (isSavingPassword) return; const token = localStorage.getItem('sousou_token'); const oldPwd = document.getElementById('old-password-input').value; const newPwd = document.getElementById('new-password-input').value; const confirmPwd = document.getElementById('confirm-password-input').value; const errEl = document.getElementById('password-error'); // 验证 if (passwordModalMode === 'change' && !oldPwd) { errEl.textContent = '请输入旧密码'; errEl.classList.add('show'); return; } if (!newPwd || newPwd.length < 6) { errEl.textContent = '新密码至少6位'; errEl.classList.add('show'); return; } if (newPwd.length > 32) { errEl.textContent = '新密码最多32位'; errEl.classList.add('show'); return; } if (newPwd !== confirmPwd) { errEl.textContent = '两次输入的密码不一致'; errEl.classList.add('show'); return; } isSavingPassword = true; const btn = document.getElementById('save-password-btn'); btn.disabled = true; btn.textContent = '保存中...'; errEl.classList.remove('show'); try { const url = passwordModalMode === 'set' ? '/api/user/set-password' : '/api/user/change-password'; const body = passwordModalMode === 'set' ? { password: newPwd } : { old_password: oldPwd, new_password: newPwd }; const res = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token }, body: JSON.stringify(body) }); const data = await res.json(); if (data.code === 200) { hasPassword = true; updatePasswordUI(); closePasswordModal(); alert(passwordModalMode === 'set' ? '密码设置成功!' : '密码修改成功!'); } else { errEl.textContent = data.message || '保存失败'; errEl.classList.add('show'); } } catch(e) { errEl.textContent = '网络错误,请重试'; errEl.classList.add('show'); } finally { isSavingPassword = false; btn.disabled = false; btn.textContent = '保存'; } } // ========== 监测详情弹窗 HTML(由 viewMonitorDetail 调用) ==========