From 9fcb5ca63336c1cbc95c1f7eb6ec616ebd97c1eb Mon Sep 17 00:00:00 2001 From: MoYingJi Date: Sat, 25 Oct 2025 03:06:42 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E6=94=B6?= =?UTF-8?q?=E8=97=8F=E4=B8=93=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/auth.ts | 27 +++++++++++++++++++++++++++ src/views/List/album.vue | 11 +++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/utils/auth.ts b/src/utils/auth.ts index d7289ca..6d5b128 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -21,6 +21,7 @@ import { dailyRecommend } from "@/api/rec"; import { isElectron } from "./helper"; import { likePlaylist, playlistTracks } from "@/api/playlist"; import { likeArtist } from "@/api/artist"; +import { likeAlbum } from "@/api/album"; import { radioSub } from "@/api/radio"; /** @@ -262,6 +263,32 @@ export const toLikePlaylist = debounce( { leading: true, trailing: false }, ); +// 收藏/取消收藏歌单 +export const toLikeAlbum = debounce( + async (id: number, like: boolean) => { + if (!id) return; + if (!isLogin()) { + window.$message.warning("请登录后使用"); + return; + } + if (isLogin() === 2) { + window.$message.warning("该登录模式暂不支持该操作"); + return; + } + const { code } = await likeAlbum(id, like ? 1 : 2); + if (code === 200) { + window.$message.success((like ? "收藏" : "取消收藏") + "专辑成功"); + // 更新 + await updateUserLikeAlbums(); + } else { + window.$message.success((like ? "收藏" : "取消收藏") + "专辑失败,请重试"); + return; + } + }, + 300, + { leading: true, trailing: false }, +); + // 收藏/取消收藏歌手 export const toLikeArtist = debounce( async (id: number, like: boolean) => { diff --git a/src/views/List/album.vue b/src/views/List/album.vue index 14f3b2e..3cb94e5 100644 --- a/src/views/List/album.vue +++ b/src/views/List/album.vue @@ -101,7 +101,13 @@ {{ loading ? "加载中..." : "播放" }} - + @@ -170,7 +176,7 @@ import type { CoverType, SongType } from "@/types/main"; import type { DropdownOption } from "naive-ui"; import { songDetail } from "@/api/song"; -import { albumDetail } from "@/api/album"; +import { albumDetail, likeAlbum } from "@/api/album"; import { formatCoverList, formatSongsList } from "@/utils/format"; import { coverLoaded, fuzzySearch, renderIcon } from "@/utils/helper"; import { renderToolbar } from "@/utils/meta"; @@ -179,6 +185,7 @@ import { debounce } from "lodash-es"; import { formatTimestamp } from "@/utils/time"; import { openJumpArtist } from "@/utils/modal"; import player from "@/utils/player"; +import { toLikeAlbum } from "@/utils/auth"; const router = useRouter(); const dataStore = useDataStore(); From 5e376783489e2afc9025f8ebf9e73e93cd8aa221 Mon Sep 17 00:00:00 2001 From: MoYingJi Date: Sat, 25 Oct 2025 03:28:37 +0800 Subject: [PATCH 2/4] =?UTF-8?q?refactor(auth):=20=E6=8A=BD=E7=A6=BB?= =?UTF-8?q?=E6=94=B6=E8=97=8F/=E8=AE=A2=E9=98=85=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E5=88=B0=E9=80=9A=E7=94=A8=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `toLikeSomething` 的 `request` 参数必须套一层 getter 才行,不然报错在定义前使用,不是很懂 TypeScript 🤔 --- src/utils/auth.ts | 137 ++++++++++++++-------------------------------- 1 file changed, 40 insertions(+), 97 deletions(-) diff --git a/src/utils/auth.ts b/src/utils/auth.ts index 6d5b128..0e86eec 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -237,109 +237,52 @@ export const toLikeSong = debounce( { leading: true, trailing: false }, ); -// 收藏/取消收藏歌单 -export const toLikePlaylist = debounce( - async (id: number, like: boolean) => { - if (!id) return; - if (!isLogin()) { - window.$message.warning("请登录后使用"); - return; - } - if (isLogin() === 2) { - window.$message.warning("该登录模式暂不支持该操作"); - return; - } - const { code } = await likePlaylist(id, like ? 1 : 2); - if (code === 200) { - window.$message.success((like ? "收藏" : "取消收藏") + "歌单成功"); - // 更新 - await updateUserLikePlaylist(); - } else { - window.$message.success((like ? "收藏" : "取消收藏") + "歌单失败,请重试"); - return; - } - }, - 300, - { leading: true, trailing: false }, -); +const toLikeSomething = + ( + actionName: string, + thingName: string, + request: () => (id: number, t: 1 | 2) => Promise<{ code: number }>, + update: () => Promise, + ) => { + return debounce( + async (id: number, like: boolean) => { + // 错误情况 + if (!id) return; + if (!isLogin()) { + window.$message.warning("请登录后使用"); + return; + } + if (isLogin() === 2) { + window.$message.warning("该登录模式暂不支持该操作"); + return; + } + // 请求 + const { code } = await request()(id, like ? 1 : 2); + if (code === 200) { + window.$message.success((like ? "" : "取消") + actionName + thingName + "成功"); + // 更新 + await update(); + } else { + window.$message.success((like ? "" : "取消") + actionName + thingName + "失败,请重试"); + return; + } + }, + 300, + { leading: true, trailing: false }, + ); + }; // 收藏/取消收藏歌单 -export const toLikeAlbum = debounce( - async (id: number, like: boolean) => { - if (!id) return; - if (!isLogin()) { - window.$message.warning("请登录后使用"); - return; - } - if (isLogin() === 2) { - window.$message.warning("该登录模式暂不支持该操作"); - return; - } - const { code } = await likeAlbum(id, like ? 1 : 2); - if (code === 200) { - window.$message.success((like ? "收藏" : "取消收藏") + "专辑成功"); - // 更新 - await updateUserLikeAlbums(); - } else { - window.$message.success((like ? "收藏" : "取消收藏") + "专辑失败,请重试"); - return; - } - }, - 300, - { leading: true, trailing: false }, -); +export const toLikePlaylist = toLikeSomething("收藏", "歌单", () => likePlaylist, updateUserLikePlaylist) + +// 收藏/取消收藏歌单 +export const toLikeAlbum = toLikeSomething("收藏", "专辑", () => likeAlbum, updateUserLikeAlbums) // 收藏/取消收藏歌手 -export const toLikeArtist = debounce( - async (id: number, like: boolean) => { - if (!id) return; - if (!isLogin()) { - window.$message.warning("请登录后使用"); - return; - } - if (isLogin() === 2) { - window.$message.warning("该登录模式暂不支持该操作"); - return; - } - const { code } = await likeArtist(id, like ? 1 : 2); - if (code === 200) { - window.$message.success((like ? "收藏" : "取消收藏") + "歌手成功"); - // 更新 - await updateUserLikeArtists(); - } else { - window.$message.success((like ? "收藏" : "取消收藏") + "歌手失败,请重试"); - return; - } - }, - 300, - { leading: true, trailing: false }, -); +export const toLikeArtist = toLikeSomething("收藏", "歌手", () => likeArtist, updateUserLikeArtists) // 订阅/取消订阅播客 -export const toSubRadio = debounce( - async (id: number, like: boolean) => { - if (!id) return; - if (!isLogin()) { - window.$message.warning("请登录后使用"); - return; - } - if (isLogin() === 2) { - window.$message.warning("该登录模式暂不支持该操作"); - return; - } - const { code } = await radioSub(id, like ? 1 : 0); - if (code === 200) { - window.$message.success((like ? "订阅" : "取消订阅") + "播客成功"); - // 更新 - await updateUserLikeDjs(); - } else { - window.$message.success((like ? "订阅" : "取消订阅") + "播客失败,请重试"); - return; - } - }, - 300, - { leading: true, trailing: false }, -); +export const toSubRadio = toLikeSomething("订阅", "播客", () => radioSub, updateUserLikeDjs) // 循环获取用户喜欢数据 const setUserLikeDataLoop = async ( From e854dbb8166c783857a8479e5abb20f0a17ec62b Mon Sep 17 00:00:00 2001 From: MoYingJi Date: Sat, 25 Oct 2025 03:33:12 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20=E6=9C=AA=E4=BD=BF=E7=94=A8=E7=9A=84?= =?UTF-8?q?=20import=20=E5=92=8C=E6=B3=A8=E9=87=8A=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/auth.ts | 2 +- src/views/List/album.vue | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/auth.ts b/src/utils/auth.ts index 0e86eec..3264f1a 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -275,7 +275,7 @@ const toLikeSomething = // 收藏/取消收藏歌单 export const toLikePlaylist = toLikeSomething("收藏", "歌单", () => likePlaylist, updateUserLikePlaylist) -// 收藏/取消收藏歌单 +// 收藏/取消收藏专辑 export const toLikeAlbum = toLikeSomething("收藏", "专辑", () => likeAlbum, updateUserLikeAlbums) // 收藏/取消收藏歌手 diff --git a/src/views/List/album.vue b/src/views/List/album.vue index 3cb94e5..12a93a4 100644 --- a/src/views/List/album.vue +++ b/src/views/List/album.vue @@ -176,7 +176,7 @@ import type { CoverType, SongType } from "@/types/main"; import type { DropdownOption } from "naive-ui"; import { songDetail } from "@/api/song"; -import { albumDetail, likeAlbum } from "@/api/album"; +import { albumDetail } from "@/api/album"; import { formatCoverList, formatSongsList } from "@/utils/format"; import { coverLoaded, fuzzySearch, renderIcon } from "@/utils/helper"; import { renderToolbar } from "@/utils/meta"; From 424c1e76f059a97476203472af93e588f4b3d373 Mon Sep 17 00:00:00 2001 From: MoYingJi Date: Sat, 25 Oct 2025 03:36:08 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=A0=BC=E5=BC=8F=20?= =?UTF-8?q?=E5=8E=BB=E6=8E=89=E4=B8=80=E5=B1=82=E5=B5=8C=E5=A5=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/auth.ts | 54 +++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/src/utils/auth.ts b/src/utils/auth.ts index 3264f1a..35c33be 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -243,34 +243,32 @@ const toLikeSomething = thingName: string, request: () => (id: number, t: 1 | 2) => Promise<{ code: number }>, update: () => Promise, - ) => { - return debounce( - async (id: number, like: boolean) => { - // 错误情况 - if (!id) return; - if (!isLogin()) { - window.$message.warning("请登录后使用"); - return; - } - if (isLogin() === 2) { - window.$message.warning("该登录模式暂不支持该操作"); - return; - } - // 请求 - const { code } = await request()(id, like ? 1 : 2); - if (code === 200) { - window.$message.success((like ? "" : "取消") + actionName + thingName + "成功"); - // 更新 - await update(); - } else { - window.$message.success((like ? "" : "取消") + actionName + thingName + "失败,请重试"); - return; - } - }, - 300, - { leading: true, trailing: false }, - ); - }; + ) => debounce( + async (id: number, like: boolean) => { + // 错误情况 + if (!id) return; + if (!isLogin()) { + window.$message.warning("请登录后使用"); + return; + } + if (isLogin() === 2) { + window.$message.warning("该登录模式暂不支持该操作"); + return; + } + // 请求 + const { code } = await request()(id, like ? 1 : 2); + if (code === 200) { + window.$message.success((like ? "" : "取消") + actionName + thingName + "成功"); + // 更新 + await update(); + } else { + window.$message.success((like ? "" : "取消") + actionName + thingName + "失败,请重试"); + return; + } + }, + 300, + { leading: true, trailing: false }, + ); // 收藏/取消收藏歌单 export const toLikePlaylist = toLikeSomething("收藏", "歌单", () => likePlaylist, updateUserLikePlaylist)