import { useMusicApi } from '../hooks' ; import { getSimilar } from './artist' ; const musicApi = useMusicApi ( ) ; async function getSongUrl ( id , cookie ? ) { const { body } = await musicApi . song_url ( { id , cookie , } ) ; const [ d ] = body . data ; return { id , src : d . url , bitRate : d . br , } ; } async function getRecentUser ( id , cookie ? ) { try { const { body } = await musicApi . simi_user ( { id , cookie } ) ; if ( body . code === 200 ) { const userProfiles = body . userprofiles ; return userProfiles . map ( ( e ) => { return { id : e . userId , name : e . nickname , avatar : `${ e . avatarUrl }?param=50y50` , link : `/user/${ e . userId }` , } ; } ) ; } } catch ( e ) { console . error ( e ) ; } return [ ] ; } async function getSimilarPlaylist ( id , cookie ? ) { try { const { body } = await musicApi . simi_playlist ( { id , cookie } ) ; if ( body . code === 200 ) { const playlists = body . playlists ; return playlists . map ( ( e ) => { return { id : e . id , name : e . name , cover : e . coverImgUrl , link : `/player/0/${ e . id }` , } ; } ) ; } } catch ( e ) { console . error ( e ) ; } return [ ] ; } async function getAlbumBySong ( id , cookie ? ) { try { const { body } = await musicApi . simi_song ( { id , cookie } ) ; if ( body . code === 200 ) { const songs = body . songs ; return songs . map ( ( e ) => { const { album } = e ; return { id : album . id , name : album . name , cover : album . picUrl , link : `/player/1/${ album . id }` , } ; } ) ; } } catch ( e ) { console . error ( e ) ; } return [ ] ; } async function getSimilarUsers ( songid , artistid , cookie ? ) { const [ users , artists ] = await Promise . all ( [ getRecentUser ( songid , cookie ) , getSimilar ( artistid ) ] ) ; return { users , artists , } ; } async function getRecommendPlaylist ( songid ) { const [ playlists1 , playlists2 ] = await Promise . all ( [ getAlbumBySong ( songid ) , getSimilarPlaylist ( songid ) ] ) ; return { playlists : [ ... playlists1 , ... playlists2 ] , } ; } async function getPlayListDetail ( type , id , cookie ? ) { const resData = { } ; let songs = [ ] ; if ( type === '0' ) { const { body } = await musicApi . playlist_detail ( { id , cookie } ) ; const meta = body . playlist ; songs = meta . tracks ; resData . meta = { id , type , name : meta . name , size : meta . trackCount , cover : meta . coverImgUrl , author : [ { id : meta . creator . userId . toString ( ) , name : meta . creator . nickname , link : `/user/${ meta . creator . userId }` , } , ] , played : meta . playCount , subscribed : meta . subscribed , } ; } else { const { body } = await musicApi . album ( { id , cookie } ) ; songs = body . songs ; const meta = body . album ; resData . meta = { name : meta . name , size : meta . size , cover : meta . picUrl , author : meta . artists . map ( ( e ) => ( { id : e . id , name : e . name , link : e . id ? `/artist/${ e . id }` : '' , } ) ) , company : meta . company , subscribed : meta . info . liked , } ; } resData . songs = songs . map ( ( d ) => { const { al , ar } = d ; return { id : d . id , name : d . name , duration : d . dt , album : { id : al . id , name : al . name , cover : `${ al . picUrl }?param=y100y100` , link : `/player/1/${ al . id }` , } , artists : ar . map ( ( e ) => ( { id : e . id , name : e . name , link : e . id ? `/artist/${ e . id }` : '' , } ) ) , } ; } ) ; return resData ; } export { getSongUrl , getPlayListDetail , getSimilarUsers , getRecommendPlaylist } ;