4444import type { DropdownOption } from " naive-ui" ;
4545import { SongType } from " @/types/main" ;
4646import { songDetail } from " @/api/song" ;
47- import { playlistDetail } from " @/api/playlist" ;
47+ import { playlistDetail , playlistAllSongs } from " @/api/playlist" ;
4848import { formatCoverList , formatSongsList } from " @/utils/format" ;
4949import { renderIcon , copyData } from " @/utils/helper" ;
5050import { isObject } from " lodash-es" ;
@@ -61,8 +61,16 @@ const dataStore = useDataStore();
6161// 是否激活
6262const isActivated = ref <boolean >(false );
6363
64- const { detailData, listData, loading, getSongListHeight, setDetailData, setListData, setLoading } =
65- useListDetail ();
64+ const {
65+ detailData,
66+ listData,
67+ loading,
68+ getSongListHeight,
69+ setDetailData,
70+ setListData,
71+ appendListData,
72+ setLoading,
73+ } = useListDetail ();
6674const { searchValue, searchData, displayData, clearSearch, performSearch } =
6775 useListSearch (listData );
6876const { listScrolling, handleListScroll, resetScroll } = useListScroll ();
@@ -181,12 +189,21 @@ const loadPlaylistData = async (id: number, forceRefresh: boolean = false) => {
181189 setDetailData (formatCoverList (detail .playlist )[0 ]);
182190 // 获取全部 ID 顺序
183191 const serverIds: number [] = detail .privileges ?.map ((p : any ) => p .id ) || [];
184- if (serverIds .length === 0 ) {
185- setLoading (false );
186- return ;
192+ const trackCount = detail .playlist ?.trackCount || 0 ;
193+
194+ // 如果 privileges 数量少于 trackCount,说明数据不完整,需要全量获取
195+ if (serverIds .length < trackCount && trackCount > 0 ) {
196+ console .log (` 🔄 Liked songs incomplete (${serverIds .length }/${trackCount }), fetching all... ` );
197+ await fetchAllSongs (id , trackCount );
198+ } else {
199+ if (serverIds .length === 0 ) {
200+ setLoading (false );
201+ return ;
202+ }
203+ // 同步歌曲列表
204+ await syncSongList (serverIds , id );
187205 }
188- // 同步歌曲列表
189- await syncSongList (serverIds , id );
206+
190207 // 更新缓存
191208 if (currentRequestId .value === id && detailData .value ) {
192209 dataStore .setLikeSongsList (detailData .value , listData .value );
@@ -200,6 +217,41 @@ const loadPlaylistData = async (id: number, forceRefresh: boolean = false) => {
200217 }
201218};
202219
220+ /**
221+ * 全量获取歌曲列表
222+ * 当 privileges 数据不完整时调用
223+ */
224+ const fetchAllSongs = async (id : number , total : number ) => {
225+ const limit = 500 ;
226+ let offset = 0 ;
227+ const allSongs: SongType [] = [];
228+
229+ while (offset < total ) {
230+ if (currentRequestId .value !== id ) return ;
231+ try {
232+ const result = await playlistAllSongs (id , limit , offset );
233+ if (currentRequestId .value !== id ) return ;
234+ const songs = formatSongsList (result .songs );
235+ allSongs .push (... songs );
236+ // 实时更新列表展示
237+ if (offset === 0 ) {
238+ setListData (songs );
239+ } else {
240+ appendListData (songs );
241+ }
242+ offset += limit ;
243+ } catch (error ) {
244+ console .error (" Failed to fetch all songs:" , error );
245+ break ;
246+ }
247+ }
248+
249+ if (currentRequestId .value !== id ) return ;
250+ // 确保最终列表完整性
251+ setListData (allSongs );
252+ console .log (` ✅ Fetched all ${allSongs .length } liked songs ` );
253+ };
254+
203255/**
204256 * 加载缓存
205257 */
0 commit comments