@@ -180,18 +180,12 @@ export const updateUserLikeAlbums = async () => {
180180
181181// 更新用户喜欢电台
182182export const updateUserLikeDjs = async ( ) => {
183- const dataStore = useDataStore ( ) ;
184- if ( ! isLogin ( ) || ! dataStore . userData . userId ) return ;
185- const result = await userDj ( ) ;
186- dataStore . setUserLikeData ( "djs" , formatCoverList ( result . djRadios ) ) ;
183+ await setUserLikeDataLoop ( userDj , formatCoverList , "djs" ) ;
187184} ;
188185
189186// 更新用户喜欢MV
190187export const updateUserLikeMvs = async ( ) => {
191- const dataStore = useDataStore ( ) ;
192- if ( ! isLogin ( ) || ! dataStore . userData . userId ) return ;
193- const result = await userMv ( ) ;
194- dataStore . setUserLikeData ( "mvs" , formatCoverList ( result . data ) ) ;
188+ await setUserLikeDataLoop ( userMv , formatCoverList , "mvs" ) ;
195189} ;
196190
197191// 喜欢歌曲
@@ -294,40 +288,57 @@ export const toSubRadio = toLikeSomething("订阅", "播客", () => radioSub, up
294288
295289// 循环获取用户喜欢数据
296290const setUserLikeDataLoop = async < T > (
297- apiFunction : ( limit : number , offset : number ) => Promise < { data : any [ ] ; count : number } > ,
291+ apiFunction : ( limit : number , offset : number ) => Promise < any > ,
298292 formatFunction : ( data : any [ ] ) => T [ ] ,
299293 key : keyof UserLikeDataType ,
300294) => {
301295 const dataStore = useDataStore ( ) ;
302296 const userId = dataStore . userData . userId ;
303297 if ( ! isLogin ( ) || ! userId ) return ;
304- // 必要数据
305- let offset : number = 0 ;
298+
299+ let offset = 0 ;
306300 const allData : T [ ] = [ ] ;
307- const limit : number = 100 ;
308- // 是否可循环
309- let canLoop : boolean = true ;
310- // 循环获取
311- while ( canLoop ) {
312- const { data, count } = await apiFunction ( limit , offset ) ;
313- // 数据处理
314- const formattedData = formatFunction ( data ) ;
315- // 若为空
316- if ( formattedData . length === 0 ) break ;
317- // 合并数据
318- allData . push ( ...formattedData ) ;
319- // 更新偏移量
320- offset += limit ;
321- canLoop = offset < count && formattedData . length > 0 ;
322- }
323- // 更新数据
324- if ( key === "artists" ) {
325- dataStore . setUserLikeData ( key , allData as ArtistType [ ] ) ;
326- } else if ( key === "albums" || key === "mvs" || key === "djs" ) {
327- dataStore . setUserLikeData ( key , allData as CoverType [ ] ) ;
328- } else {
329- console . error ( `Unsupported key: ${ key } ` ) ;
330- }
301+ const limit = 50 ; // 限制每页50条
302+
303+ while ( true ) {
304+ try {
305+ const result = await apiFunction ( limit , offset ) ;
306+ // 根据不同 API 提取数据字段
307+ let data : any [ ] = [ ] ;
308+ if ( key === "djs" ) {
309+ data = result . djRadios || [ ] ;
310+ } else if ( key === "playlists" ) {
311+ data = result . playlist || [ ] ;
312+ } else {
313+ data = result . data || [ ] ;
314+ }
315+
316+ if ( ! Array . isArray ( data ) || data . length === 0 ) {
317+ break ; // 没有更多数据
318+ }
319+
320+ // 格式化并合并数据
321+ const formattedData = formatFunction ( data ) ;
322+ allData . push ( ...formattedData ) ;
323+
324+ // 数据少于分页大小,说明已是最后一页
325+ if ( data . length < limit ) {
326+ break ;
327+ }
328+
329+ offset += limit ;
330+ } catch ( error ) {
331+ console . error ( `Error fetching ${ key } data at offset ${ offset } :` , error ) ;
332+ break ;
333+ if ( key === "artists" ) {
334+ dataStore . setUserLikeData ( key , allData as ArtistType [ ] ) ;
335+ } else if ( key === "playlists" || key === "albums" || key === "mvs" || key === "djs" ) {
336+ dataStore . setUserLikeData ( key , allData as CoverType [ ] ) ;
337+ } else {
338+ console . error ( `Unsupported key in setUserLikeDataLoop: ${ key } ` ) ;
339+ }
340+
341+ console . log ( `✅ Fetched ${ allData . length } ${ key } for user ${ userId } ` ) ;
331342 return allData ;
332343} ;
333344
0 commit comments