@@ -197,6 +197,9 @@ const searchData = ref<SongType[]>([]);
197197// 歌单 ID
198198const playlistId = computed <number >(() => dataStore .userLikeData .playlists ?.[0 ]?.id );
199199
200+ // 当前正在请求的歌单 ID,用于防止竞态条件
201+ const currentRequestId = ref <number >(0 );
202+
200203// 加载提示
201204const loading = ref <boolean >(true );
202205const loadingMsg = ref <MessageReactive | null >(null );
@@ -276,6 +279,8 @@ const getPlaylistDetail = async (
276279 },
277280) => {
278281 if (! id ) return ;
282+ // 设置当前请求的歌单 ID,用于防止竞态条件
283+ currentRequestId .value = id ;
279284 // 设置加载状态
280285 loading .value = true ;
281286 const { getList, refresh } = options ;
@@ -301,6 +306,8 @@ const getPlaylistData = async (id: number, getList: boolean, refresh: boolean) =
301306 loadLikedCache ();
302307 // 获取歌单详情
303308 const detail = await playlistDetail (id );
309+ // 检查是否仍然是当前请求的歌单
310+ if (currentRequestId .value !== id ) return ;
304311 playlistDetailData .value = formatCoverList (detail .playlist )[0 ];
305312 // 不需要获取列表或无歌曲
306313 if (! getList || playlistDetailData .value .count === 0 ) {
@@ -311,11 +318,15 @@ const getPlaylistData = async (id: number, getList: boolean, refresh: boolean) =
311318 if (isLogin () === 1 && (playlistDetailData .value ?.count as number ) < 800 ) {
312319 const ids: number [] = detail .privileges .map ((song : any ) => song .id as number );
313320 const result = await songDetail (ids );
321+ // 检查是否仍然是当前请求的歌单
322+ if (currentRequestId .value !== id ) return ;
314323 // 直接批量详情返回时也进行一次按 id 去重
315324 playlistData .value = uniqBy (formatSongsList (result .songs ), " id" );
316325 } else {
317326 await getPlaylistAllSongs (id , playlistDetailData .value .count || 0 , refresh );
318327 }
328+ // 检查是否仍然是当前请求的歌单
329+ if (currentRequestId .value !== id ) return ;
319330 // 更新我喜欢
320331 dataStore .setLikeSongsList (playlistDetailData .value , playlistData .value );
321332 loading .value = false ;
@@ -347,14 +358,29 @@ const getPlaylistAllSongs = async (
347358 const limit: number = 500 ;
348359 const listData: SongType [] = [];
349360 do {
361+ // 检查是否仍然是当前请求的歌单
362+ if (currentRequestId .value !== id ) {
363+ loadingMsgShow (false );
364+ return ;
365+ }
350366 const result = await playlistAllSongs (id , limit , offset );
367+ // 再次检查是否仍然是当前请求的歌单(请求完成后)
368+ if (currentRequestId .value !== id ) {
369+ loadingMsgShow (false );
370+ return ;
371+ }
351372 const songData = formatSongsList (result .songs );
352373 listData .push (... songData );
353374 // 非刷新模式下,增量拼接时进行去重,避免与缓存或上一页数据重复
354375 if (! refresh ) playlistData .value = uniqBy ([... playlistData .value , ... songData ], " id" );
355376 // 更新数据
356377 offset += limit ;
357- } while (offset < count && isLikedPage .value );
378+ } while (offset < count && isLikedPage .value && currentRequestId .value === id );
379+ // 最终检查是否仍然是当前请求的歌单
380+ if (currentRequestId .value !== id ) {
381+ loadingMsgShow (false );
382+ return ;
383+ }
358384 // 刷新模式下,统一以最终聚合数据为准,并进行去重
359385 if (refresh ) playlistData .value = uniqBy (listData , " id" );
360386 // 关闭加载
0 commit comments