feat: 初始化音乐项目后台管理系统- 新增后台管理相关页面和功能组件
- 实现管理员评论管理、数据统计等功能 - 添加后台布局和路由管理 -配置数据库和Redis连接 - 实现文件上传和访问路径配置 - 添加Sa-Token安全配置
This commit is contained in:
576
music-qianduan/src/views/SingerDetail.vue
Normal file
576
music-qianduan/src/views/SingerDetail.vue
Normal file
@@ -0,0 +1,576 @@
|
||||
<script setup>
|
||||
import { ref, onMounted, computed, onUnmounted } from 'vue' // 添加 onUnmounted
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { usePlayerStore } from '../stores/player'
|
||||
import { useUserStore } from '../stores/user'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { getSingerById, toggleCollectSinger } from '../api/singer'
|
||||
import { getCommentList, addComment } from '../api/comment'
|
||||
import { processImageUrl } from '../utils/imageUtils'
|
||||
import { processMusicUrl, getMusicBySinger } from '../api/music'
|
||||
import { connect, subscribe, unsubscribe, disconnect } from '../utils/websocket' // 导入 WebSocket 工具
|
||||
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
const playerStore = usePlayerStore()
|
||||
const userStore = useUserStore()
|
||||
|
||||
// 歌手ID
|
||||
const singerId = computed(() => route.params.id)
|
||||
|
||||
// 歌手详情
|
||||
const singer = ref(null)
|
||||
|
||||
// 歌手的音乐列表
|
||||
const songs = ref([])
|
||||
|
||||
// 歌手的音乐列表
|
||||
|
||||
// 评论列表
|
||||
const comments = ref([])
|
||||
|
||||
// 评论内容
|
||||
const commentContent = ref('')
|
||||
|
||||
// 加载状态
|
||||
const loading = ref(false)
|
||||
|
||||
// 收藏状态
|
||||
const isCollected = ref(false)
|
||||
|
||||
// 获取歌手详情
|
||||
const fetchSingerDetail = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await getSingerById(singerId.value)
|
||||
if (res.code === 200) {
|
||||
singer.value = res.data
|
||||
|
||||
// 处理头像图片URL
|
||||
if (singer.value.avatar) {
|
||||
singer.value.avatar = processImageUrl(singer.value.avatar)
|
||||
}
|
||||
|
||||
// 获取歌手的音乐列表
|
||||
await fetchSingerMusic()
|
||||
|
||||
// 获取收藏状态
|
||||
isCollected.value = singer.value.collected === true
|
||||
} else {
|
||||
ElMessage.error(res.message || '获取歌手详情失败')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取歌手详情失败:', error)
|
||||
ElMessage.error('获取歌手详情失败')
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 获取歌手的音乐列表
|
||||
const fetchSingerMusic = async () => {
|
||||
try {
|
||||
const res = await getMusicBySinger(singerId.value, 1, 20)
|
||||
if (res.code === 200) {
|
||||
songs.value = res.data.list || res.data.records || []
|
||||
|
||||
// 处理音乐URL和封面URL
|
||||
songs.value = songs.value.map(song => ({
|
||||
...song,
|
||||
cover: processMusicUrl(song.cover),
|
||||
url: processMusicUrl(song.url)
|
||||
}))
|
||||
} else {
|
||||
ElMessage.error(res.message || '获取歌手音乐失败')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取歌手音乐失败:', error)
|
||||
ElMessage.error('获取歌手音乐失败')
|
||||
}
|
||||
}
|
||||
|
||||
// 获取评论列表
|
||||
const fetchComments = async () => {
|
||||
try {
|
||||
const res = await getCommentList(singerId.value, 3, 1, 20) // targetType 3 表示歌手
|
||||
if (res.code === 200) {
|
||||
comments.value = res.data.list || res.data.records || []
|
||||
|
||||
// 处理用户头像
|
||||
comments.value.forEach(comment => {
|
||||
if (comment.user && comment.user.avatar) {
|
||||
comment.user.avatar = processImageUrl(comment.user.avatar)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
ElMessage.error(res.message || '获取评论列表失败')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取评论列表失败:', error)
|
||||
ElMessage.error('获取评论列表失败')
|
||||
}
|
||||
}
|
||||
|
||||
// 播放全部
|
||||
const playAll = () => {
|
||||
if (songs.value.length === 0) {
|
||||
ElMessage.warning('没有可播放的音乐')
|
||||
return
|
||||
}
|
||||
|
||||
// 清空当前播放列表
|
||||
playerStore.clearPlaylist()
|
||||
|
||||
// 添加所有音乐到播放列表
|
||||
songs.value.forEach(song => {
|
||||
const musicObj = {
|
||||
...song,
|
||||
singer: singer.value.name
|
||||
}
|
||||
playerStore.addToPlaylist(musicObj)
|
||||
})
|
||||
|
||||
// 播放第一首
|
||||
playerStore.setCurrentMusic({
|
||||
...songs.value[0],
|
||||
singer: singer.value.name
|
||||
})
|
||||
|
||||
ElMessage.success('开始播放全部歌曲')
|
||||
}
|
||||
|
||||
// 播放音乐
|
||||
const playMusic = (song) => {
|
||||
playerStore.setCurrentMusic({
|
||||
...song,
|
||||
singer: singer.value.name
|
||||
})
|
||||
}
|
||||
|
||||
// 添加到播放列表
|
||||
const addToPlaylist = (song) => {
|
||||
playerStore.addToPlaylist({
|
||||
...song,
|
||||
singer: singer.value.name
|
||||
})
|
||||
ElMessage.success('已添加到播放列表')
|
||||
}
|
||||
|
||||
// 收藏/取消收藏歌手
|
||||
const toggleCollect = async () => {
|
||||
if (!userStore.isLoggedIn) {
|
||||
ElMessage.warning('请先登录')
|
||||
router.push('/login')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await toggleCollectSinger(singerId.value)
|
||||
if (res.code === 200) {
|
||||
isCollected.value = !isCollected.value
|
||||
ElMessage.success(isCollected.value ? '收藏成功' : '已取消收藏')
|
||||
} else {
|
||||
ElMessage.error(res.message || '操作失败')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('收藏操作失败:', error)
|
||||
ElMessage.error('操作失败,请稍后重试')
|
||||
}
|
||||
}
|
||||
|
||||
// 提交评论
|
||||
const submitComment = async () => {
|
||||
if (!userStore.isLoggedIn) {
|
||||
ElMessage.warning('请先登录')
|
||||
router.push('/login')
|
||||
return
|
||||
}
|
||||
|
||||
if (!commentContent.value.trim()) {
|
||||
ElMessage.warning('评论内容不能为空')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await addComment({
|
||||
targetId: singerId.value,
|
||||
targetType: 3, // 3表示歌手
|
||||
content: commentContent.value
|
||||
})
|
||||
|
||||
if (res.code === 200) {
|
||||
// 添加成功,评论将通过 WebSocket 推送,无需手动刷新
|
||||
// await fetchComments() // 移除手动刷新
|
||||
commentContent.value = ''
|
||||
ElMessage.success('评论已发送') // 提示语可以调整
|
||||
} else {
|
||||
ElMessage.error(res.message || '评论失败')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('提交评论失败:', error)
|
||||
ElMessage.error('评论失败,请稍后重试')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 格式化时间
|
||||
const formatTime = (seconds) => {
|
||||
const mins = Math.floor(seconds / 60)
|
||||
const secs = Math.floor(seconds % 60)
|
||||
return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`
|
||||
}
|
||||
|
||||
// 格式化粉丝数
|
||||
const formatFans = (count) => {
|
||||
if (count < 10000) {
|
||||
return count.toString()
|
||||
} else if (count < 100000000) {
|
||||
return (count / 10000).toFixed(1) + '万'
|
||||
} else {
|
||||
return (count / 100000000).toFixed(1) + '亿'
|
||||
}
|
||||
}
|
||||
|
||||
// 获取歌手类型名称
|
||||
const getSingerTypeName = (type) => {
|
||||
const typeMap = {
|
||||
1: '华语男歌手',
|
||||
2: '华语女歌手',
|
||||
3: '华语组合',
|
||||
4: '欧美男歌手',
|
||||
5: '欧美女歌手',
|
||||
6: '欧美组合',
|
||||
7: '日韩男歌手',
|
||||
8: '日韩女歌手',
|
||||
9: '日韩组合'
|
||||
}
|
||||
return typeMap[type] || '未知'
|
||||
}
|
||||
|
||||
// WebSocket 订阅实例
|
||||
let commentSubscription = null;
|
||||
|
||||
onMounted(() => {
|
||||
fetchSingerDetail()
|
||||
fetchComments()
|
||||
|
||||
// 连接 WebSocket 并订阅评论
|
||||
connect(() => {
|
||||
const destination = `/topic/comments/3/${singerId.value}`; // 3 代表歌手类型
|
||||
commentSubscription = subscribe(destination, (newComment) => {
|
||||
// 收到新评论,添加到列表开头
|
||||
if (newComment && newComment.id) {
|
||||
// 处理新评论的用户头像
|
||||
if (newComment.user && newComment.user.avatar) {
|
||||
newComment.user.avatar = processImageUrl(newComment.user.avatar);
|
||||
}
|
||||
// 检查是否已存在
|
||||
if (!comments.value.some(c => c.id === newComment.id)) {
|
||||
comments.value.unshift(newComment); // 添加到开头
|
||||
console.log('New comment received via WebSocket:', newComment);
|
||||
}
|
||||
} else {
|
||||
console.warn("Received invalid comment data via WebSocket:", newComment);
|
||||
}
|
||||
});
|
||||
}, (error) => {
|
||||
console.error("WebSocket connection error:", error);
|
||||
// ElMessage.error('评论服务连接失败,实时更新不可用');
|
||||
});
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
// 取消订阅并断开连接
|
||||
if (commentSubscription) {
|
||||
const destination = `/topic/comments/3/${singerId.value}`;
|
||||
unsubscribe(destination);
|
||||
commentSubscription = null; // 清理订阅实例
|
||||
}
|
||||
disconnect();
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="singer-detail-container" v-loading="loading">
|
||||
<template v-if="singer">
|
||||
<!-- 歌手信息 -->
|
||||
<div class="singer-info">
|
||||
<div class="singer-avatar">
|
||||
<img :src="singer.avatar" :alt="singer.name">
|
||||
</div>
|
||||
<div class="singer-meta">
|
||||
<h1 class="singer-name">{{ singer.name }}</h1>
|
||||
<div class="singer-type">类型:{{ getSingerTypeName(singer.type) }}</div>
|
||||
<div class="singer-stats">
|
||||
<div class="stat-item">
|
||||
<div class="stat-value">{{ singer.musicCount }}</div>
|
||||
<div class="stat-label">单曲数</div>
|
||||
</div>
|
||||
|
||||
<div class="stat-item">
|
||||
<div class="stat-value">{{ formatFans(singer.fansCount || 0) }}</div>
|
||||
<div class="stat-label">粉丝数</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="singer-intro">{{ singer.introduction }}</div>
|
||||
|
||||
<div class="singer-actions">
|
||||
<el-button type="primary" @click="playAll" :disabled="!songs.length">
|
||||
<el-icon><video-play /></el-icon>
|
||||
播放全部
|
||||
</el-button>
|
||||
|
||||
<el-button
|
||||
:type="isCollected ? 'warning' : 'default'"
|
||||
@click="toggleCollect"
|
||||
>
|
||||
<el-icon><star /></el-icon>
|
||||
{{ isCollected ? '已收藏' : '收藏' }}
|
||||
</el-button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 歌曲列表、专辑和评论 -->
|
||||
<el-tabs class="singer-tabs">
|
||||
<el-tab-pane label="热门歌曲">
|
||||
<el-table
|
||||
:data="songs"
|
||||
stripe
|
||||
style="width: 100%"
|
||||
@row-dblclick="playMusic"
|
||||
>
|
||||
<el-table-column width="80" align="center">
|
||||
<template #default="scope">
|
||||
<el-button
|
||||
circle
|
||||
:icon="'VideoPlay'"
|
||||
size="small"
|
||||
@click="playMusic(scope.row)"
|
||||
></el-button>
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column label="封面" width="80" align="center">
|
||||
<template #default="scope">
|
||||
<img
|
||||
:src="scope.row.cover"
|
||||
:alt="scope.row.name"
|
||||
class="song-cover"
|
||||
>
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column prop="name" label="歌曲名" min-width="200"></el-table-column>
|
||||
|
||||
<el-table-column label="专辑" min-width="150">
|
||||
<template #default="scope">
|
||||
<span v-if="scope.row.album">{{ scope.row.album }}</span>
|
||||
<span v-else>无专辑信息</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column prop="duration" label="时长" width="100" align="center">
|
||||
<template #default="scope">
|
||||
{{ formatTime(scope.row.duration) }}
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column label="操作" width="100" align="center">
|
||||
<template #default="scope">
|
||||
<el-button
|
||||
icon="Plus"
|
||||
circle
|
||||
size="small"
|
||||
@click.stop="addToPlaylist(scope.row)"
|
||||
></el-button>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
|
||||
<!-- 空状态 -->
|
||||
<el-empty
|
||||
v-if="!songs.length"
|
||||
description="暂无歌曲"
|
||||
></el-empty>
|
||||
</el-tab-pane>
|
||||
|
||||
|
||||
|
||||
<el-tab-pane label="评论">
|
||||
<!-- 评论输入框 -->
|
||||
<div class="comment-input">
|
||||
<el-input
|
||||
v-model="commentContent"
|
||||
type="textarea"
|
||||
:rows="3"
|
||||
placeholder="发表你的评论..."
|
||||
/>
|
||||
<el-button type="primary" @click="submitComment">发表评论</el-button>
|
||||
</div>
|
||||
|
||||
<!-- 评论列表 -->
|
||||
<div class="comment-list">
|
||||
<div v-for="comment in comments" :key="comment.id" class="comment-item">
|
||||
<div class="comment-avatar">
|
||||
<el-avatar :src="comment.user.avatar" :size="40" />
|
||||
</div>
|
||||
<div class="comment-content">
|
||||
<div class="comment-user">{{ comment.user.username }}</div>
|
||||
<div class="comment-text">{{ comment.content }}</div>
|
||||
<div class="comment-time">{{ comment.createTime }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</el-tab-pane>
|
||||
</el-tabs>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.singer-detail-container {
|
||||
padding: 0;
|
||||
height: 100%;
|
||||
background-color: #fff;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.singer-info {
|
||||
display: flex;
|
||||
margin-bottom: 30px;
|
||||
padding: 20px;
|
||||
border-bottom: 1px solid #eee;
|
||||
}
|
||||
|
||||
.singer-avatar {
|
||||
width: 180px;
|
||||
height: 180px;
|
||||
margin-right: 30px;
|
||||
border-radius: 4px;
|
||||
overflow: hidden;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.singer-avatar img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
.singer-meta {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.singer-name {
|
||||
font-size: 2rem;
|
||||
margin: 0 0 15px 0;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.singer-type {
|
||||
margin-bottom: 15px;
|
||||
color: #666;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.singer-stats {
|
||||
display: flex;
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
.stat-item {
|
||||
margin-right: 30px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.stat-value {
|
||||
font-size: 1.2rem;
|
||||
font-weight: bold;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.stat-label {
|
||||
font-size: 0.8rem;
|
||||
color: #999;
|
||||
}
|
||||
|
||||
.singer-intro {
|
||||
margin-bottom: 20px;
|
||||
color: #666;
|
||||
font-size: 1rem;
|
||||
line-height: 1.6;
|
||||
max-height: 120px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 5;
|
||||
-webkit-box-orient: vertical;
|
||||
}
|
||||
|
||||
.singer-actions {
|
||||
margin-top: auto;
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.singer-tabs {
|
||||
margin: 0 20px 30px;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.song-cover {
|
||||
width: 50px;
|
||||
height: 50px;
|
||||
border-radius: 4px;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
|
||||
|
||||
.comment-input {
|
||||
margin-bottom: 20px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
align-items: flex-end;
|
||||
}
|
||||
|
||||
.comment-list {
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.comment-item {
|
||||
display: flex;
|
||||
margin-bottom: 20px;
|
||||
padding-bottom: 20px;
|
||||
border-bottom: 1px solid #eee;
|
||||
}
|
||||
|
||||
.comment-avatar {
|
||||
margin-right: 15px;
|
||||
}
|
||||
|
||||
.comment-content {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.comment-user {
|
||||
font-weight: bold;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
|
||||
.comment-text {
|
||||
margin-bottom: 5px;
|
||||
color: #333;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.comment-time {
|
||||
font-size: 0.8rem;
|
||||
color: #999;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user