// 清空当前助手对话记录
document.getElementById('clearDataBtn').addEventListener('click', () => {
if (!confirm('确定清空当前助手的对话记录?')) return;
const currentAgent = 'default';
const histKey = 'qt_history_' + currentAgent;
localStorage.removeItem(histKey);
startNewChat();
showToast('已清空当前助手的对话记录', false);
});
// ===== 快捷键(阶段5接管补齐) =====
document.getElementById('setShortcutBtn').addEventListener('click', () => {
fetch('/api/v1/users/me/settings', {
method: 'PUT',
headers: {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + getAccessToken()},
credentials: 'include',
body: JSON.stringify({key: 'shortcut_new_chat', value: 'Ctrl+Shift+N'})
}).then(() => showToast('新对话快捷键已保存', false)).catch(() => showToast('保存失败', true));
});
document.addEventListener('keydown', (e) => {
if (e.ctrlKey && e.shiftKey && (e.key === 'N' || e.key === 'n')) {
const t = e.target.tagName;
if (t === 'INPUT' || t === 'TEXTAREA') return;
e.preventDefault();
startNewChat();
showToast('已开启新对话', false);
}
});
// ===== 反馈提交(阶段5接管补齐) =====
document.getElementById('submitFeedbackBtn').addEventListener('click', () => {
const input = document.getElementById('feedbackInput');
const text = (input.value || '').trim();
if (!text) { showToast('请先填写反馈内容', true); return; }
fetch('/api/v1/users/me/settings', {headers: {'Authorization': 'Bearer ' + getAccessToken()}, credentials: 'include'})
.then(r => r.json())
.then(data => {
const arr = Array.isArray(data.settings && data.settings.feedbacks) ? [...data.settings.feedbacks] : [];
arr.push({text: text, created_at: new Date().toISOString()});
return fetch('/api/v1/users/me/settings', {
method: 'PUT',
headers: {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + getAccessToken()},
credentials: 'include',
body: JSON.stringify({key: 'feedbacks', value: arr})
});
})
.then(() => { input.value = ''; showToast('反馈已提交,感谢!', false); })
.catch(() => showToast('提交失败', true));
});
// ===== 安全中心(阶段5接管补齐) =====
document.getElementById('logoutBtnInSettings').addEventListener('click', () => {
if (confirm('确定退出登录?')) logout();
});
function loadSecurityInfo() {
try {
const st = document.getElementById('securityLoginStatus');
const lt = document.getElementById('securityLastLogin');
if (st) st.textContent = (typeof currentUser !== 'undefined' && currentUser && currentUser.username) ? ('已登录:' + currentUser.username) : '已登录';
if (lt) lt.textContent = '本次会话 ' + new Date().toLocaleString('zh-CN');
} catch (e) {}
}
loadSecurityInfo();
fetch('/api/v1/users/me/settings', {headers: {'Authorization': 'Bearer ' + getAccessToken()}, credentials: 'include'})
.then(r => r.json())
.then(data => {
const sc = data.settings && data.settings.shortcut_new_chat;
if (sc) { const el = document.getElementById('setShortcutBtn'); if (el) el.textContent = '已保存: ' + sc; }
}).catch(() => {});
// 导出聊天记录
document.getElementById('exportHistoryBtn').addEventListener('click', () => {
const currentAgent = 'default';
const histKey = 'qt_history_' + currentAgent;
const history = localStorage.getItem(histKey);
if (!history) {
showToast('当前助手暂无聊天记录', true);
return;
}
try {
const data = JSON.parse(history);
const blob = new Blob([JSON.stringify(data, null, 2)], {type: 'application/json'});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'aigo_chat_history.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
} catch (e) {
console.error('导出失败:', e);
showToast('导出失败,请重试', true);
}
});