diff --git a/www/app.js b/www/app.js index 3b1ab61..e1fe6f7 100644 --- a/www/app.js +++ b/www/app.js @@ -67,6 +67,166 @@ let pinnedAgents = { let currentAgent = null; // 当前选中的智能体 +// 用户状态 +let currentUser = null; // 当前登录用户 { username, password, registeredAt } + +// 每日使用统计(未登录用户) +let dailyUsage = { + date: null, // 日期 YYYY-MM-DD + chatSessions: 0, // 对话会话数 + chatMessages: 0, // 对话消息数 + agentUsed: null, // 使用的智能体ID(只能用一个) + agentMessages: 0 // 智能体消息数 +}; + +// 未登录用户限制 +const GUEST_LIMITS = { + maxChatSessionsPerDay: 1, // 每天最多1个对话会话 + maxChatMessagesPerDay: 20, // 每天最多20条对话消息 + maxAgentPerDay: 1, // 每天只能用1个智能体(通用助手) + maxAgentMessagesPerDay: 20 // 每天最多20条智能体消息 +}; + +// 获取今日日期字符串 +function getTodayDate() { + const today = new Date(); + return `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`; +} + +// 检查并重置每日使用统计 +function checkDailyUsage() { + const today = getTodayDate(); + if (dailyUsage.date !== today) { + // 新的一天,重置统计 + dailyUsage = { + date: today, + chatSessions: 0, + chatMessages: 0, + agentUsed: null, + agentMessages: 0 + }; + saveDailyUsage(); + } +} + +// 保存用户状态 +function saveCurrentUser() { + if (currentUser) { + localStorage.setItem('currentUser', JSON.stringify(currentUser)); + } else { + localStorage.removeItem('currentUser'); + } +} + +// 保存每日使用统计 +function saveDailyUsage() { + localStorage.setItem('dailyUsage', JSON.stringify(dailyUsage)); +} + +// 检查是否可以创建新对话(未登录用户) +function canCreateNewChat() { + if (currentUser) return true; // 登录用户无限制 + + checkDailyUsage(); + return dailyUsage.chatSessions < GUEST_LIMITS.maxChatSessionsPerDay; +} + +// 检查是否可以发送对话消息(未登录用户) +function canSendChatMessage() { + if (currentUser) return true; // 登录用户无限制 + + checkDailyUsage(); + return dailyUsage.chatMessages < GUEST_LIMITS.maxChatMessagesPerDay; +} + +// 检查是否可以使用智能体(未登录用户) +function canUseAgent(agentId) { + if (currentUser) return true; // 登录用户无限制 + + checkDailyUsage(); + + // 未登录用户只能使用通用助手 + if (agentId !== 'assistant') { + return { allowed: false, reason: 'guest_agent_only' }; + } + + // 如果今天还没用智能体,可以用 + if (!dailyUsage.agentUsed) { + return { allowed: true }; + } + + // 如果已经用了其他智能体,不能换 + if (dailyUsage.agentUsed !== agentId) { + return { allowed: false, reason: 'guest_one_agent_only' }; + } + + return { allowed: true }; +} + +// 检查是否可以发送智能体消息(未登录用户) +function canSendAgentMessage() { + if (currentUser) return true; // 登录用户无限制 + + checkDailyUsage(); + return dailyUsage.agentMessages < GUEST_LIMITS.maxAgentMessagesPerDay; +} + +// 增加对话会话计数 +function incrementChatSession() { + if (!currentUser) { + checkDailyUsage(); + dailyUsage.chatSessions++; + saveDailyUsage(); + } +} + +// 增加对话消息计数 +function incrementChatMessage() { + if (!currentUser) { + checkDailyUsage(); + dailyUsage.chatMessages++; + saveDailyUsage(); + } +} + +// 设置使用的智能体(未登录用户) +function setAgentUsed(agentId) { + if (!currentUser) { + checkDailyUsage(); + if (!dailyUsage.agentUsed) { + dailyUsage.agentUsed = agentId; + saveDailyUsage(); + } + } +} + +// 增加智能体消息计数 +function incrementAgentMessage() { + if (!currentUser) { + checkDailyUsage(); + dailyUsage.agentMessages++; + saveDailyUsage(); + } +} + +// 获取剩余配额提示 +function getRemainingQuota() { + if (currentUser) return null; // 登录用户无限制 + + checkDailyUsage(); + + const chatSessionRemain = GUEST_LIMITS.maxChatSessionsPerDay - dailyUsage.chatSessions; + const chatMsgRemain = GUEST_LIMITS.maxChatMessagesPerDay - dailyUsage.chatMessages; + const agentMsgRemain = GUEST_LIMITS.maxAgentMessagesPerDay - dailyUsage.agentMessages; + + return { + chatSessionRemain, + chatMsgRemain, + agentMsgRemain, + agentUsed: dailyUsage.agentUsed + }; +} + // 获取使用智能体的对话列表(按时间倒序) function getAgentConversationHistory(limit = 5) { return conversations @@ -145,6 +305,21 @@ document.addEventListener('DOMContentLoaded', () => { pinnedAgents = JSON.parse(savedPinnedAgents); } + // 加载用户登录状态 + const savedUser = localStorage.getItem('currentUser'); + if (savedUser) { + currentUser = JSON.parse(savedUser); + } + + // 加载每日使用统计 + const savedDailyUsage = localStorage.getItem('dailyUsage'); + if (savedDailyUsage) { + dailyUsage = JSON.parse(savedDailyUsage); + } + + // 检查并重置每日使用统计 + checkDailyUsage(); + // 根据用户配置更新显示的智能体 updateAgentsDisplay(); @@ -1113,6 +1288,8 @@ function showAgentFavoritePage() { // ==================== 我的页面 ==================== function renderProfilePage() { + const quota = getRemainingQuota(); + return `