import { globalCache , filestore } from '@/db' ; import { ajax , ajax2 } from '@/utils' ; import { FollowingResult , Following } from '@/models' ; author CaoMeiYouRen
 *  date export param { number } uid returns export async function getUsernameFromUID ( uid ) { if ( typeof uid !== 'number' || uid <= 0 ) { return '' ; } const key = `bili-username-from-uid-${ uid }` ; let name = await globalCache . get ( key ) || '' ; if ( ! name ) { const result = await ajax ( 'https://api.bilibili.com/x/space/acc/info' , { mid : uid , jsonp : 'jsonp' , } , { } , 'GET' , { Referer : `https://space.bilibili.com/${ uid }/` , } ) ; if ( result . data ?. code === 0 ) { name = result . data ?. data ?. name ; await globalCache . set ( key , name , 3600 * 24 * 7 ) ; } } return name ; } author CaoMeiYouRen
 *  date export param { number } uid returns export async function getRoomIdFromUID ( uid ) { if ( typeof uid !== 'number' || uid <= 0 ) { return 0 ; } const key = `bili-roomid-from-uid-${ uid }` ; let roomid = Number ( await filestore . get ( key ) || 0 ) ; if ( ! roomid ) { const result = await ajax ( 'https://api.live.bilibili.com/room/v1/Room/getRoomInfoOld' , { mid : uid , } , { } , 'GET' , { Referer : `https://space.bilibili.com/${ uid }/` , } ) ; if ( result . data ?. code === 0 ) { roomid = result . data ?. data ?. roomid ; await filestore . set ( key , roomid ) ; } } return roomid ; } author CaoMeiYouRen
 *  date export param { number } uid returns { Promise < boolean > } export async function isValidUser ( uid ) { return Boolean ( await getUsernameFromUID ( uid ) ) ; } author CaoMeiYouRen
 *  date export param { number } uid export async function getAllFollowings ( uid , tag ? ) { const m = await getFollowingNumber ( uid ) ; if ( m <= 0 ) { return [ ] ; } const n = Math . ceil ( m / 50 ) % 5 + 1 ; let followings = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { const temp = await getFollowings ( uid , i + 1 , tag ) ; followings = followings . concat ( temp ) ; } return followings ; } author CaoMeiYouRen
 *  date export param { number } uid param { number } pn param { number } tag export async function getFollowings ( uid , pn , tag ? ) { const result = await ajax2 ( { url : 'https://api.bilibili.com/x/relation/followings' , query : { vmid : uid , pn , } , headers : { Referer : `https://space.bilibili.com/${ uid }/` , } , } ) ; if ( result . data ?. code !== 0 ) { return [ ] ; } const data = result . data ; return data ?. data ?. list . filter ( ( e ) => { if ( ! tag ) { return true ; } return e ?. tag ?. includes ( tag ) ; } ) ; } author CaoMeiYouRen
 *  date export param { number } uid returns { Promise < number > } export async function getFollowingNumber ( uid ) { const result = await ajax2 ( { url : 'https://api.bilibili.com/x/relation/stat' , query : { vmid : uid , } , headers : { Referer : `https://space.bilibili.com/${ uid }/` , } , } ) ; return result . data ?. data ?. following || 0 ; }