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:
@@ -116,3 +116,27 @@ export function getMessages(params) {
|
|||||||
params
|
params
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加智能体收藏
|
||||||
|
* @param {number} agentId - 智能体ID
|
||||||
|
*/
|
||||||
|
export function addFavorite(agentId) {
|
||||||
|
return request({
|
||||||
|
url: `${BASE_URL}/agent/favorite/create`,
|
||||||
|
method: 'post',
|
||||||
|
params: { agentId }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取消智能体收藏
|
||||||
|
* @param {number} agentId - 智能体ID
|
||||||
|
*/
|
||||||
|
export function removeFavorite(agentId) {
|
||||||
|
return request({
|
||||||
|
url: `${BASE_URL}/agent/favorite/delete`,
|
||||||
|
method: 'delete',
|
||||||
|
params: { agentId }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -134,6 +134,15 @@
|
|||||||
|
|
||||||
<!-- 底部操作栏 -->
|
<!-- 底部操作栏 -->
|
||||||
<div class="card-footer">
|
<div class="card-footer">
|
||||||
|
<button
|
||||||
|
class="favorite-btn"
|
||||||
|
:class="{ 'favorite-btn--active': agent.isFavorite }"
|
||||||
|
@click.stop="handleFavorite(agent)"
|
||||||
|
>
|
||||||
|
<StarFilled v-if="agent.isFavorite" />
|
||||||
|
<StarOutlined v-else />
|
||||||
|
<span>{{ agent.isFavorite ? '已收藏' : '收藏' }}</span>
|
||||||
|
</button>
|
||||||
<div class="footer-spacer"></div>
|
<div class="footer-spacer"></div>
|
||||||
<button class="chat-btn" @click.stop="handleChat(agent)">
|
<button class="chat-btn" @click.stop="handleChat(agent)">
|
||||||
<MessageOutlined class="chat-btn-icon" />
|
<MessageOutlined class="chat-btn-icon" />
|
||||||
@@ -172,12 +181,15 @@ import {
|
|||||||
CloseOutlined,
|
CloseOutlined,
|
||||||
ArrowRightOutlined,
|
ArrowRightOutlined,
|
||||||
MessageOutlined,
|
MessageOutlined,
|
||||||
AppstoreOutlined
|
AppstoreOutlined,
|
||||||
|
StarOutlined,
|
||||||
|
StarFilled
|
||||||
} from '@ant-design/icons-vue'
|
} from '@ant-design/icons-vue'
|
||||||
import { message } from 'ant-design-vue'
|
import { message } from 'ant-design-vue'
|
||||||
import FullWidthLayout from '@/layouts/components/FullWidthLayout.vue'
|
import FullWidthLayout from '@/layouts/components/FullWidthLayout.vue'
|
||||||
import ChatDrawer from '@/components/agents/ChatDrawer.vue'
|
import ChatDrawer from '@/components/agents/ChatDrawer.vue'
|
||||||
import { getAgentList } from '@/api/agent'
|
import { getAgentList, addFavorite, removeFavorite } from '@/api/agent'
|
||||||
|
import tokenManager from '@gold/utils/token-manager'
|
||||||
|
|
||||||
// 状态管理
|
// 状态管理
|
||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
@@ -218,7 +230,7 @@ const categories = computed(() => {
|
|||||||
return cats
|
return cats
|
||||||
})
|
})
|
||||||
|
|
||||||
// 过滤后的列表
|
// 过滤后的列表(收藏置顶)
|
||||||
const filteredAgentList = computed(() => {
|
const filteredAgentList = computed(() => {
|
||||||
let list = agentList.value
|
let list = agentList.value
|
||||||
|
|
||||||
@@ -234,7 +246,11 @@ const filteredAgentList = computed(() => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return list
|
// 收藏的智能体置顶
|
||||||
|
return list.sort((a, b) => {
|
||||||
|
if (a.isFavorite === b.isFavorite) return 0
|
||||||
|
return a.isFavorite ? -1 : 1
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// 获取智能体列表
|
// 获取智能体列表
|
||||||
@@ -250,7 +266,8 @@ const fetchAgentList = async () => {
|
|||||||
description: item.description,
|
description: item.description,
|
||||||
avatar: item.icon,
|
avatar: item.icon,
|
||||||
categoryName: item.categoryName || '其他',
|
categoryName: item.categoryName || '其他',
|
||||||
tagColor: getTagColor(item.categoryName)
|
tagColor: getTagColor(item.categoryName),
|
||||||
|
isFavorite: item.isFavorite || false
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -327,6 +344,30 @@ const handleSendMessage = (data) => {
|
|||||||
console.log('发送消息:', data)
|
console.log('发送消息:', data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 收藏切换
|
||||||
|
const handleFavorite = async (agent) => {
|
||||||
|
// 检查登录状态
|
||||||
|
if (!tokenManager.isLoggedIn()) {
|
||||||
|
message.warning('请先登录')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (agent.isFavorite) {
|
||||||
|
await removeFavorite(agent.id)
|
||||||
|
agent.isFavorite = false
|
||||||
|
message.success('已取消收藏')
|
||||||
|
} else {
|
||||||
|
await addFavorite(agent.id)
|
||||||
|
agent.isFavorite = true
|
||||||
|
message.success('收藏成功')
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('收藏操作失败:', error)
|
||||||
|
message.error('操作失败,请重试')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 初始化
|
// 初始化
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
fetchAgentList()
|
fetchAgentList()
|
||||||
@@ -799,6 +840,38 @@ onMounted(() => {
|
|||||||
font-size: 13px;
|
font-size: 13px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 收藏按钮
|
||||||
|
.favorite-btn {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 5px;
|
||||||
|
padding: 6px 12px;
|
||||||
|
background: transparent;
|
||||||
|
border: 1px solid var(--color-gray-300);
|
||||||
|
border-radius: 8px;
|
||||||
|
font-size: 12px;
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--color-gray-600);
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all 0.2s ease;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
border-color: #F59E0B;
|
||||||
|
color: #F59E0B;
|
||||||
|
background: #FFFBEB;
|
||||||
|
}
|
||||||
|
|
||||||
|
&:active {
|
||||||
|
transform: scale(0.95);
|
||||||
|
}
|
||||||
|
|
||||||
|
&--active {
|
||||||
|
border-color: #F59E0B;
|
||||||
|
color: #F59E0B;
|
||||||
|
background: #FFFBEB;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ============================================
|
// ============================================
|
||||||
// 空状态
|
// 空状态
|
||||||
// ============================================
|
// ============================================
|
||||||
|
|||||||
@@ -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.pojo.CommonResult;
|
||||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
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.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.service.AiAgentService;
|
||||||
import cn.iocoder.yudao.module.tik.muye.aiagent.vo.AppAiAgentRespVO;
|
import cn.iocoder.yudao.module.tik.muye.aiagent.vo.AppAiAgentRespVO;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.Parameter;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户 App - AI智能体
|
* 用户 App - AI智能体
|
||||||
*
|
|
||||||
* @author 芋道源码
|
|
||||||
*/
|
*/
|
||||||
@Tag(name = "用户 App - AI智能体")
|
@Tag(name = "用户 App - AI智能体")
|
||||||
@RestController
|
@RestController
|
||||||
@@ -31,11 +31,43 @@ public class AppAiAgentController {
|
|||||||
@Resource
|
@Resource
|
||||||
private AiAgentService aiAgentService;
|
private AiAgentService aiAgentService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private AiAgentFavoriteService aiAgentFavoriteService;
|
||||||
|
|
||||||
@GetMapping("/list")
|
@GetMapping("/list")
|
||||||
@Operation(summary = "获取启用的智能体列表")
|
@Operation(summary = "获取启用的智能体列表")
|
||||||
public CommonResult<List<AppAiAgentRespVO>> getEnabledAgentList() {
|
public CommonResult<List<AppAiAgentRespVO>> getEnabledAgentList() {
|
||||||
|
// 获取智能体列表
|
||||||
List<AiAgentDO> list = aiAgentService.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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -29,4 +29,7 @@ public class AppAiAgentRespVO {
|
|||||||
@Schema(description = "分类名称(中文)", example = "文案创作")
|
@Schema(description = "分类名称(中文)", example = "文案创作")
|
||||||
private String categoryName;
|
private String categoryName;
|
||||||
|
|
||||||
|
@Schema(description = "是否已收藏", example = "true")
|
||||||
|
private Boolean isFavorite;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user