Firefly Keeper
请输入密码解锁
if (type === 'error') colorClass = 'text-red-400';
if (type === 'process') colorClass = 'text-blue-400';
statusLog.innerHTML = `
[${time}] ${msg}
` + statusLog.innerHTML;
}
// 发送数据逻辑
async function sendData() {
const text = inputField.value.trim();
if (!text) {
log("请输入记账内容", "error");
return;
}
// UI 锁定
sendBtn.disabled = true;
sendBtn.innerHTML = `
处理中...`;
try {
log("正在发送给 AI 审计...", "process");
// 发送 POST 请求
const response = await fetch(WEBHOOK_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: text })
});
if (response.ok) {
const result = await response.json();
log("✅ 记账成功!", "success");
// 可以在这里把结果打印出来,视 N8N 返回值而定
if (result.text) log(`AI回复: ${result.text}`, "success");
saveHistory(text, 'success'); // 保存历史
inputField.value = ''; // 清空输入框
} else {
log(`❌ 服务器报错: ${response.status}`, "error");
saveHistory(text, 'error'); // 保存历史
}
} catch (error) {
log(`❌ 网络错误: ${error.message}`, "error");
} finally {
// UI 恢复
sendBtn.disabled = false;
sendBtn.innerHTML = `
发送记账`;
}
}
// 语音识别逻辑 (仅支持 Chrome/Android/iOS Safari 14.5+)
let recognition = null;
if ('webkitSpeechRecognition' in window || 'SpeechRecognition' in window) {
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
recognition = new SpeechRecognition();
recognition.lang = 'zh-CN';
recognition.continuous = true; // 开启连续模式
recognition.interimResults = true; // 开启临时结果
let originalText = '';
recognition.onstart = () => {
recordingOverlay.classList.remove('hidden');
log("正在聆听...", "process");
originalText = inputField.value;
};
recognition.onend = () => {
recordingOverlay.classList.add('hidden');
};
recognition.onresult = (event) => {
let currentSessionText = '';
for (let i = 0; i < event.results.length; ++i) { currentSessionText +=event.results[i][0].transcript; }
inputField.value=originalText + currentSessionText; }; micBtn.onclick=()=> {
recognition.start();
};
window.stopRecording = () => {
recognition.stop();
};
} else {
micBtn.style.display = 'none'; // 不支持语音就隐藏按钮
log("当前浏览器不支持语音识别", "error");
}
// ================= 历史记录逻辑 =================
function loadHistory() {
const history = JSON.parse(localStorage.getItem('firefly_history') || '[]');
renderHistory(history);
}
function saveHistory(text, status) {
const history = JSON.parse(localStorage.getItem('firefly_history') || '[]');
const newItem = {
text: text,
status: status,
time: new Date().toLocaleString('zh-CN', { month: 'numeric', day: 'numeric', hour: '2-digit',
minute: '2-digit' })
};
// 添加到开头,保留最近10条
history.unshift(newItem);
if (history.length > 10) history.pop();
localStorage.setItem('firefly_history', JSON.stringify(history));
renderHistory(history);
}
function clearHistory() {
if (confirm('确定要清除所有历史记录吗?')) {
localStorage.removeItem('firefly_history');
renderHistory([]);
}
}
function renderHistory(history) {
if (history.length === 0) {
historyList.innerHTML = '
暂无记录';
return;
}
historyList.innerHTML = history.map(item => {
const statusColor = item.status === 'success' ? 'bg-emerald-500/20 text-emerald-400
border-emerald-500/30' : 'bg-red-500/20 text-red-400 border-red-500/30';
const icon = item.status === 'success' ? '✓' : '✕';
return `
${item.text}
${item.time}
${icon}
`;
}).join('');
}