From 6dca0c157f203cb72bfe38c6a0b1aa01d7876479 Mon Sep 17 00:00:00 2001 From: Aqr-K <95741669+Aqr-K@users.noreply.github.com> Date: Fri, 6 Dec 2024 23:44:04 +0800 Subject: [PATCH 1/2] fix(rule): bug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 替换错误的规则校验方法。 --- src/views/setting/AccountSettingRule.vue | 71 +++++++++--------------- 1 file changed, 27 insertions(+), 44 deletions(-) diff --git a/src/views/setting/AccountSettingRule.vue b/src/views/setting/AccountSettingRule.vue index ded66741..f7f0b15d 100644 --- a/src/views/setting/AccountSettingRule.vue +++ b/src/views/setting/AccountSettingRule.vue @@ -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([]) -// 自定义规则类型,仅用于导入判断 -const customRulesType = ref({ - // 规则ID - id: '', - // 名称 - name: '', - // 包含 - include: '', - // 排除 - exclude: '', - // 大小范围 - size_range: '', - // 最少做种人数 - seeders: '', - // 发布时间 - publish_time: '', -}) // 所有规则组列表 const filterRuleGroups = ref([]) -// 规则组类型,仅用于导入判断 -const filterRuleGroupsType = ref({ - // 名称 - name: '', - // 规则串 - rule_string: '', - // 适用类媒体类型 None-全部 电影/电视剧 - media_type: '', - // # 适用媒体类别 None-全部 对应二级分类 - category: '', -}) // 种子优先规则 const selectedTorrentPriority = ref('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 } // 规则变化时赋值 From 59a7607c07410388fff61d4eae68b1c92ba48fdc Mon Sep 17 00:00:00 2001 From: Aqr-K <95741669+Aqr-K@users.noreply.github.com> Date: Sat, 7 Dec 2024 05:28:09 +0800 Subject: [PATCH 2/2] Update AccountSettingRule.vue --- src/views/setting/AccountSettingRule.vue | 61 +++++++++++++----------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/src/views/setting/AccountSettingRule.vue b/src/views/setting/AccountSettingRule.vue index f7f0b15d..d28ce66f 100644 --- a/src/views/setting/AccountSettingRule.vue +++ b/src/views/setting/AccountSettingRule.vue @@ -188,15 +188,11 @@ function saveCodeString(type: string, codeString: any) { // 更新数据 try { if (type === 'custom') { - for (const value of parsedCode) { - if (!checkValueValidity(value, type)) return false - } + if (!checkValueValidity(parsedCode, type)) return false const newCustomRules = extractCustomRules(parsedCode) || [] customRules.value = [...customRules.value, ...newCustomRules] } else if (type === 'group') { - for (const value of parsedCode) { - if (!checkValueValidity(value, type)) return false - } + if (!checkValueValidity(parsedCode, type)) return false const newFilterRuleGroups = extractFilterRuleGroups(parsedCode) || [] filterRuleGroups.value = [...filterRuleGroups.value, ...newFilterRuleGroups] } else { @@ -241,33 +237,40 @@ function extractFilterRuleGroups(value: any) { } // 根据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) +function checkValueValidity(values: any, type: string): boolean { + try { + // 允许空值存在,不影响最终的导入 + if (!values) return true + if (!type) return false + for (const value of values) { + 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 + 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 + } + } else if (type == 'group') { + if (!hasName || hasId || !noDuplicates) { + if (!noDuplicates) $toast.warning(`存在重名值`) + if (hasId) $toast.error(`导入失败!发现有规则存在ID,可能属于自定义规则!`) + return false + } + } else { + console.error(`传入了不合法类型`) + return false + } } - } else if (type == 'group') { - if (!hasName || hasId || !noDuplicates) { - if (!noDuplicates) $toast.warning(`存在重名值`) - if (hasId) $toast.error(`导入失败!发现有规则存在ID,可能属于自定义规则!`) - return false - } - } else { - console.error(`传入了不合法类型`) + return true + } catch (e) { + console.error(e) return false } - return true } // 规则变化时赋值