feat(playlist): 添加歌单封面上传功能

- 在编辑表单中新增 coverFile 字段用于存储封面文件
- 移除原有的头像上传URL和请求头配置
- 实现封面预览功能,支持本地图片预览
- 修改歌单更新接口调用,支持封面文件参数传递
- 替换原有的封面上传成功回调为封面选择处理逻辑
- 更新上传组件配置,使用手动上传模式并支持图片预览
- 优化封面上传前验证和错误处理机制
This commit is contained in:
ikmkj
2026-03-11 14:12:55 +08:00
parent bfb401cac6
commit f0fa9cf67d

View File

@@ -34,7 +34,8 @@ const createForm = reactive({
const editForm = reactive({ const editForm = reactive({
id: null, id: null,
name: '', name: '',
description: '' description: '',
coverFile: null
}) })
// 表单规则 // 表单规则
@@ -52,14 +53,6 @@ const formRules = {
const createFormRef = ref(null) const createFormRef = ref(null)
const editFormRef = ref(null) const editFormRef = ref(null)
// 头像上传URL
const coverUploadUrl = ref(`${import.meta.env.VITE_API_BASE_URL || ''}/api/file/upload/image`)
// 头像上传headers
const uploadHeaders = ref({
[userStore.tokenName]: userStore.token
})
// 获取自建歌单列表 // 获取自建歌单列表
const fetchUserPlaylists = async () => { const fetchUserPlaylists = async () => {
loading.value = true loading.value = true
@@ -107,6 +100,8 @@ const openEditDialog = (playlist) => {
editForm.id = playlist.id editForm.id = playlist.id
editForm.name = playlist.name editForm.name = playlist.name
editForm.description = playlist.description editForm.description = playlist.description
editForm.coverFile = null
coverPreview.value = playlist.cover
editDialogVisible.value = true editDialogVisible.value = true
} }
@@ -144,7 +139,7 @@ const handleUpdatePlaylist = () => {
const res = await updatePlaylist(editForm.id, { const res = await updatePlaylist(editForm.id, {
name: editForm.name, name: editForm.name,
description: editForm.description description: editForm.description
}) }, editForm.coverFile)
if (res.code === 200) { if (res.code === 200) {
// 更新成功,重新获取歌单列表 // 更新成功,重新获取歌单列表
@@ -185,33 +180,13 @@ const handleDeletePlaylist = (id) => {
}).catch(() => {}) }).catch(() => {})
} }
// 封面上传成功 // 封面预览URL
const handleCoverSuccess = async (response) => { const coverPreview = ref('')
if (response.code === 200) {
// 上传成功获取图片URL
const imageUrl = response.data
try { // 封面选择处理(不自动上传,仅本地预览)
// 更新歌单封面 const handleCoverChange = (file) => {
const res = await updatePlaylist(currentPlaylist.value.id, { editForm.coverFile = file.raw
...editForm, coverPreview.value = URL.createObjectURL(file.raw)
cover: imageUrl
})
if (res.code === 200) {
// 更新成功,重新获取歌单列表
await fetchUserPlaylists()
ElMessage.success('封面上传成功')
} else {
ElMessage.error(res.message || '更新歌单封面失败')
}
} catch (error) {
console.error('更新歌单封面失败:', error)
ElMessage.error('更新歌单封面失败')
}
} else {
ElMessage.error(response.message || '封面上传失败')
}
} }
// 封面上传前的处理 // 封面上传前的处理
@@ -354,15 +329,15 @@ onMounted(() => {
<el-form-item label="封面"> <el-form-item label="封面">
<el-upload <el-upload
class="cover-uploader" class="cover-uploader"
:action="coverUploadUrl" action="#"
:headers="uploadHeaders"
:show-file-list="false" :show-file-list="false"
:on-success="handleCoverSuccess" :auto-upload="false"
:on-change="handleCoverChange"
:before-upload="beforeCoverUpload" :before-upload="beforeCoverUpload"
> >
<img <img
v-if="currentPlaylist" v-if="coverPreview"
:src="currentPlaylist.cover" :src="coverPreview"
class="cover" class="cover"
/> />
<el-icon v-else class="cover-uploader-icon"><plus /></el-icon> <el-icon v-else class="cover-uploader-icon"><plus /></el-icon>