import { useMusicApi } from '../hooks' ; import IAlbum from '/@/interface/IAlbum' ; import Artist from '/@/interface/Artist' ; import IPlayList from '/@/interface/IPlayList' ; import IUserProfile from '/@/interface/IUserProfile' ; const musicApi = useMusicApi ( ) ; interface SearchResult { playlists ? : IPlayList [ ] ; albums ? : IAlbum [ ] ; artists ? : Artist [ ] ; users ? : IUserProfile [ ] ; nextOffset : number ; } async function getPlaylists ( keywords , offset = 0 ) { let playlists = [ ] ; try { const { body } = await musicApi . search ( { offset , keywords , limit : 30 , type : 1000 , } ) ; if ( body . code === 200 ) { const result = body . result ; playlists = result . playlists . map ( ( e ) => { const { creator } = e ; return { id : e . id , name : e . name , cover : e . coverImgUrl , played : e . playCount , star : e . bookCount , size : e . trackCount , link : `/player/0/${ e . id }` , creator : creator ? { id : creator . id , name : creator . name , link : `/user/${ creator . id }` , } : { } , } ; } ) ; } } catch ( e ) { console . error ( e ) ; } return { playlists , nextOffset : offset + 30 , } ; } async function getAlbums ( keywords , offset = 0 ) { let albums = [ ] ; try { const { body } = await musicApi . search ( { offset , keywords , limit : 30 , type : 10 , } ) ; if ( body . code === 200 ) { const result = body . result ; albums = result . albums . map ( ( e ) => { const { artist } = e ; return { id : e . id , name : e . name , cover : e . picUrl , publishTime : e . publishTime , size : e . size , link : `/player/1/${ e . id }` , artist : { id : artist . id , name : artist . name , link : `/artist/${ artist . id }` , } , } ; } ) ; } } catch ( e ) { console . error ( e ) ; } return { albums , nextOffset : offset + 30 , } ; } async function getArtists ( keywords , offset = 0 ) { let artists = [ ] ; try { const { body } = await musicApi . search ( { offset , keywords , limit : 30 , type : 100 , } ) ; if ( body . code === 200 ) { const result = body . result ; artists = result . artists . map ( ( e ) => { return { id : e . id , name : e . name , avatar : e . picUrl || 'https://p3.music.126.net/VnZiScyynLG7atLIZ2YPkw==/18686200114669622.jpg?param=100y100' , followed : e . followed , size : e . albumSize , link : `/artist/${ e . id }` , } ; } ) ; } } catch ( e ) { console . error ( e ) ; } return { artists , nextOffset : offset + 30 , } ; } async function getUsers ( keywords , offset = 0 ) { let users = [ ] ; try { const { body } = await musicApi . search ( { offset , keywords , limit : 30 , type : 1002 , } ) ; if ( body . code === 200 ) { const result = body . result ; users = result . userprofiles . map ( ( e ) => { return { id : e . userId , name : e . nickname , avatar : e . avatarUrl , link : `/user/${ e . userId }` , } ; } ) ; } } catch ( e ) { console . error ( e ) ; } return { users , nextOffset : offset + 30 , } ; } async function searchByType ( type , keyword , offset ? ) { let data ; switch ( type . toString ( ) ) { case '1000' : data = await getPlaylists ( keyword , offset ) ; break ; case '10' : data = await getAlbums ( keyword , offset ) ; break ; case '100' : data = await getArtists ( keyword , offset ) ; break ; case '1002' : data = await getUsers ( keyword , offset ) ; break ; default : data = { nextOffset : 0 } ; } return data ; } export default searchByType ;