From e725335a6f8f439f846a4e3539b1fb39d2c98885 Mon Sep 17 00:00:00 2001 From: sion123 <450702724@qq.com> Date: Wed, 4 Mar 2026 19:47:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/web-gold/src/views/agents/Agents.vue | 31 ++++++------------- .../aiagent/mapper/AiAgentFavoriteMapper.java | 15 +++++++++ .../service/AiAgentFavoriteServiceImpl.java | 20 +++--------- 3 files changed, 30 insertions(+), 36 deletions(-) diff --git a/frontend/app/web-gold/src/views/agents/Agents.vue b/frontend/app/web-gold/src/views/agents/Agents.vue index 6f5c38e09e..f79424c3b5 100644 --- a/frontend/app/web-gold/src/views/agents/Agents.vue +++ b/frontend/app/web-gold/src/views/agents/Agents.vue @@ -203,7 +203,6 @@ import FullWidthLayout from '@/layouts/components/FullWidthLayout.vue' import ChatDrawer from '@/components/agents/ChatDrawer.vue' import HistoryPanel from '@/components/agents/HistoryPanel.vue' import { getAgentList, addFavorite, removeFavorite } from '@/api/agent' -import tokenManager from '@gold/utils/token-manager' // 状态管理 const loading = ref(false) @@ -357,11 +356,6 @@ const handleChat = (agent) => { } const handleHistory = (agent) => { - // 检查登录状态 - if (!tokenManager.isLoggedIn()) { - message.warning('请先登录') - return - } historyAgentId.value = agent.id historyPanelVisible.value = true } @@ -374,27 +368,22 @@ const handleSendMessage = (data) => { console.log('发送消息:', data) } -// 收藏切换 +// 收藏切换(乐观更新) const handleFavorite = async (agent) => { - // 检查登录状态 - if (!tokenManager.isLoggedIn()) { - message.warning('请先登录') - return - } + const newStatus = !agent.isFavorite + agent.isFavorite = newStatus // 立即更新 UI try { - if (agent.isFavorite) { - await removeFavorite(agent.id) - agent.isFavorite = false - message.success('已取消收藏') - } else { + if (newStatus) { await addFavorite(agent.id) - agent.isFavorite = true message.success('收藏成功') + } else { + await removeFavorite(agent.id) + message.success('已取消收藏') } - } catch (error) { - console.error('收藏操作失败:', error) - message.error('操作失败,请重试') + } catch { + agent.isFavorite = !newStatus // 失败时回滚 + message.error('操作失败') } } diff --git a/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/muye/aiagent/mapper/AiAgentFavoriteMapper.java b/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/muye/aiagent/mapper/AiAgentFavoriteMapper.java index a7ff130ef5..593b97c977 100644 --- a/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/muye/aiagent/mapper/AiAgentFavoriteMapper.java +++ b/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/muye/aiagent/mapper/AiAgentFavoriteMapper.java @@ -2,7 +2,10 @@ package cn.iocoder.yudao.module.tik.muye.aiagent.mapper; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.module.tik.muye.aiagent.dal.AiAgentFavoriteDO; +import org.apache.ibatis.annotations.Delete; +import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; import java.util.List; @@ -18,4 +21,16 @@ public interface AiAgentFavoriteMapper extends BaseMapperX { return selectList(AiAgentFavoriteDO::getUserId, userId); } + /** + * 插入收藏(幂等:重复时更新时间) + */ + @Insert("INSERT INTO muye_ai_agent_favorite (user_id, agent_id, creator, create_time, updater, update_time, deleted, tenant_id) VALUES (#{userId}, #{agentId}, #{userId}, NOW(), #{userId}, NOW(), 0, 1) ON DUPLICATE KEY UPDATE update_time = NOW(), deleted = 0") + void insertOrUpdate(@Param("userId") Long userId, @Param("agentId") Long agentId); + + /** + * 物理删除收藏记录(绕过逻辑删除) + */ + @Delete("DELETE FROM muye_ai_agent_favorite WHERE id = #{id}") + void deletePhysicsById(@Param("id") Long id); + } diff --git a/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/muye/aiagent/service/AiAgentFavoriteServiceImpl.java b/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/muye/aiagent/service/AiAgentFavoriteServiceImpl.java index 75eced8f95..0f754dc39a 100644 --- a/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/muye/aiagent/service/AiAgentFavoriteServiceImpl.java +++ b/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/muye/aiagent/service/AiAgentFavoriteServiceImpl.java @@ -7,7 +7,6 @@ import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; -import java.util.HashSet; import java.util.List; import java.util.Set; @@ -27,19 +26,10 @@ public class AiAgentFavoriteServiceImpl implements AiAgentFavoriteService { @Override public Long createFavorite(Long userId, Long agentId) { - // 检查是否已收藏(幂等:已存在则直接返回) - AiAgentFavoriteDO existFavorite = aiAgentFavoriteMapper.selectByUserIdAndAgentId(userId, agentId); - if (existFavorite != null) { - return existFavorite.getId(); - } - - // 创建收藏 - AiAgentFavoriteDO favorite = AiAgentFavoriteDO.builder() - .userId(userId) - .agentId(agentId) - .build(); - aiAgentFavoriteMapper.insert(favorite); - return favorite.getId(); + // 幂等插入:重复时更新时间 + aiAgentFavoriteMapper.insertOrUpdate(userId, agentId); + AiAgentFavoriteDO favorite = aiAgentFavoriteMapper.selectByUserIdAndAgentId(userId, agentId); + return favorite != null ? favorite.getId() : null; } @Override @@ -48,7 +38,7 @@ public class AiAgentFavoriteServiceImpl implements AiAgentFavoriteService { if (favorite == null) { throw exception(FAVORITE_NOT_EXISTS); } - aiAgentFavoriteMapper.deleteById(favorite.getId()); + aiAgentFavoriteMapper.deletePhysicsById(favorite.getId()); } @Override