feat: 添加右键添加到歌单 可以创建歌单 可以在我的歌单中右键取消收藏

This commit is contained in:
alger
2025-01-22 23:37:50 +08:00
parent a6ff0e7f5c
commit 25c2180247
7 changed files with 610 additions and 35 deletions

View File

@@ -83,3 +83,17 @@ export const likeSong = (id: number, like: boolean = true) => {
export const getLikedList = () => {
return request.get('/likelist');
};
// 创建歌单
export const createPlaylist = (params: { name: string; privacy: number }) => {
return request.post('/playlist/create', params);
};
// 添加或删除歌单歌曲
export const updatePlaylistTracks = (params: {
op: 'add' | 'del';
pid: number;
tracks: string;
}) => {
return request.get('/playlist/tracks', { params });
};

View File

@@ -59,7 +59,12 @@
:class="setAnimationClass('animate__bounceInUp')"
:style="getItemAnimationDelay(index)"
>
<song-item :item="formatDetail(item)" @play="handlePlay" />
<song-item
:item="formatDetail(item)"
:can-remove="canRemove"
@play="handlePlay"
@remove-song="(id) => emit('remove-song', id)"
/>
</div>
<div v-if="isLoadingMore" class="loading-more">加载更多...</div>
<play-bottom />
@@ -97,15 +102,17 @@ const props = withDefaults(
[key: string]: any;
};
cover?: boolean;
canRemove?: boolean;
}>(),
{
loading: false,
cover: true,
zIndex: 9996
zIndex: 9996,
canRemove: false
}
);
const emit = defineEmits(['update:show', 'update:loading']);
const emit = defineEmits(['update:show', 'update:loading', 'remove-song']);
const page = ref(0);
const pageSize = 20;

View File

@@ -0,0 +1,355 @@
<template>
<n-drawer
:show="modelValue"
:width="400"
placement="right"
@update:show="$emit('update:modelValue', $event)"
>
<n-drawer-content title="添加到歌单" class="mac-style-drawer">
<n-scrollbar class="h-full">
<div class="playlist-drawer">
<!-- 创建新歌单按钮和表单 -->
<div class="create-playlist-section">
<div
class="create-playlist-button"
:class="{ 'is-expanded': isCreating }"
@click="toggleCreateForm"
>
<div class="create-playlist-icon">
<i class="iconfont" :class="isCreating ? 'ri-close-line' : 'ri-add-line'"></i>
</div>
<div class="create-playlist-text">{{ isCreating ? '取消创建' : '创建新歌单' }}</div>
</div>
<!-- 创建歌单表单 -->
<div class="create-playlist-form" :class="{ 'is-visible': isCreating }">
<n-input
v-model:value="formValue.name"
placeholder="歌单名称"
maxlength="40"
class="mac-style-input"
:status="inputError ? 'error' : undefined"
>
<template #prefix>
<i class="iconfont ri-music-2-line"></i>
</template>
</n-input>
<div class="privacy-switch">
<div class="privacy-label">
<i
class="iconfont"
:class="formValue.privacy ? 'ri-lock-line' : 'ri-earth-line'"
></i>
<span>{{ formValue.privacy ? '私密歌单' : '公开歌单' }}</span>
</div>
<n-switch v-model:value="formValue.privacy" class="mac-style-switch">
<template #checked>私密</template>
<template #unchecked>公开</template>
</n-switch>
</div>
<div class="form-actions">
<n-button
type="primary"
quaternary
class="mac-style-button"
:loading="creating"
:disabled="!formValue.name"
@click="handleCreatePlaylist"
>
创建歌单
</n-button>
</div>
</div>
</div>
<!-- 歌单列表 -->
<div class="playlist-list">
<div
v-for="playlist in playlists"
:key="playlist.id"
class="playlist-item"
@click="handleAddToPlaylist(playlist)"
>
<n-image
:src="getImgUrl(playlist.coverImgUrl || playlist.picUrl, '100y100')"
class="playlist-item-img"
preview-disabled
:img-props="{
crossorigin: 'anonymous'
}"
/>
<div class="playlist-item-info">
<div class="playlist-item-name">{{ playlist.name }}</div>
<div class="playlist-item-count">{{ playlist.trackCount }}首歌曲</div>
</div>
<div class="playlist-item-action">
<i class="iconfont ri-add-line"></i>
</div>
</div>
</div>
</div>
</n-scrollbar>
</n-drawer-content>
</n-drawer>
</template>
<script lang="ts" setup>
import { useMessage } from 'naive-ui';
import { computed, ref, watch } from 'vue';
import { useStore } from 'vuex';
import { createPlaylist, updatePlaylistTracks } from '@/api/music';
import { getUserPlaylist } from '@/api/user';
import { getImgUrl } from '@/utils';
const props = defineProps<{
modelValue: boolean;
songId?: number;
}>();
const emit = defineEmits(['update:modelValue']);
const message = useMessage();
const playlists = ref<any[]>([]);
const creating = ref(false);
const store = useStore();
const isCreating = ref(false);
const formValue = ref({
name: '',
privacy: false
});
const inputError = computed(() => {
return isCreating.value && !formValue.value.name;
});
const toggleCreateForm = () => {
if (creating.value) return;
isCreating.value = !isCreating.value;
if (!isCreating.value) {
formValue.value.name = '';
formValue.value.privacy = false;
}
};
// 获取用户歌单
const fetchUserPlaylists = async () => {
try {
const { user } = store.state;
if (!user?.userId) {
message.error('请先登录');
emit('update:modelValue', false);
return;
}
const res = await getUserPlaylist(user.userId);
if (res.data?.playlist) {
playlists.value = res.data.playlist;
}
} catch (error) {
console.error('获取歌单失败:', error);
message.error('获取歌单失败');
}
};
// 添加到歌单
const handleAddToPlaylist = async (playlist: any) => {
if (!props.songId) return;
try {
const res = await updatePlaylistTracks({
op: 'add',
pid: playlist.id,
tracks: props.songId.toString()
});
console.log('res.data', res.data);
if (res.status === 200) {
message.success('添加成功');
emit('update:modelValue', false);
} else {
throw new Error(res.data?.msg || '添加失败');
}
} catch (error: any) {
console.error('添加到歌单失败:', error);
message.error(error.message || '添加到歌单失败');
}
};
// 创建歌单
const handleCreatePlaylist = async () => {
if (!formValue.value.name) {
message.error('请输入歌单名称');
return;
}
try {
creating.value = true;
const res = await createPlaylist({
name: formValue.value.name,
privacy: formValue.value.privacy ? 10 : 0
});
if (res.data?.id) {
message.success('创建成功');
isCreating.value = false;
formValue.value.name = '';
formValue.value.privacy = false;
await fetchUserPlaylists();
}
} catch (error) {
console.error('创建歌单失败:', error);
message.error('创建歌单失败');
} finally {
creating.value = false;
}
};
// 监听显示状态变化
watch(
() => props.modelValue,
(newVal) => {
if (newVal) {
fetchUserPlaylists();
}
}
);
</script>
<style lang="scss" scoped>
.mac-style-drawer {
@apply h-full;
:deep(.n-drawer-header__main) {
@apply text-base font-medium;
}
:deep(.n-drawer-content) {
@apply h-full;
}
:deep(.n-drawer-content-wrapper) {
@apply h-full;
}
:deep(.n-scrollbar-rail) {
@apply right-0.5;
}
}
.playlist-drawer {
@apply flex flex-col gap-6;
}
.create-playlist-section {
@apply flex flex-col;
}
.create-playlist-button {
@apply flex items-center gap-4 p-3 rounded-xl cursor-pointer transition-all duration-200
bg-gray-50 dark:bg-gray-800 hover:bg-gray-100 dark:hover:bg-gray-700;
&.is-expanded {
@apply bg-gray-100 dark:bg-gray-700;
.create-playlist-icon {
transform: rotate(45deg);
}
}
&-icon {
@apply w-10 h-10 rounded-xl bg-green-500 flex items-center justify-center text-white
transition-all duration-300;
.iconfont {
@apply text-xl transition-transform duration-300;
}
}
&-text {
@apply text-sm font-medium transition-colors duration-300;
}
}
.create-playlist-form {
@apply max-h-0 overflow-hidden transition-all duration-300 ease-in-out opacity-0;
&.is-visible {
@apply max-h-[200px] mt-4 opacity-100;
}
.mac-style-input {
@apply rounded-lg;
:deep(.n-input-wrapper) {
@apply bg-gray-50 dark:bg-gray-800 border-0;
}
:deep(.n-input__input) {
@apply text-sm;
}
:deep(.n-input__prefix) {
@apply text-gray-400;
}
}
.form-actions {
@apply mt-4;
.mac-style-button {
@apply w-full rounded-lg text-sm py-2 bg-green-500 hover:bg-green-600 text-white;
}
}
}
.privacy-switch {
@apply flex items-center justify-between mt-4 px-2;
.privacy-label {
@apply flex items-center gap-2;
.iconfont {
@apply text-base text-gray-500 dark:text-gray-400;
}
span {
@apply text-sm;
}
}
:deep(.n-switch) {
@apply h-5 min-w-[40px];
}
}
.playlist-list {
@apply flex flex-col gap-2;
}
.playlist-item {
@apply flex items-center gap-3 p-2 rounded-xl cursor-pointer transition-all duration-200
hover:bg-gray-50 dark:hover:bg-gray-800;
&-img {
@apply w-10 h-10 rounded-xl;
}
&-info {
@apply flex-1 min-w-0;
}
&-name {
@apply text-sm font-medium truncate;
}
&-count {
@apply text-xs text-gray-500 dark:text-gray-400;
}
&-action {
@apply w-8 h-8 rounded-lg flex items-center justify-center
text-gray-400 hover:text-green-500 transition-colors duration-200;
.iconfont {
@apply text-xl;
}
}
}
</style>

View File

@@ -73,9 +73,10 @@
<n-dropdown
v-if="isElectron"
:show="showDropdown"
:options="dropdownOptions"
:x="dropdownX"
:y="dropdownY"
:options="dropdownOptions"
:z-index="99999"
placement="bottom-start"
@clickoutside="showDropdown = false"
@select="handleSelect"
@@ -86,8 +87,8 @@
<script lang="ts" setup>
import { cloneDeep } from 'lodash';
import type { MenuOption } from 'naive-ui';
import { useMessage } from 'naive-ui';
import { computed, h, ref, useTemplateRef } from 'vue';
import { NImage, NText, useMessage } from 'naive-ui';
import { computed, h, inject, ref, useTemplateRef } from 'vue';
import { useStore } from 'vuex';
import { getSongUrl } from '@/hooks/MusicListHook';
@@ -104,13 +105,15 @@ const props = withDefaults(
favorite?: boolean;
selectable?: boolean;
selected?: boolean;
canRemove?: boolean;
}>(),
{
mini: false,
list: false,
favorite: true,
selectable: false,
selected: false
selected: false,
canRemove: false
}
);
@@ -132,19 +135,109 @@ const dropdownY = ref(0);
const isDownloading = ref(false);
const dropdownOptions = computed<MenuOption[]>(() => [
{
label: '下一首播放',
key: 'playNext',
icon: () => h('i', { class: 'iconfont ri-play-list-2-line' })
},
{
label: isDownloading.value ? '下载中...' : `下载 ${props.item.name}`,
key: 'download',
icon: () => h('i', { class: 'iconfont ri-download-line' }),
disabled: isDownloading.value
const openPlaylistDrawer = inject<(songId: number) => void>('openPlaylistDrawer');
const renderSongPreview = () => {
return h(
'div',
{
class: 'flex items-center gap-3 px-2 py-1 dark:border-gray-800'
},
[
h(NImage, {
src: getImgUrl(props.item.picUrl || props.item.al?.picUrl, '100y100'),
class: 'w-10 h-10 rounded-lg flex-shrink-0',
previewDisabled: true,
imgProps: {
crossorigin: 'anonymous'
}
}),
h(
'div',
{
class: 'flex-1 min-w-0 py-1'
},
[
h(
'div',
{
class: 'mb-1'
},
[
h(
NText,
{
depth: 1,
class: 'text-sm font-medium'
},
{
default: () => props.item.name
}
)
]
)
]
)
]
);
};
const dropdownOptions = computed<MenuOption[]>(() => {
const options: MenuOption[] = [
{
key: 'header',
type: 'render',
render: renderSongPreview
},
{
key: 'divider1',
type: 'divider'
},
{
label: '播放',
key: 'play',
icon: () => h('i', { class: 'iconfont ri-play-circle-line' })
},
{
label: '下一首播放',
key: 'playNext',
icon: () => h('i', { class: 'iconfont ri-play-list-2-line' })
},
{
type: 'divider',
key: 'd1'
},
{
label: '添加到歌单',
key: 'addToPlaylist',
icon: () => h('i', { class: 'iconfont ri-folder-add-line' })
},
{
label: isFavorite.value ? '取消喜欢' : '喜欢',
key: 'favorite',
icon: () =>
h('i', {
class: `iconfont ${isFavorite.value ? 'ri-heart-fill text-red-500' : 'ri-heart-line'}`
})
}
];
if (props.canRemove) {
options.push(
{
type: 'divider',
key: 'd2'
},
{
label: '从歌单中删除',
key: 'remove',
icon: () => h('i', { class: 'iconfont ri-delete-bin-line' })
}
);
}
]);
return options;
});
const handleContextMenu = (e: MouseEvent) => {
e.preventDefault();
@@ -159,6 +252,14 @@ const handleSelect = (key: string | number) => {
downloadMusic();
} else if (key === 'playNext') {
handlePlayNext();
} else if (key === 'addToPlaylist') {
openPlaylistDrawer?.(props.item.id);
} else if (key === 'favorite') {
toggleFavorite(new Event('click'));
} else if (key === 'play') {
playMusicEvent(props.item);
} else if (key === 'remove') {
emits('remove-song', props.item.id);
}
};
@@ -229,7 +330,7 @@ const downloadMusic = async () => {
}
};
const emits = defineEmits(['play', 'select']);
const emits = defineEmits(['play', 'select', 'remove-song']);
const songImageRef = useTemplateRef('songImg');
const imageLoad = async () => {
@@ -463,4 +564,56 @@ const handlePlayNext = () => {
}
}
}
:deep(.n-dropdown-menu) {
@apply min-w-[240px] overflow-hidden rounded-lg border dark:border-gray-800;
.n-dropdown-option {
@apply h-9 text-sm;
&:hover {
@apply bg-gray-100 dark:bg-gray-800;
}
.n-dropdown-option-body {
@apply h-full;
.n-dropdown-option-body__prefix {
@apply w-8 flex justify-center items-center;
.iconfont {
@apply text-base;
}
}
}
}
.n-dropdown-divider {
@apply my-1;
}
}
:deep(.song-preview) {
@apply flex items-center gap-3 p-3 border-b dark:border-gray-800;
.n-image {
@apply w-12 h-12 rounded-lg flex-shrink-0;
}
.song-preview-info {
@apply flex-1 min-w-0 py-1;
.song-preview-name {
@apply text-sm font-medium truncate mb-1;
}
.song-preview-artist {
@apply text-xs text-gray-500 dark:text-gray-400 truncate;
}
}
}
:deep(.n-dropdown-option-body--render) {
@apply p-0;
}
</style>

View File

@@ -32,11 +32,12 @@
<install-app-modal v-if="!isElectron"></install-app-modal>
<update-modal v-if="isElectron" />
<artist-drawer ref="artistDrawerRef" :show="artistDrawerShow" />
<playlist-drawer v-model="showPlaylistDrawer" :song-id="currentSongId" />
</div>
</template>
<script lang="ts" setup>
import { computed, defineAsyncComponent, nextTick, onMounted, ref, watch } from 'vue';
import { computed, defineAsyncComponent, nextTick, onMounted, provide, ref, watch } from 'vue';
import { useRoute } from 'vue-router';
import { useStore } from 'vuex';
@@ -63,6 +64,7 @@ const SearchBar = defineAsyncComponent(() => import('./components/SearchBar.vue'
const TitleBar = defineAsyncComponent(() => import('./components/TitleBar.vue'));
const ArtistDrawer = defineAsyncComponent(() => import('@/components/common/ArtistDrawer.vue'));
const PlaylistDrawer = defineAsyncComponent(() => import('@/components/common/PlaylistDrawer.vue'));
const store = useStore();
@@ -93,6 +95,18 @@ watch(
}
}
);
const showPlaylistDrawer = ref(false);
const currentSongId = ref<number | undefined>();
// 提供一个方法来打开歌单抽屉
const openPlaylistDrawer = (songId: number) => {
currentSongId.value = songId;
showPlaylistDrawer.value = true;
};
// 将方法提供给全局
provide('openPlaylistDrawer', openPlaylistDrawer);
</script>
<style lang="scss" scoped>

View File

@@ -44,19 +44,14 @@ request.interceptors.request.use(
// 在请求发送之前做一些处理
// 在get请求params中添加timestamp
if (config.method === 'get') {
config.params = {
...config.params,
timestamp: Date.now()
};
const token = localStorage.getItem('token');
if (token) {
config.params.cookie = `${token} os=pc;`;
} else {
config.params.cookie = 'os=pc;';
}
config.params = {
...config.params,
timestamp: Date.now()
};
const token = localStorage.getItem('token');
if (token) {
config.params.cookie = `${token} os=pc;`;
}
if (isElectron) {
const proxyConfig = setData?.proxyConfig;
if (proxyConfig?.enable && ['http', 'https'].includes(proxyConfig?.protocol)) {

View File

@@ -84,16 +84,20 @@
:song-list="list?.tracks || []"
:list-info="list"
:loading="listLoading"
:can-remove="true"
@remove-song="handleRemoveFromPlaylist"
/>
</div>
</template>
<script lang="ts" setup>
import { useMessage } from 'naive-ui';
import { computed, onBeforeUnmount, ref, watch } from 'vue';
import { useRouter } from 'vue-router';
import { useStore } from 'vuex';
import { getListDetail } from '@/api/list';
import { updatePlaylistTracks } from '@/api/music';
import { getUserDetail, getUserPlaylist, getUserRecord } from '@/api/user';
import PlayBottom from '@/components/common/PlayBottom.vue';
import SongItem from '@/components/common/SongItem.vue';
@@ -116,6 +120,7 @@ const mounted = ref(true);
const isShowList = ref(false);
const list = ref<Playlist>();
const listLoading = ref(false);
const message = useMessage();
const user = computed(() => store.state.user);
@@ -185,6 +190,8 @@ watch(
(newPath) => {
if (newPath === '/user') {
checkLoginStatus();
} else {
loadPage();
}
}
);
@@ -215,11 +222,41 @@ const showPlaylist = async (id: number, name: string) => {
listLoading.value = true;
list.value = {
name
name,
id
} as Playlist;
await loadPlaylistDetail(id);
listLoading.value = false;
};
// 加载歌单详情
const loadPlaylistDetail = async (id: number) => {
const { data } = await getListDetail(id);
list.value = data.playlist;
listLoading.value = false;
};
// 从歌单中删除歌曲
const handleRemoveFromPlaylist = async (songId: number) => {
if (!list.value?.id) return;
try {
const res = await updatePlaylistTracks({
op: 'del',
pid: list.value.id,
tracks: songId.toString()
});
if (res.status === 200) {
message.success('删除成功');
// 重新加载歌单详情
await loadPlaylistDetail(list.value.id);
} else {
throw new Error(res.data?.msg || '删除失败');
}
} catch (error: any) {
console.error('删除歌曲失败:', error);
message.error(error.message || '删除失败');
}
};
const handlePlay = () => {