修复过滤的问题

This commit is contained in:
thofx
2023-08-12 20:17:54 +08:00
parent e14ce862c7
commit 6d3aa3f052
+115 -255
View File
@@ -1,5 +1,5 @@
<script lang="ts" setup> <script lang="ts" setup>
import { isIntersected } from '@/@core/utils' import { ref } from 'vue'
import api from '@/api' import api from '@/api'
import type { Context } from '@/api/types' import type { Context } from '@/api/types'
import TorrentCard from '@/components/cards/TorrentCard.vue' import TorrentCard from '@/components/cards/TorrentCard.vue'
@@ -15,13 +15,15 @@ const props = defineProps({
type: String, type: String,
}) })
interface SearchTorrent extends Context {
more?: Array<Context>
}
// 数据列表 // 数据列表
const dataList = ref<Context[]>([]) const dataList = ref <Array<SearchTorrent>>([])
// 分组后的数据列表 // 分组后的数据列表
const groupedDataList = computed(() => { const groupedDataList = ref<Map<string, Context[]>>()
return groupByTitleAndSize(dataList.value)
})
// 是否刷新过 // 是否刷新过
const isRefreshed = ref(false) const isRefreshed = ref(false)
@@ -60,171 +62,78 @@ const filterForm = reactive({
}) })
// 获取站点过滤选项 // 获取站点过滤选项
const getSiteFilterOptions = computed(() => { const siteFilterOptions = ref<Array<string>>([])
const options: string[] = []
dataList.value.forEach((data) => {
if (data.torrent_info?.site_name && !options.includes(data.torrent_info?.site_name))
options.push(data.torrent_info?.site_name)
})
return options
})
// 获取季过滤选项 // 获取季过滤选项
const getSeasonFilterOptions = computed(() => { const seasonFilterOptions = ref<Array<string>>([])
const options: string[] = []
dataList.value.forEach((data) => {
if (
data.meta_info.season_episode
&& !options.includes(data.meta_info.season_episode)
)
options.push(data.meta_info.season_episode)
})
return options
})
// 获取制作组过滤选项 // 获取制作组过滤选项
const getReleaseGroupFilterOptions = computed(() => { const releaseGroupFilterOptions = ref<Array<string>>([])
const options: string[] = []
dataList.value.forEach((data) => {
if (data.meta_info.resource_team && !options.includes(data.meta_info.resource_team))
options.push(data.meta_info.resource_team)
})
return options
})
// 获取视频编码过滤选项 // 获取视频编码过滤选项
const getVideoCodeFilterOptions = computed(() => { const videoCodeFilterOptions = ref<Array<string>>([])
const options: string[] = []
dataList.value.forEach((data) => {
if (data.meta_info.video_encode && !options.includes(data.meta_info.video_encode))
options.push(data.meta_info.video_encode)
})
return options
})
// 获取促销状态过滤选项 // 获取促销状态过滤选项
const getFreeStateFilterOptions = computed(() => { const freeStateFilterOptions = ref<Array<string>>([])
const options: string[] = []
dataList.value.forEach((data) => {
if (
data.torrent_info.volume_factor
&& !options.includes(data.torrent_info.volume_factor)
)
options.push(data.torrent_info.volume_factor)
})
return options
})
// 获取质量过滤选项 // 获取质量过滤选项
const getEditionFilterOptions = computed(() => { const editionFilterOptions = ref<Array<string>>([])
const options: string[] = []
dataList.value.forEach((data) => {
if (data.meta_info.edition && !options.includes(data.meta_info.edition))
options.push(data.meta_info.edition)
})
return options
})
// 获取分辨率过滤选项 // 获取分辨率过滤选项
const getResolutionFilterOptions = computed(() => { const resolutionFilterOptions = ref<Array<string>>([])
const options: string[] = []
dataList.value.forEach((data) => {
if (data.meta_info.resource_pix && !options.includes(data.meta_info.resource_pix))
options.push(data.meta_info.resource_pix)
})
return options
})
// 按过滤项过滤卡片 // 按过滤项过滤卡片
// eslint-disable-next-line sonarjs/cognitive-complexity watchEffect(() => {
function filterTorrentsCard(data: Context) { // 清空数据
// 当前分组的所有数据 dataList.value.splice(0)
const items: Context[]
= groupedDataList.value.get(`${data.torrent_info.title}_${data.torrent_info.size}`)
?? []
// 站点名称、促销状态 const match = (filter: Array<string>, value: string | undefined) =>
const site_names = [] filter.length === 0 || (value && filter.includes(value))
const volume_factors = []
for (const { torrent_info } of items) {
site_names.push(torrent_info.site_name)
volume_factors.push(torrent_info.volume_factor)
}
const { meta_info } = data groupedDataList.value?.forEach((value) => {
if (value.length > 0) {
const matchData = value.filter((data) => {
const { meta_info, torrent_info } = data
// 季、制作组、视频编码
const { season_episode, resource_team, video_encode } = meta_info
return (
// 站点过滤
match(filterForm.site, torrent_info.site_name)
// 促销状态过滤
&& match(filterForm.freeState, torrent_info.volume_factor)
// 季过滤
&& match(filterForm.season, season_episode)
// 制作组过滤
&& match(filterForm.releaseGroup, resource_team)
// 视频编码过滤
&& match(filterForm.videoCode, video_encode)
// 分辨率过滤
&& match(filterForm.resolution, meta_info.resource_pix)
// 质量过滤
&& match(filterForm.edition, meta_info.edition)
)
})
if (matchData.length > 0) {
const firstData = matchData[0] as SearchTorrent
if (matchData.length > 1)
firstData.more = matchData.slice(1)
// 季、制作组、视频编码 dataList.value.push(firstData)
const { season_episode, resource_team, video_encode } = meta_info }
}
// 站点过滤 })
if (filterForm.site.length > 0 && !isIntersected(filterForm.site, site_names)) })
return false
// 促销状态过滤
if (
filterForm.freeState.length > 0
&& !isIntersected(filterForm.freeState, volume_factors)
)
return false
// 季过滤
if (filterForm.season.length > 0 && !filterForm.season.includes(season_episode))
return false
// 制作组过滤
if (
filterForm.releaseGroup.length > 0
&& !filterForm.releaseGroup.includes(resource_team || '')
)
return false
// 视频编码过滤
if (
filterForm.videoCode.length > 0
&& !filterForm.videoCode.includes(video_encode || '')
)
return false
// 分辨率过滤
if (
filterForm.resolution.length > 0
&& !filterForm.resolution.includes(meta_info.resource_pix || '')
)
return false
// 质量过滤
return !(filterForm.edition.length > 0 && !filterForm.edition.includes(meta_info.edition))
}
// 获取订阅列表数据 // 获取订阅列表数据
async function fetchData() { async function fetchData(): Promise<Array<Context>> {
try { try {
let searchData: Array<Context>
const keyword = props.keyword ?? '' const keyword = props.keyword ?? ''
const mtype = props.type ?? '' const mtype = props.type ?? ''
if (!keyword) { if (!keyword) {
// 查询上次搜索结果 // 查询上次搜索结果
dataList.value = await api.get('search/last') searchData = await api.get('search/last')
} }
else { else {
startLoadingProgress() startLoadingProgress()
const qualify = props.keyword?.startsWith('tmdb:') || props.keyword?.startsWith('douban:')
// 优先按TMDBID精确查询 // 优先按TMDBID精确查询
if (props.keyword?.startsWith('tmdb:') || props.keyword?.startsWith('douban:')) { if (qualify) {
dataList.value = await api.get(`search/media/${props.keyword}`, { searchData = await api.get(`search/media/${props.keyword}`, {
params: { params: {
mtype, mtype,
}, },
@@ -232,51 +141,57 @@ async function fetchData() {
} }
else { else {
// 按标题模糊查询 // 按标题模糊查询
dataList.value = await api.get(`search/title/${props.keyword}`) searchData = await api.get(`search/title/${props.keyword}`)
} }
stopLoadingProgress() stopLoadingProgress()
} }
isRefreshed.value = true isRefreshed.value = true
return Promise.resolve(searchData)
} }
catch (error) { catch (error) {
console.error(error) console.error(error)
return Promise.reject(error)
} }
} }
// 按标题和大小分组 function initData() {
function groupByTitleAndSize(contextArray: Context[]): Map<string, Context[]> { // load data
const groupMap = new Map<string, Context[]>() fetchData().then((data) => {
const groupMap = new Map<string, Context[]>()
for (const context of contextArray) { data.forEach((item) => {
const { torrent_info } = context const { torrent_info } = item
const key = `${torrent_info.title}_${torrent_info.size}` // init options
initOptions(item)
if (groupMap.has(key)) { // group data
// 已存在相同标题和大小的分组,将当前上下文信息添加到分组中 const key = `${torrent_info.title}_${torrent_info.size}`
const group = groupMap.get(key) if (groupMap.has(key)) {
// 已存在相同标题和大小的分组,将当前上下文信息添加到分组中
group?.push(context) const group = groupMap.get(key)
} group?.push(item)
else { }
// 创建新的分组,并将当前上下文信息添加到分组中 else {
groupMap.set(key, [context]) // 创建新的分组,并将当前上下文信息添加到分组中
} groupMap.set(key, [item])
} }
})
return groupMap groupedDataList.value = groupMap
}
// 获取每个分组的第一个数据
const getFirstContexts = computed(() => {
const firstContexts: Context[] = []
groupedDataList.value.forEach((group) => {
if (group.length > 0)
firstContexts.push(group[0])
}) })
}
return firstContexts function initOptions(data: Context) {
}) const { torrent_info, meta_info } = data
const optionValue = (options: Array<string>, value: string | undefined) => {
value && !options.includes(value) && options.push(value)
}
optionValue(siteFilterOptions.value, torrent_info?.site_name)
optionValue(seasonFilterOptions.value, meta_info?.season_episode)
optionValue(releaseGroupFilterOptions.value, meta_info?.resource_team)
optionValue(videoCodeFilterOptions.value, meta_info?.video_encode)
optionValue(freeStateFilterOptions.value, torrent_info?.volume_factor)
optionValue(editionFilterOptions.value, meta_info?.edition)
optionValue(resolutionFilterOptions.value, meta_info?.resource_pix)
}
// 使用SSE监听加载进度 // 使用SSE监听加载进度
function startLoadingProgress() { function startLoadingProgress() {
@@ -302,20 +217,16 @@ function stopLoadingProgress() {
} }
// 加载时获取数据 // 加载时获取数据
onBeforeMount(fetchData) onMounted(initData)
</script> </script>
<template> <template>
<VCard class="bg-transparent mb-3 pt-2 shadow-none"> <VCard class="bg-transparent mb-3 pt-2 shadow-none">
<VRow> <VRow>
<VCol <VCol v-if="siteFilterOptions.length > 0" cols="6" md="">
v-if="getSiteFilterOptions.length > 0"
cols="6"
md=""
>
<VSelect <VSelect
v-model="filterForm.site" v-model="filterForm.site"
:items="getSiteFilterOptions" :items="siteFilterOptions"
size="small" size="small"
density="compact" density="compact"
chips chips
@@ -323,14 +234,10 @@ onBeforeMount(fetchData)
multiple multiple
/> />
</VCol> </VCol>
<VCol <VCol v-if="seasonFilterOptions.length > 0" cols="6" md="">
v-if="getSeasonFilterOptions.length > 0"
cols="6"
md=""
>
<VSelect <VSelect
v-model="filterForm.season" v-model="filterForm.season"
:items="getSeasonFilterOptions" :items="seasonFilterOptions"
size="small" size="small"
density="compact" density="compact"
chips chips
@@ -338,14 +245,10 @@ onBeforeMount(fetchData)
multiple multiple
/> />
</VCol> </VCol>
<VCol <VCol v-if="releaseGroupFilterOptions.length > 0" cols="6" md="">
v-if="getReleaseGroupFilterOptions.length > 0"
cols="6"
md=""
>
<VSelect <VSelect
v-model="filterForm.releaseGroup" v-model="filterForm.releaseGroup"
:items="getReleaseGroupFilterOptions" :items="releaseGroupFilterOptions"
size="small" size="small"
density="compact" density="compact"
chips chips
@@ -353,14 +256,10 @@ onBeforeMount(fetchData)
multiple multiple
/> />
</VCol> </VCol>
<VCol <VCol v-if="editionFilterOptions.length > 0" cols="6" md="">
v-if="getEditionFilterOptions.length > 0"
cols="6"
md=""
>
<VSelect <VSelect
v-model="filterForm.edition" v-model="filterForm.edition"
:items="getEditionFilterOptions" :items="editionFilterOptions"
size="small" size="small"
density="compact" density="compact"
chips chips
@@ -368,14 +267,10 @@ onBeforeMount(fetchData)
multiple multiple
/> />
</VCol> </VCol>
<VCol <VCol v-if="resolutionFilterOptions.length > 0" cols="6" md="">
v-if="getResolutionFilterOptions.length > 0"
cols="6"
md=""
>
<VSelect <VSelect
v-model="filterForm.resolution" v-model="filterForm.resolution"
:items="getResolutionFilterOptions" :items="resolutionFilterOptions"
size="small" size="small"
density="compact" density="compact"
chips chips
@@ -383,14 +278,10 @@ onBeforeMount(fetchData)
multiple multiple
/> />
</VCol> </VCol>
<VCol <VCol v-if="videoCodeFilterOptions.length > 0" cols="6" md="">
v-if="getVideoCodeFilterOptions.length > 0"
cols="6"
md=""
>
<VSelect <VSelect
v-model="filterForm.videoCode" v-model="filterForm.videoCode"
:items="getVideoCodeFilterOptions" :items="videoCodeFilterOptions"
size="small" size="small"
density="compact" density="compact"
chips chips
@@ -398,14 +289,10 @@ onBeforeMount(fetchData)
multiple multiple
/> />
</VCol> </VCol>
<VCol <VCol v-if="freeStateFilterOptions.length > 0" cols="6" md="">
v-if="getFreeStateFilterOptions.length > 0"
cols="6"
md=""
>
<VSelect <VSelect
v-model="filterForm.freeState" v-model="filterForm.freeState"
:items="getFreeStateFilterOptions" :items="freeStateFilterOptions"
size="small" size="small"
density="compact" density="compact"
chips chips
@@ -415,40 +302,13 @@ onBeforeMount(fetchData)
</VCol> </VCol>
</VRow> </VRow>
</VCard> </VCard>
<div <div v-if="!isRefreshed" class="mt-12 w-full text-center text-gray-500 text-sm flex flex-col items-center">
v-if="!isRefreshed" <VProgressCircular v-if="!props.keyword" size="48" indeterminate color="primary" />
class="mt-12 w-full text-center text-gray-500 text-sm flex flex-col items-center" <VProgressCircular v-if="props.keyword" class="mb-3" color="primary" :model-value="progressValue" size="64" />
>
<VProgressCircular
v-if="!props.keyword"
size="48"
indeterminate
color="primary"
/>
<VProgressCircular
v-if="props.keyword"
class="mb-3"
color="primary"
:model-value="progressValue"
size="64"
/>
<span>{{ progressText }}</span> <span>{{ progressText }}</span>
</div> </div>
<div <div v-if="dataList.length > 0" class="grid gap-3 grid-torrent-card items-start">
v-if="dataList.length > 0" <TorrentCard v-for="data in dataList" :key="`${data.torrent_info.title}_${data.torrent_info.site}`" :torrent="data" :more="data.more" />
class="grid gap-3 grid-torrent-card items-start"
>
<TorrentCard
v-for="data in getFirstContexts"
v-show="filterTorrentsCard(data)"
:key="data.torrent_info.title"
:torrent="data"
:more="
groupedDataList
.get(`${data.torrent_info.title}_${data.torrent_info.size}`)
?.slice(1)
"
/>
</div> </div>
<NoDataFound <NoDataFound
v-if="dataList.length === 0 && isRefreshed" v-if="dataList.length === 0 && isRefreshed"