feat(playlist): 增加歌单收藏状态检查功能
- 新增 isPlaylistCollected 接口和相关实现 - 修改 toggleCollectPlaylist 函数,增加当前收藏状态参数 - 在前端 PlaylistDetail 组件中添加收藏状态检查逻辑 - 后端增加对应的控制层、服务层和实现类方法
This commit is contained in:
@@ -189,6 +189,19 @@ public class PlaylistController {
|
|||||||
return Result.success("取消收藏成功", result);
|
return Result.success("取消收藏成功", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查当前用户是否收藏了该歌单
|
||||||
|
*
|
||||||
|
* @param id 歌单ID
|
||||||
|
* @return 响应结果
|
||||||
|
*/
|
||||||
|
@SaCheckLogin
|
||||||
|
@GetMapping("/{id}/is-collected")
|
||||||
|
public Result<Boolean> isCollected(@PathVariable Long id) {
|
||||||
|
boolean result = playlistService.isCollected(id);
|
||||||
|
return Result.success(result);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取用户收藏的歌单列表
|
* 获取用户收藏的歌单列表
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -175,6 +175,19 @@ public class SingerController {
|
|||||||
return Result.success("取消收藏成功", result);
|
return Result.success("取消收藏成功", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查当前用户是否收藏了该歌手
|
||||||
|
*
|
||||||
|
* @param id 歌手ID
|
||||||
|
* @return 响应结果
|
||||||
|
*/
|
||||||
|
@SaCheckLogin
|
||||||
|
@GetMapping("/{id}/is-collected")
|
||||||
|
public Result<Boolean> isCollected(@PathVariable Long id) {
|
||||||
|
boolean result = singerService.isCollected(id);
|
||||||
|
return Result.success(result);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取用户收藏的歌手列表
|
* 获取用户收藏的歌手列表
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -99,6 +99,14 @@ public interface PlaylistService {
|
|||||||
*/
|
*/
|
||||||
boolean uncollectPlaylist(Long id);
|
boolean uncollectPlaylist(Long id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查当前用户是否收藏了该歌单
|
||||||
|
*
|
||||||
|
* @param id 歌单ID
|
||||||
|
* @return 是否收藏
|
||||||
|
*/
|
||||||
|
boolean isCollected(Long id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取用户收藏的歌单列表
|
* 获取用户收藏的歌单列表
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -90,6 +90,14 @@ public interface SingerService {
|
|||||||
*/
|
*/
|
||||||
boolean uncollectSinger(Long id);
|
boolean uncollectSinger(Long id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查当前用户是否收藏了该歌手
|
||||||
|
*
|
||||||
|
* @param id 歌手ID
|
||||||
|
* @return 是否收藏
|
||||||
|
*/
|
||||||
|
boolean isCollected(Long id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取用户收藏的歌手列表
|
* 获取用户收藏的歌手列表
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -247,6 +247,7 @@ public class PlaylistServiceImpl implements PlaylistService {
|
|||||||
playlist.setCover(coverUrl);
|
playlist.setCover(coverUrl);
|
||||||
playlist.setCreator(user);
|
playlist.setCreator(user);
|
||||||
playlist.setPlayCount(0L);
|
playlist.setPlayCount(0L);
|
||||||
|
playlist.setCollectCount(0);
|
||||||
playlist.setIsOfficial(0); // 默认为非官方歌单
|
playlist.setIsOfficial(0); // 默认为非官方歌单
|
||||||
|
|
||||||
// 设置歌单类型
|
// 设置歌单类型
|
||||||
@@ -358,7 +359,7 @@ public class PlaylistServiceImpl implements PlaylistService {
|
|||||||
userPlaylistRepository.collectPlaylist(user.getId(), playlist.getId());
|
userPlaylistRepository.collectPlaylist(user.getId(), playlist.getId());
|
||||||
|
|
||||||
// 更新收藏数
|
// 更新收藏数
|
||||||
playlist.setCollectCount(playlist.getCollectCount() + 1);
|
playlist.setCollectCount((playlist.getCollectCount() == null ? 0 : playlist.getCollectCount()) + 1);
|
||||||
playlistRepository.save(playlist);
|
playlistRepository.save(playlist);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -385,12 +386,21 @@ public class PlaylistServiceImpl implements PlaylistService {
|
|||||||
userPlaylistRepository.uncollectPlaylist(user.getId(), playlist.getId());
|
userPlaylistRepository.uncollectPlaylist(user.getId(), playlist.getId());
|
||||||
|
|
||||||
// 更新收藏数
|
// 更新收藏数
|
||||||
playlist.setCollectCount(playlist.getCollectCount() - 1);
|
playlist.setCollectCount((playlist.getCollectCount() == null ? 0 : playlist.getCollectCount()) - 1);
|
||||||
playlistRepository.save(playlist);
|
playlistRepository.save(playlist);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCollected(Long id) {
|
||||||
|
if (!StpUtil.isLogin()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Long userId = StpUtil.getLoginIdAsLong();
|
||||||
|
return userPlaylistRepository.existsByUserIdAndPlaylistId(userId, id);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PageResult<PlaylistDTO> getCollectedPlaylist(Integer page, Integer size) {
|
public PageResult<PlaylistDTO> getCollectedPlaylist(Integer page, Integer size) {
|
||||||
// 获取当前登录用户
|
// 获取当前登录用户
|
||||||
|
|||||||
@@ -214,6 +214,7 @@ public class SingerServiceImpl implements SingerService {
|
|||||||
singer.setName(singerDTO.getName());
|
singer.setName(singerDTO.getName());
|
||||||
singer.setAvatar(avatarUrl);
|
singer.setAvatar(avatarUrl);
|
||||||
singer.setIntroduction(singerDTO.getIntroduction());
|
singer.setIntroduction(singerDTO.getIntroduction());
|
||||||
|
singer.setCollectCount(0);
|
||||||
|
|
||||||
// 设置歌手类型
|
// 设置歌手类型
|
||||||
if (singerDTO.getType() != null && singerDTO.getType().getId() != null) {
|
if (singerDTO.getType() != null && singerDTO.getType().getId() != null) {
|
||||||
@@ -309,7 +310,7 @@ public class SingerServiceImpl implements SingerService {
|
|||||||
userSingerRepository.collectSinger(user.getId(), singer.getId());
|
userSingerRepository.collectSinger(user.getId(), singer.getId());
|
||||||
|
|
||||||
// 更新收藏数
|
// 更新收藏数
|
||||||
singer.setCollectCount(singer.getCollectCount() + 1);
|
singer.setCollectCount((singer.getCollectCount() == null ? 0 : singer.getCollectCount()) + 1);
|
||||||
singerRepository.save(singer);
|
singerRepository.save(singer);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -336,12 +337,21 @@ public class SingerServiceImpl implements SingerService {
|
|||||||
userSingerRepository.uncollectSinger(user.getId(), singer.getId());
|
userSingerRepository.uncollectSinger(user.getId(), singer.getId());
|
||||||
|
|
||||||
// 更新收藏数
|
// 更新收藏数
|
||||||
singer.setCollectCount(singer.getCollectCount() - 1);
|
singer.setCollectCount((singer.getCollectCount() == null ? 0 : singer.getCollectCount()) - 1);
|
||||||
singerRepository.save(singer);
|
singerRepository.save(singer);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCollected(Long id) {
|
||||||
|
if (!StpUtil.isLogin()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Long userId = StpUtil.getLoginIdAsLong();
|
||||||
|
return userSingerRepository.existsByUserIdAndSingerId(userId, id);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PageResult<SingerDTO> getCollectedSinger(Integer page, Integer size) {
|
public PageResult<SingerDTO> getCollectedSinger(Integer page, Integer size) {
|
||||||
// 获取当前登录用户
|
// 获取当前登录用户
|
||||||
|
|||||||
@@ -289,11 +289,25 @@ export function deletePlaylist(id) {
|
|||||||
/**
|
/**
|
||||||
* 收藏/取消收藏歌单
|
* 收藏/取消收藏歌单
|
||||||
* @param {number} id 歌单ID
|
* @param {number} id 歌单ID
|
||||||
|
* @param {boolean} isCollected 当前是否收藏
|
||||||
* @returns {Promise<any>}
|
* @returns {Promise<any>}
|
||||||
*/
|
*/
|
||||||
export function toggleCollectPlaylist(id) {
|
export function toggleCollectPlaylist(id, isCollected) {
|
||||||
|
if (isCollected) {
|
||||||
|
return api.delete(`/playlist/${id}/collect`)
|
||||||
|
} else {
|
||||||
return api.post(`/playlist/${id}/collect`)
|
return api.post(`/playlist/${id}/collect`)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查当前用户是否收藏了该歌单
|
||||||
|
* @param {number} id 歌单ID
|
||||||
|
* @returns {Promise<any>}
|
||||||
|
*/
|
||||||
|
export function isPlaylistCollected(id) {
|
||||||
|
return api.get(`/playlist/${id}/is-collected`)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 向歌单添加音乐
|
* 向歌单添加音乐
|
||||||
|
|||||||
@@ -203,24 +203,24 @@ export function getHotSinger(limit = 10) {
|
|||||||
/**
|
/**
|
||||||
* 收藏/取消收藏歌手
|
* 收藏/取消收藏歌手
|
||||||
* @param {number} id 歌手ID
|
* @param {number} id 歌手ID
|
||||||
|
* @param {boolean} isCollected 当前是否收藏
|
||||||
* @returns {Promise<any>}
|
* @returns {Promise<any>}
|
||||||
*/
|
*/
|
||||||
export function toggleCollectSinger(id) {
|
export function toggleCollectSinger(id, isCollected) {
|
||||||
// 检查是否已收藏
|
|
||||||
return api.get(`/singer/${id}`).then(res => {
|
|
||||||
if (res.code === 200) {
|
|
||||||
const isCollected = res.data.isCollected === true
|
|
||||||
|
|
||||||
// 根据当前收藏状态决定调用收藏或取消收藏接口
|
|
||||||
if (isCollected) {
|
if (isCollected) {
|
||||||
return api.delete(`/singer/${id}/collect`)
|
return api.delete(`/singer/${id}/collect`)
|
||||||
} else {
|
} else {
|
||||||
return api.post(`/singer/${id}/collect`)
|
return api.post(`/singer/${id}/collect`)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return Promise.reject(new Error(res.message || '获取歌手信息失败'))
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
/**
|
||||||
|
* 检查当前用户是否收藏了该歌手
|
||||||
|
* @param {number} id 歌手ID
|
||||||
|
* @returns {Promise<any>}
|
||||||
|
*/
|
||||||
|
export function isSingerCollected(id) {
|
||||||
|
return api.get(`/singer/${id}/is-collected`)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { useRoute, useRouter } from 'vue-router'
|
|||||||
import { usePlayerStore } from '../stores/player'
|
import { usePlayerStore } from '../stores/player'
|
||||||
import { useUserStore } from '../stores/user'
|
import { useUserStore } from '../stores/user'
|
||||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||||
import { getPlaylistById, toggleCollectPlaylist, deletePlaylist as apiDeletePlaylist } from '../api/playlist'
|
import { getPlaylistById, toggleCollectPlaylist, deletePlaylist as apiDeletePlaylist, isPlaylistCollected } from '../api/playlist'
|
||||||
import { getMusicStatus } from '../api/music'
|
import { getMusicStatus } from '../api/music'
|
||||||
import { getCommentList, addComment, deleteComment as apiDeleteComment } from '../api/comment'
|
import { getCommentList, addComment, deleteComment as apiDeleteComment } from '../api/comment'
|
||||||
import { connect, subscribe, unsubscribe, disconnect } from '../utils/websocket' // 导入 WebSocket 工具
|
import { connect, subscribe, unsubscribe, disconnect } from '../utils/websocket' // 导入 WebSocket 工具
|
||||||
@@ -67,8 +67,10 @@ const fetchPlaylistDetail = async () => {
|
|||||||
// 判断是否是创建者
|
// 判断是否是创建者
|
||||||
isCreator.value = playlist.value.creator?.id === userStore.user?.id
|
isCreator.value = playlist.value.creator?.id === userStore.user?.id
|
||||||
|
|
||||||
// 获取收藏状态
|
// 如果用户已登录,则检查是否收藏
|
||||||
isCollected.value = playlist.value.collected === true
|
if (userStore.isLoggedIn) {
|
||||||
|
checkIfCollected()
|
||||||
|
}
|
||||||
|
|
||||||
// 更新歌单内歌曲的状态
|
// 更新歌单内歌曲的状态
|
||||||
updateMusicStatuses(playlist.value.songs)
|
updateMusicStatuses(playlist.value.songs)
|
||||||
@@ -83,6 +85,18 @@ const fetchPlaylistDetail = async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查是否收藏
|
||||||
|
const checkIfCollected = async () => {
|
||||||
|
try {
|
||||||
|
const res = await isPlaylistCollected(playlistId.value)
|
||||||
|
if (res.code === 200) {
|
||||||
|
isCollected.value = res.data
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('检查收藏状态失败:', error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 获取评论列表
|
// 获取评论列表
|
||||||
const fetchComments = async () => {
|
const fetchComments = async () => {
|
||||||
try {
|
try {
|
||||||
@@ -139,7 +153,7 @@ const toggleCollect = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await toggleCollectPlaylist(playlistId.value)
|
const res = await toggleCollectPlaylist(playlistId.value, isCollected.value)
|
||||||
if (res.code === 200) {
|
if (res.code === 200) {
|
||||||
isCollected.value = !isCollected.value
|
isCollected.value = !isCollected.value
|
||||||
// 更新收藏数
|
// 更新收藏数
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { useRoute, useRouter } from 'vue-router'
|
|||||||
import { usePlayerStore } from '../stores/player'
|
import { usePlayerStore } from '../stores/player'
|
||||||
import { useUserStore } from '../stores/user'
|
import { useUserStore } from '../stores/user'
|
||||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||||
import { getSingerById, toggleCollectSinger } from '../api/singer'
|
import { getSingerById, isSingerCollected, collectSinger, uncollectSinger } from '../api/singer'
|
||||||
import { getCommentList, addComment, deleteComment as apiDeleteComment } from '../api/comment'
|
import { getCommentList, addComment, deleteComment as apiDeleteComment } from '../api/comment'
|
||||||
import { processImageUrl } from '../utils/imageUtils'
|
import { processImageUrl } from '../utils/imageUtils'
|
||||||
import { processMusicUrl, getMusicBySinger, getMusicStatus } from '../api/music'
|
import { processMusicUrl, getMusicBySinger, getMusicStatus } from '../api/music'
|
||||||
@@ -67,8 +67,10 @@ const fetchSingerDetail = async () => {
|
|||||||
// 获取歌手的音乐列表
|
// 获取歌手的音乐列表
|
||||||
await fetchSingerMusic()
|
await fetchSingerMusic()
|
||||||
|
|
||||||
// 获取收藏状态
|
// 如果用户已登录,则检查是否收藏
|
||||||
isCollected.value = singer.value.collected === true
|
if (userStore.isLoggedIn) {
|
||||||
|
checkIfCollected()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
ElMessage.error(res.message || '获取歌手详情失败')
|
ElMessage.error(res.message || '获取歌手详情失败')
|
||||||
}
|
}
|
||||||
@@ -80,6 +82,18 @@ const fetchSingerDetail = async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查是否收藏
|
||||||
|
const checkIfCollected = async () => {
|
||||||
|
try {
|
||||||
|
const res = await isSingerCollected(singerId.value)
|
||||||
|
if (res.code === 200) {
|
||||||
|
isCollected.value = res.data
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('检查收藏状态失败:', error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 获取歌手的音乐列表
|
// 获取歌手的音乐列表
|
||||||
const fetchSingerMusic = async () => {
|
const fetchSingerMusic = async () => {
|
||||||
try {
|
try {
|
||||||
@@ -181,7 +195,7 @@ const toggleCollect = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await toggleCollectSinger(singerId.value)
|
const res = await toggleCollectSinger(singerId.value, isCollected.value)
|
||||||
if (res.code === 200) {
|
if (res.code === 200) {
|
||||||
isCollected.value = !isCollected.value
|
isCollected.value = !isCollected.value
|
||||||
// 更新收藏数
|
// 更新收藏数
|
||||||
|
|||||||
Reference in New Issue
Block a user