fix(rule): bug

- 替换错误的规则校验方法。
This commit is contained in:
Aqr-K
2024-12-06 23:44:04 +08:00
committed by GitHub
parent d2aa5a64aa
commit 6dca0c157f

View File

@@ -8,41 +8,12 @@ import { CustomRule, FilterRuleGroup } from '@/api/types'
import CustomerRuleCard from '@/components/cards/CustomRuleCard.vue'
import FilterRuleGroupCard from '@/components/cards/FilterRuleGroupCard.vue'
import ImportCodeDialog from '@/components/dialog/ImportCodeDialog.vue'
import internal from 'stream'
// 自定义规则列表
const customRules = ref<CustomRule[]>([])
// 自定义规则类型,仅用于导入判断
const customRulesType = ref<CustomRule>({
// 规则ID
id: '',
// 名称
name: '',
// 包含
include: '',
// 排除
exclude: '',
// 大小范围
size_range: '',
// 最少做种人数
seeders: '',
// 发布时间
publish_time: '',
})
// 所有规则组列表
const filterRuleGroups = ref<FilterRuleGroup[]>([])
// 规则组类型,仅用于导入判断
const filterRuleGroupsType = ref<FilterRuleGroup>({
// 名称
name: '',
// 规则串
rule_string: '',
// 适用类媒体类型 None-全部 电影/电视剧
media_type: '',
// # 适用媒体类别 None-全部 对应二级分类
category: '',
})
// 种子优先规则
const selectedTorrentPriority = ref<string>('seeder')
@@ -218,13 +189,13 @@ function saveCodeString(type: string, codeString: any) {
try {
if (type === 'custom') {
for (const value of parsedCode) {
if (!validateValueAgainstInterface(value, customRulesType)) return false
if (!checkValueValidity(value, type)) return false
}
const newCustomRules = extractCustomRules(parsedCode) || []
customRules.value = [...customRules.value, ...newCustomRules]
} else if (type === 'group') {
for (const value of parsedCode) {
if (!validateValueAgainstInterface(value, filterRuleGroupsType)) return false
if (!checkValueValidity(value, type)) return false
}
const newFilterRuleGroups = extractFilterRuleGroups(parsedCode) || []
filterRuleGroups.value = [...filterRuleGroups.value, ...newFilterRuleGroups]
@@ -269,22 +240,34 @@ function extractFilterRuleGroups(value: any) {
}
}
function validateValueAgainstInterface(value: any, interfaceDefinition: any): boolean {
console.log(value)
console.log(interfaceDefinition)
try {
// 循环判断是否命中全部接口定义,暂时只检查是否存在,不检查是否多出
for (const key in interfaceDefinition.value) {
if (value[key] === undefined) {
$toast.error(`导入规则失败!输入了不符合要求的数据!`)
return false
}
// 根据ID简单区分规则与规则组
function checkValueValidity(value: any, type: string): boolean {
// 允许空值存在,不影响最终的导入
if (!value) return true
if (!type) return false
const keys = Object.keys(value)
const uniqueKeys = new Set(keys)
const hasName = keys.includes('name')
const hasId = keys.includes('id')
const noDuplicates = keys.length === uniqueKeys.size
if (type == 'custom') {
if (!hasName || !hasId || !noDuplicates) {
if (!noDuplicates) $toast.warning(`存在重名值`)
if (!hasId) $toast.error(`导入失败发现有规则不存在ID可能属于优先级规则组`)
return false
}
return true
} catch (e) {
console.error(e)
} else if (type == 'group') {
if (!hasName || hasId || !noDuplicates) {
if (!noDuplicates) $toast.warning(`存在重名值`)
if (hasId) $toast.error(`导入失败发现有规则存在ID可能属于自定义规则`)
return false
}
} else {
console.error(`传入了不合法类型`)
return false
}
return true
}
// 规则变化时赋值