This commit is contained in:
lyswhut
2022-10-29 11:36:35 +08:00
parent 66b965d9f7
commit e45cee6596
715 changed files with 57096 additions and 31771 deletions
+208
View File
@@ -0,0 +1,208 @@
// import { getSongListSetting } from '@renderer/utils/data'
import { deduplicationList, toNewMusicInfo } from '@renderer/utils'
import musicSdk from '@renderer/utils/musicSdk'
import { markRaw, markRawList } from '@common/utils/vueTools'
import {
tags,
TagInfo,
listInfo,
listDetailInfo,
ListDetailInfo,
ListInfoItem,
ListInfo,
selectListInfo,
isVisibleListDetail,
openSongListInputInfo,
} from './state'
const cache = new Map<string, any>()
export const setTags = (tagInfo: TagInfo, source: LX.OnlineSource) => {
tags[source] = markRaw(tagInfo)
}
export const clearList = () => {
listInfo.list = []
listInfo.total = 0
listInfo.noItemLabel = ''
listInfo.page = 1
listInfo.key = ''
}
export const setList = (result: ListInfo, tagId: string, sortId: string, page: number) => {
listInfo.list = markRaw([...result.list])
listInfo.total = result.total
listInfo.limit = result.limit
listInfo.page = page
listInfo.source = result.source
listInfo.tagId = tagId
listInfo.sortId = sortId
if (result.list.length) listInfo.noItemLabel = ''
else listInfo.noItemLabel = window.i18n.t('no_item')
}
export const setListDetail = (result: ListDetailInfo, id: string, page: number) => {
listDetailInfo.list = markRaw([...result.list])
listDetailInfo.id = id
listDetailInfo.source = result.source
listDetailInfo.total = result.total
listDetailInfo.limit = result.limit
listDetailInfo.page = page
listDetailInfo.info = markRaw({ ...result.info })
if (result.list.length) listDetailInfo.noItemLabel = ''
else listDetailInfo.noItemLabel = window.i18n.t('no_item')
}
export const setSelectListInfo = (info: ListInfoItem) => {
selectListInfo.author = info.author
selectListInfo.desc = info.desc
selectListInfo.id = info.id
selectListInfo.img = info.img
selectListInfo.name = info.name
selectListInfo.play_count = info.play_count
selectListInfo.source = info.source
}
export const clearListDetail = () => {
listDetailInfo.list = []
listDetailInfo.id = ''
listDetailInfo.source = 'kw'
listDetailInfo.total = 0
listDetailInfo.limit = 30
listDetailInfo.page = 1
listDetailInfo.key = null
listDetailInfo.info = {}
listDetailInfo.noItemLabel = ''
}
export const getTags = async<T extends LX.OnlineSource>(source: T) => {
return musicSdk[source]?.songList.getTags() as Promise<TagInfo<T>>
}
/**
* 获取歌单列表
* @param source 歌单源
* @param tabId 类型id
* @param sortId 排序
* @param page 页数
* @param isRefresh 是否跳过缓存
* @returns
*/
export const getAndSetList = async(source: LX.OnlineSource, tabId: string, sortId: string, page: number, isRefresh = false) => {
// let source = rootState.setting.songList.source
// let tabId = rootState.setting.songList.tagInfo.id
// let sortId = rootState.setting.songList.sortId
// console.log(sortId)
let key = `slist__${source}__${sortId}__${tabId}__${page}`
// if (state.list.list.length && state.list.key == key) return
if (!isRefresh) {
if (listInfo.key == key && listInfo.list.length) return
if (cache.has(key)) {
listInfo.key = key
setList(cache.get(key), tabId, sortId, page)
return
}
}
listInfo.noItemLabel = window.i18n.t('list__loading')
listInfo.key = key
// clearList()
return musicSdk[source]?.songList.getList(sortId, tabId, page).then((result: ListInfo) => {
cache.set(key, result)
if (key != listInfo.key) return
setList(result, tabId, sortId, page)
}).catch((error: any) => {
clearList()
listInfo.noItemLabel = window.i18n.t('list__load_failed')
console.log(error)
throw error
})
}
/**
* 获取歌单内单页歌曲
* @param id 歌单id
* @param source 歌单源
* @param isRefresh 是否跳过缓存
* @returns
*/
export const getListDetail = async(id: string, source: LX.OnlineSource, page: number, isRefresh = false): Promise<ListDetailInfo> => {
let key = `sdetail__${source}__${id}__${page}`
if (!isRefresh && cache.has(key)) return cache.get(key)
return musicSdk[source]?.songList.getListDetail(id, page).then((result: ListDetailInfo) => {
result.list = markRawList(deduplicationList(result.list.map(m => toNewMusicInfo(m))))
cache.set(key, result)
return result
})
}
/**
* 获取歌单内全部歌曲
* @param id 歌单id
* @param source 歌单源
* @param isRefresh 是否跳过缓存
* @returns
*/
export const getListDetailAll = async(id: string, source: LX.OnlineSource, isRefresh = false): Promise<LX.Music.MusicInfoOnline[]> => {
// console.log(source, id)
// eslint-disable-next-line @typescript-eslint/promise-function-async
const loadData = (id: string, page: number): Promise<ListDetailInfo> => {
let key = `sdetail__${source}__${id}__${page}`
if (isRefresh && cache.has(key)) cache.delete(key)
return cache.has(key)
? Promise.resolve(cache.get(key))
: musicSdk[source]?.songList.getListDetail(id, page).then((result: ListDetailInfo) => {
result.list = markRawList(deduplicationList(result.list.map(m => toNewMusicInfo(m))))
cache.set(key, result)
return result
}) ?? Promise.reject(new Error('source not found'))
}
return await loadData(id, 1).then((result: ListDetailInfo) => {
if (result.total <= result.limit) return result.list
let maxPage = Math.ceil(result.total / result.limit)
// eslint-disable-next-line @typescript-eslint/promise-function-async
const loadDetail = (loadPage = 2): Promise<ListDetailInfo['list']> => {
return loadPage == maxPage
? loadData(id, loadPage).then((result: ListDetailInfo) => result.list)
// eslint-disable-next-line @typescript-eslint/promise-function-async
: loadData(id, loadPage).then((result1: ListDetailInfo) => loadDetail(++loadPage).then((result2: ListDetailInfo['list']) => [...result1.list, ...result2]))
}
return loadDetail().then(result2 => [...result.list, ...result2])
}).then((list: ListDetailInfo['list']) => deduplicationList(list))
}
/**
* 获取并设置歌单内单页歌曲
* @param id 歌单id
* @param source 歌单源
* @param isRefresh 是否跳过缓存
* @returns
*/
export const getAndSetListDetail = async(id: string, source: LX.OnlineSource, page: number, isRefresh = false) => {
let key = `sdetail__${source}__${id}__${page}`
if (!isRefresh && listDetailInfo.key == key && listDetailInfo.list.length) return
listDetailInfo.key = key
listDetailInfo.noItemLabel = window.i18n.t('list__loading')
return getListDetail(id, source, page, isRefresh).then((result: ListDetailInfo) => {
if (key != listDetailInfo.key) return
setListDetail(result, id, page)
}).catch((error: any) => {
clearListDetail()
listDetailInfo.noItemLabel = window.i18n.t('list__load_failed')
console.log(error)
throw error
})
}
export const setVisibleListDetail = (visible: boolean) => {
isVisibleListDetail.value = visible
}
export const setOpenSongListInputInfo = (text: string, source: string) => {
openSongListInputInfo.text = text
openSongListInputInfo.source = source
}
+124
View File
@@ -0,0 +1,124 @@
import { reactive, markRaw, ref, shallowReactive } from '@common/utils/vueTools'
import music from '@renderer/utils/musicSdk'
export declare interface SortInfo {
name: string
id: string
}
export const sources: LX.OnlineSource[] = markRaw([])
export const sortList = markRaw<Partial<Record<LX.OnlineSource, SortInfo[]>>>({})
for (const source of music.sources) {
const songList = music[source.id as LX.OnlineSource].songList
if (!songList) continue
sources.push(source.id as LX.OnlineSource)
sortList[source.id as LX.OnlineSource] = songList.sortList as SortInfo[]
}
export declare interface TagInfoItem<T extends LX.OnlineSource = LX.OnlineSource> {
parent_id: string
parent_name: string
id: string
name: string
source: T
}
export declare interface TagInfoTypeItem<T extends LX.OnlineSource = LX.OnlineSource> {
name: string
list: Array<TagInfoItem<T>>
}
export declare interface TagInfo<Source extends LX.OnlineSource = LX.OnlineSource> {
tags: Array<TagInfoTypeItem<Source>>
hotTag: Array<TagInfoItem<Source>>
source: Source
}
type Tags = Partial<Record<LX.OnlineSource, TagInfo>>
export const tags = shallowReactive<Tags>({})
export declare interface ListInfoItem {
play_count: string
id: string
author: string
name: string
// time: basic.publish_time,
img: string
// grade: basic.favorcnt / 10,
desc: string | null
source: LX.OnlineSource
total?: string
}
export declare interface ListInfo {
list: ListInfoItem[]
total: number
page: number
limit: number
key: string | null
noItemLabel: string
source?: LX.OnlineSource
tagId: string
sortId: string
}
export declare interface ListDetailInfo {
list: LX.Music.MusicInfoOnline[]
source: LX.OnlineSource
desc: string | null
total: number
page: number
limit: number
key: string | null
id: string
info: {
name?: string
img?: string
desc?: string
author?: string
play_count?: string
}
noItemLabel: string
}
export const listInfo = reactive<ListInfo>({
list: [],
total: 0,
page: 1,
limit: 30,
key: null,
noItemLabel: '',
source: 'kw',
tagId: '',
sortId: '',
})
export const listDetailInfo = reactive<ListDetailInfo>({
list: [],
id: '',
desc: null,
total: 0,
page: 1,
limit: 30,
key: null,
source: 'kw',
info: {},
noItemLabel: '',
})
export const selectListInfo = markRaw<ListInfoItem>({
play_count: '',
id: '',
author: '',
name: '',
// time: basic.publish_time,
img: '',
// grade: basic.favorcnt / 10,
desc: '',
source: 'kw',
})
export const isVisibleListDetail = ref(false)
export const openSongListInputInfo = markRaw({
text: '',
source: '',
})