feat: add agent favorite functionality with UI and API implementation

- Add `addFavorite` and `removeFavorite` API functions in agent.js
- Implement favorite button UI in Agents.vue with star icons
- Add login check before favorite operations using token manager
- Sort agents with favorites appearing at the top of the list
- Include success/error messages for user feedback
- Add backend API endpoints for creating/deleting agent favorites
- Update agent list response to include favorite status
- Style favorite button with hover and active states
This commit is contained in:
2026-02-26 20:15:24 +08:00
parent 120a4529a5
commit d429dc887a
8 changed files with 315 additions and 11 deletions

View File

@@ -2,25 +2,25 @@ package cn.iocoder.yudao.module.tik.muye.aiagent;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.tik.muye.aiagent.dal.AiAgentDO;
import cn.iocoder.yudao.module.tik.muye.aiagent.service.AiAgentFavoriteService;
import cn.iocoder.yudao.module.tik.muye.aiagent.service.AiAgentService;
import cn.iocoder.yudao.module.tik.muye.aiagent.vo.AppAiAgentRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Set;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
/**
* 用户 App - AI智能体
*
* @author 芋道源码
*/
@Tag(name = "用户 App - AI智能体")
@RestController
@@ -31,11 +31,43 @@ public class AppAiAgentController {
@Resource
private AiAgentService aiAgentService;
@Resource
private AiAgentFavoriteService aiAgentFavoriteService;
@GetMapping("/list")
@Operation(summary = "获取启用的智能体列表")
public CommonResult<List<AppAiAgentRespVO>> getEnabledAgentList() {
// 获取智能体列表
List<AiAgentDO> list = aiAgentService.getEnabledAgentList();
return success(BeanUtils.toBean(list, AppAiAgentRespVO.class));
List<AppAiAgentRespVO> voList = BeanUtils.toBean(list, AppAiAgentRespVO.class);
// 获取当前用户收藏的智能体ID
Long userId = SecurityFrameworkUtils.getLoginUserId();
if (userId != null) {
Set<Long> favoriteIds = aiAgentFavoriteService.getFavoriteAgentIds(userId);
voList.forEach(vo -> vo.setIsFavorite(favoriteIds.contains(vo.getId())));
} else {
voList.forEach(vo -> vo.setIsFavorite(false));
}
return success(voList);
}
@PostMapping("/favorite/create")
@Operation(summary = "添加收藏")
@Parameter(name = "agentId", description = "智能体ID", required = true)
public CommonResult<Long> createFavorite(@RequestParam("agentId") Long agentId) {
Long userId = SecurityFrameworkUtils.getLoginUserId();
return success(aiAgentFavoriteService.createFavorite(userId, agentId));
}
@DeleteMapping("/favorite/delete")
@Operation(summary = "取消收藏")
@Parameter(name = "agentId", description = "智能体ID", required = true)
public CommonResult<Boolean> deleteFavorite(@RequestParam("agentId") Long agentId) {
Long userId = SecurityFrameworkUtils.getLoginUserId();
aiAgentFavoriteService.deleteFavorite(userId, agentId);
return success(true);
}
}

View File

@@ -0,0 +1,34 @@
package cn.iocoder.yudao.module.tik.muye.aiagent.dal;
import lombok.*;
import com.baomidou.mybatisplus.annotation.*;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
/**
* AI智能体收藏 DO
*/
@TableName("muye_ai_agent_favorite")
@KeySequence("muye_ai_agent_favorite_seq")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AiAgentFavoriteDO extends BaseDO {
/**
* 主键
*/
@TableId
private Long id;
/**
* 用户ID
*/
private Long userId;
/**
* 智能体ID
*/
private Long agentId;
}

View File

@@ -0,0 +1,21 @@
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.Mapper;
import java.util.List;
@Mapper
public interface AiAgentFavoriteMapper extends BaseMapperX<AiAgentFavoriteDO> {
default AiAgentFavoriteDO selectByUserIdAndAgentId(Long userId, Long agentId) {
return selectOne(AiAgentFavoriteDO::getUserId, userId,
AiAgentFavoriteDO::getAgentId, agentId);
}
default List<AiAgentFavoriteDO> selectListByUserId(Long userId) {
return selectList(AiAgentFavoriteDO::getUserId, userId);
}
}

View File

@@ -0,0 +1,47 @@
package cn.iocoder.yudao.module.tik.muye.aiagent.service;
import cn.iocoder.yudao.module.tik.muye.aiagent.dal.AiAgentFavoriteDO;
import java.util.List;
import java.util.Set;
/**
* AI智能体收藏 Service 接口
*/
public interface AiAgentFavoriteService {
/**
* 添加收藏
*
* @param userId 用户ID
* @param agentId 智能体ID
* @return 收藏ID
*/
Long createFavorite(Long userId, Long agentId);
/**
* 取消收藏
*
* @param userId 用户ID
* @param agentId 智能体ID
*/
void deleteFavorite(Long userId, Long agentId);
/**
* 获取用户收藏的智能体ID列表
*
* @param userId 用户ID
* @return 智能体ID集合
*/
Set<Long> getFavoriteAgentIds(Long userId);
/**
* 判断是否已收藏
*
* @param userId 用户ID
* @param agentId 智能体ID
* @return 是否已收藏
*/
boolean isFavorite(Long userId, Long agentId);
}

View File

@@ -0,0 +1,70 @@
package cn.iocoder.yudao.module.tik.muye.aiagent.service;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.tik.muye.aiagent.dal.AiAgentFavoriteDO;
import cn.iocoder.yudao.module.tik.muye.aiagent.mapper.AiAgentFavoriteMapper;
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;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
/**
* AI智能体收藏 Service 实现类
*/
@Service
@Validated
public class AiAgentFavoriteServiceImpl implements AiAgentFavoriteService {
private static final ErrorCode FAVORITE_EXISTS = new ErrorCode(1_013_001_000, "已收藏该智能体");
private static final ErrorCode FAVORITE_NOT_EXISTS = new ErrorCode(1_013_001_001, "未收藏该智能体");
@Resource
private AiAgentFavoriteMapper aiAgentFavoriteMapper;
@Override
public Long createFavorite(Long userId, Long agentId) {
// 检查是否已收藏
AiAgentFavoriteDO existFavorite = aiAgentFavoriteMapper.selectByUserIdAndAgentId(userId, agentId);
if (existFavorite != null) {
throw exception(FAVORITE_EXISTS);
}
// 创建收藏
AiAgentFavoriteDO favorite = AiAgentFavoriteDO.builder()
.userId(userId)
.agentId(agentId)
.build();
aiAgentFavoriteMapper.insert(favorite);
return favorite.getId();
}
@Override
public void deleteFavorite(Long userId, Long agentId) {
AiAgentFavoriteDO favorite = aiAgentFavoriteMapper.selectByUserIdAndAgentId(userId, agentId);
if (favorite == null) {
throw exception(FAVORITE_NOT_EXISTS);
}
aiAgentFavoriteMapper.deleteById(favorite.getId());
}
@Override
public Set<Long> getFavoriteAgentIds(Long userId) {
List<AiAgentFavoriteDO> favorites = aiAgentFavoriteMapper.selectListByUserId(userId);
Set<Long> agentIds = new HashSet<>();
for (AiAgentFavoriteDO favorite : favorites) {
agentIds.add(favorite.getAgentId());
}
return agentIds;
}
@Override
public boolean isFavorite(Long userId, Long agentId) {
return aiAgentFavoriteMapper.selectByUserIdAndAgentId(userId, agentId) != null;
}
}

View File

@@ -29,4 +29,7 @@ public class AppAiAgentRespVO {
@Schema(description = "分类名称(中文)", example = "文案创作")
private String categoryName;
@Schema(description = "是否已收藏", example = "true")
private Boolean isFavorite;
}