Fix list with same names (fix #5151)

This commit is contained in:
the1812 2025-03-10 23:48:55 +08:00
parent da6c829296
commit 91cad58d51

View File

@ -18,14 +18,11 @@
<div class="lists"> <div class="lists">
选择快速收藏夹: 选择快速收藏夹:
<VDropdown <VDropdown
v-model="selectedFavorite" v-model="selectedFavoriteList"
:items="lists.map(it => it.title)" :items="list"
:key-mapper="it => it" :key-mapper="it => it.id"
> @change="saveFavoriteList"
<template #item="{ item }"> />
{{ item }}
</template>
</VDropdown>
</div> </div>
<div class="lists-tip" :class="{ show: listShowing }">右键点击快速收藏可再次打开</div> <div class="lists-tip" :class="{ show: listShowing }">右键点击快速收藏可再次打开</div>
</div> </div>
@ -42,6 +39,20 @@ import { VDropdown } from '@/ui'
import { DisplayMode, Options } from './options' import { DisplayMode, Options } from './options'
const { options } = getComponentSettings('quickFavorite') const { options } = getComponentSettings('quickFavorite')
interface RawFavoriteListItem {
id: number
title: string
fav_state: number
}
interface FavoriteListItem {
id: number
displayName: string
}
const EmptyFavoriteList: FavoriteListItem = {
id: 0,
displayName: '<未选择>',
}
export default Vue.extend({ export default Vue.extend({
components: { components: {
VDropdown, VDropdown,
@ -50,13 +61,12 @@ export default Vue.extend({
const { displayMode } = getComponentSettings<Options>('outerWatchlater').options const { displayMode } = getComponentSettings<Options>('outerWatchlater').options
return { return {
aid: unsafeWindow.aid, aid: unsafeWindow.aid,
favoriteTitle: '',
isFavorite: false, isFavorite: false,
tipText: '', tipText: '',
tipShowing: false, tipShowing: false,
tipHandle: 0, tipHandle: 0,
lists: [], list: [],
selectedFavorite: '<未选择>', selectedFavoriteList: EmptyFavoriteList,
listShowing: false, listShowing: false,
displayMode, displayMode,
} }
@ -70,21 +80,6 @@ export default Vue.extend({
}, },
}, },
watch: { watch: {
selectedFavorite(value: string) {
if (this.lists.length === 0) {
return
}
const { lists } = this as {
lists: { title: string; id: number }[]
}
const list = lists.find(it => it.title === value)
if (list) {
options.favoriteFolderID = list.id
this.syncFavoriteState()
} else {
console.error('list not found in selectedFavorite(value)')
}
},
async listShowing(value: boolean) { async listShowing(value: boolean) {
if (value) { if (value) {
document.addEventListener('click', e => { document.addEventListener('click', e => {
@ -94,29 +89,63 @@ export default Vue.extend({
this.listShowing = false this.listShowing = false
} }
}) })
if (this.lists.length === 0) { if (this.list.length === 0) {
try { this.loadFavoriteList()
const json = await getJsonWithCredentials(
`https://api.bilibili.com/medialist/gateway/base/created?pn=1&ps=100&up_mid=${getUID()}&is_space=0`,
)
if (json.code !== 0) {
throw new Error(`获取收藏夹列表失败: ${json.message}`)
}
this.lists = lodash.get(json, 'data.list', [])
} catch (error) {
logError(error)
}
} }
} }
}, },
}, },
created() { created() {
this.syncFavoriteState() this.loadSavedList()
addComponentListener('quickFavorite.displayMode', (value: DisplayMode) => { addComponentListener('quickFavorite.displayMode', (value: DisplayMode) => {
this.displayMode = value this.displayMode = value
}) })
}, },
methods: { methods: {
async loadFavoriteList() {
try {
const json = await getJsonWithCredentials(
`https://api.bilibili.com/medialist/gateway/base/created?pn=1&ps=100&up_mid=${getUID()}&is_space=0`,
)
if (json.code !== 0) {
throw new Error(`获取收藏夹列表失败: ${json.message}`)
}
const list: RawFavoriteListItem[] = lodash.get(json, 'data.list', [])
this.list = list.map(it => ({ id: it.id, displayName: it.title }))
} catch (error) {
logError(error)
}
},
async loadSavedList() {
try {
const json = await getJsonWithCredentials(
`https://api.bilibili.com/x/v3/fav/folder/created/list-all?type=2&rid=${
this.aid
}&up_mid=${getUID()}`,
)
if (json.code !== 0) {
throw new Error(`获取收藏状态失败: ${json.message}`)
}
const list: RawFavoriteListItem[] = lodash.get(json, 'data.list', [])
const favoriteFolder = list.find(it => it.id === options.favoriteFolderID)
if (favoriteFolder === undefined) {
options.favoriteFolderID = 0
return
}
this.isFavorite = Boolean(favoriteFolder.fav_state)
this.selectedFavoriteList = {
id: favoriteFolder.id,
displayName: favoriteFolder.title,
} as FavoriteListItem
} catch (error) {
logError(error)
}
},
saveFavoriteList(list: FavoriteListItem) {
options.favoriteFolderID = list.id
this.syncFavoriteState()
},
async syncFavoriteState() { async syncFavoriteState() {
if (options.favoriteFolderID === 0 || !this.aid) { if (options.favoriteFolderID === 0 || !this.aid) {
return return
@ -135,14 +164,16 @@ export default Vue.extend({
'data.list', 'data.list',
[], [],
) )
const quickList = list.find(it => it.id === options.favoriteFolderID) const favoriteFolder = list.find(it => it.id === options.favoriteFolderID)
if (quickList === undefined) { if (favoriteFolder === undefined) {
options.favoriteFolderID = 0 options.favoriteFolderID = 0
return return
} }
this.isFavorite = Boolean(quickList.fav_state) this.isFavorite = Boolean(favoriteFolder.fav_state)
this.favoriteTitle = quickList.title this.selectedFavoriteList = {
this.selectedFavorite = quickList.title id: favoriteFolder.id,
displayName: favoriteFolder.title,
} as FavoriteListItem
} catch (error) { } catch (error) {
logError(error) logError(error)
} }
@ -182,8 +213,8 @@ export default Vue.extend({
this.isFavorite = !this.isFavorite this.isFavorite = !this.isFavorite
this.showTip( this.showTip(
this.isFavorite this.isFavorite
? `已添加至收藏夹: ${this.favoriteTitle}` ? `已添加至收藏夹: ${this.selectedFavoriteList.displayName}`
: `已移出收藏夹: ${this.favoriteTitle}`, : `已移出收藏夹: ${this.selectedFavoriteList.displayName}`,
) )
// await this.syncFavoriteState() // await this.syncFavoriteState()
} catch (error) { } catch (error) {