关于居民健康档案首页表自动化输入的代码记录

在控制台里实现自动化输入

(async function() {
    // ===== 1. 通用工具函数 =====
    // 通用函数:通过label文本来设置输入框值(增强版)
    function setInputByLabel(labelText, value) {
        const labels = document.querySelectorAll('label');
        for (const label of labels) {
            if (label.textContent.trim() === labelText) {
                // 尝试1:通过label的for属性找到对应的input
                const inputId = label.getAttribute('for');
                if (inputId) {
                    const input = document.getElementById(inputId);
                    if (input) {
                        input.value = value;
                        input.dispatchEvent(new Event('input', { bubbles: true, cancelable: true }));
                        console.log(`✓ 已设置 "${labelText}" 为: ${value}`);
                        return true;
                    }
                }
                
                // 尝试2:在label的父元素中查找input
                const parent = label.closest('.v-input__slot, .v-text-field__slot');
                if (parent) {
                    const input = parent.querySelector('input');
                    if (input) {
                        input.value = value;
                        input.dispatchEvent(new Event('input', { bubbles: true, cancelable: true }));
                        console.log(`✓ 已设置 "${labelText}" 为: ${value}`);
                        return true;
                    }
                }
                
                // 尝试3:在label的相邻元素中查找input
                const nextElement = label.nextElementSibling;
                if (nextElement) {
                    const input = nextElement.querySelector('input');
                    if (input) {
                        input.value = value;
                        input.dispatchEvent(new Event('input', { bubbles: true, cancelable: true }));
                        console.log(`✓ 已设置 "${labelText}" 为: ${value}`);
                        return true;
                    }
                }
            }
        }
        console.warn(`✗ 未找到标签为 "${labelText}" 的输入框`);
        return false;
    }

    // 通用定位函数:通过区块标题查找容器
    const findSectionContainer = (titleText) => {
        return Array.from(document.querySelectorAll('.row.ma-0.pa-0, .row.row--dense'))
            .find(row => {
                const titleEl = row.querySelector('p.ma-0.pr-4, .subtitle-1 p');
                return titleEl && titleEl.textContent.trim() === titleText;
            });
    };

    // 通用函数:通过label文本获取输入框值(用于验证)
    function getInputValueByLabel(labelText) {
        const labels = document.querySelectorAll('label');
        for (const label of labels) {
            if (label.textContent.trim() === labelText) {
                // 尝试1:通过for属性
                const inputId = label.getAttribute('for');
                if (inputId) {
                    const input = document.getElementById(inputId);
                    if (input) return input.value;
                }
                
                // 尝试2:在父元素中查找
                const parent = label.closest('.v-input__slot, .v-text-field__slot');
                if (parent) {
                    const input = parent.querySelector('input');
                    if (input) return input.value;
                }
                
                // 尝试3:在相邻元素中查找
                const nextElement = label.nextElementSibling;
                if (nextElement) {
                    const input = nextElement.querySelector('input');
                    if (input) return input.value;
                }
            }
        }
        return null;
    }

    // 通用函数:获取下拉选择框的当前值
    function getSelectValueByLabel(labelText) {
        const label = Array.from(document.querySelectorAll('label.v-label'))
            .find(l => l.textContent.trim() === labelText);
        
        if (!label) return null;
        
        // 通过aria-label查找容器
        const container = label.closest('[aria-label]');
        if (container) {
            const selection = container.querySelector('.v-select__selection');
            return selection?.textContent.trim() || null;
        }
        
        // 尝试通过其他方式
        const button = label.closest('[role="button"]');
        if (button) {
            return button.querySelector('.v-select__selection')?.textContent.trim() || null;
        }
        
        return null;
    }

    // 等待函数
    const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
    
    // ===== 2. 执行文本框输入 =====
    console.group('📝 文本框输入结果');
    setInputByLabel('档案管理机构电话', '********');
    setInputByLabel('家庭医生姓名', '*******');
    setInputByLabel('家庭医生电话', '*******');
    setInputByLabel('籍贯', '河南省************');
    setInputByLabel('出生地', '*************');
    console.groupEnd();

    // ===== 3. 执行下拉选择框输入 =====
    console.group('ToSelector 下拉选择框操作');
    try {
        // 3.1 选择【健康档案管理状态】= "在管"
        let dropdown = Array.from(document.querySelectorAll('label.v-label'))
            .find(l => l.textContent.trim() === '健康档案管理状态')
            ?.closest('[role="button"][aria-haspopup="listbox"]');
        
        if (dropdown) {
            dropdown.click();
            await wait(150); // 增加等待时间确保选项加载
            let option = Array.from(document.querySelectorAll('.v-list-item__title'))
                .find(opt => opt.textContent.trim() === '在管')
                ?.closest('.v-list-item');
            if (option) {
                option.click();
                console.log('✓ 健康档案管理状态: 已选择"在管"');
            } else {
                console.warn('✗ 未找到"在管"选项');
            }
        } else {
            console.warn('✗ 未找到"健康档案管理状态"下拉框');
        }

        // 3.2 选择【紧急联系人与本人关系】= "其他亲属关系"
        dropdown = Array.from(document.querySelectorAll('label.v-label'))
            .find(l => l.textContent.trim() === '紧急联系人与本人关系')
            ?.closest('[role="button"][aria-haspopup="listbox"]');
        
        if (dropdown) {
            dropdown.click();
            await wait(150);
            let option = Array.from(document.querySelectorAll('.v-list-item__title'))
                .find(opt => opt.textContent.trim() === '其他亲属关系')
                ?.closest('.v-list-item');
            if (option) {
                option.click();
                console.log('✓ 紧急联系人与本人关系: 已选择"其他亲属关系"');
            } else {
                console.warn('✗ 未找到"其他亲属关系"选项');
            }
        } else {
            console.warn('✗ 未找到"紧急联系人与本人关系"下拉框');
        }

        // 3.3 选择【是否孕妇】= "否"
        dropdown = Array.from(document.querySelectorAll('label.v-label'))
            .find(l => l.textContent.trim() === '是否孕妇')
            ?.closest('[role="button"][aria-haspopup="listbox"]');
        
        if (dropdown) {
            dropdown.click();
            await wait(150);
            let option = Array.from(document.querySelectorAll('.v-list-item__title'))
                .find(opt => opt.textContent.trim() === '否')
                ?.closest('.v-list-item');
            if (option) {
                option.click();
                console.log('✓ 是否孕妇: 已选择"否"');
            } else {
                console.warn('✗ 未找到"否"选项');
            }
        } else {
            console.warn('✗ 未找到"是否孕妇"下拉框');
        }
    } catch (e) {
        console.error('ToSelector 操作失败:', e);
    }
    console.groupEnd();

    // ===== 4. 执行复选框/单选框操作 =====
    console.group('🔘 复选框/单选框操作');
    try {
        // 4.1 【慢病/重点疾病】选择"无"
        const diseaseSection = findSectionContainer('慢病/重点疾病');
        if (diseaseSection) {
            const noDiseaseLabel = Array.from(diseaseSection.querySelectorAll('label.v-label'))
                .find(label => label.textContent.trim() === '无');
            
            if (noDiseaseLabel) {
                const checkbox = noDiseaseLabel.closest('.v-input--checkbox')?.querySelector('input[type="checkbox"]');
                if (checkbox) {
                    // 取消所有其他选项
                    diseaseSection.querySelectorAll('input[type="checkbox"]').forEach(cb => {
                        if (cb !== checkbox) cb.checked = false;
                    });
                    
                    checkbox.checked = true;
                    ['click', 'change', 'input'].forEach(eventType => {
                        checkbox.dispatchEvent(new Event(eventType, { bubbles: true }));
                    });
                    console.log('✓ 慢病/重点疾病: 已选择"无"');
                }
            }
        }

        // 4.2 【法定传染病】选择"无"
        const infectiousSection = findSectionContainer('法定传染病');
        if (infectiousSection) {
            const noInfectiousLabel = Array.from(infectiousSection.querySelectorAll('label.v-label'))
                .find(label => label.textContent.trim() === '无');
            
            if (noInfectiousLabel) {
                const checkbox = noInfectiousLabel.closest('.v-input--checkbox')?.querySelector('input[type="checkbox"]');
                if (checkbox) {
                    infectiousSection.querySelectorAll('input[type="checkbox"]').forEach(cb => {
                        if (cb !== checkbox) cb.checked = false;
                    });
                    
                    checkbox.checked = true;
                    ['click', 'change', 'input'].forEach(eventType => {
                        checkbox.dispatchEvent(new Event(eventType, { bubbles: true }));
                    });
                    console.log('✓ 法定传染病: 已选择"无"');
                }
            }
        }

        // 4.3 【预防接种史】选择"有"
        const vaccineSection = findSectionContainer('预防接种史');
        if (vaccineSection) {
            const yesLabel = Array.from(vaccineSection.querySelectorAll('label.v-label'))
                .find(label => label.textContent.trim() === '有');
            
            if (yesLabel) {
                let radio = document.getElementById(yesLabel.htmlFor);
                if (!radio) {
                    radio = yesLabel.closest('.v-radio')?.querySelector('input[type="radio"]');
                }
                
                if (radio && !radio.checked) {
                    radio.checked = true;
                    ['click', 'change'].forEach(eventType => {
                        radio.dispatchEvent(new Event(eventType, { bubbles: true }));
                    });
                    console.log('✓ 预防接种史: 已选择"有"');
                }
            }
        }

        // 4.4 【体重状况】选择"正常"
        const weightSection = findSectionContainer('体重状况');
        if (weightSection) {
            const normalLabel = Array.from(weightSection.querySelectorAll('label.v-label'))
                .find(label => label.textContent.trim() === '正常');
            
            if (normalLabel) {
                let radio = document.getElementById(normalLabel.htmlFor);
                if (!radio) {
                    radio = normalLabel.closest('.v-radio')?.querySelector('input[type="radio"]');
                }
                
                if (radio && !radio.checked) {
                    radio.checked = true;
                    ['click', 'change'].forEach(eventType => {
                        radio.dispatchEvent(new Event(eventType, { bubbles: true }));
                    });
                    console.log('✓ 体重状况: 已选择"正常"');
                }
            }
        }
    } catch (e) {
        console.error('🔘 操作失败:', e);
    }
    console.groupEnd();

    // ===== 5. 验证结果(延迟执行)=====
    setTimeout(() => {
        console.group('✅ 最终验证结果');
        
        // 验证文本框
        console.log('📊 文本框验证:');
        [
            '档案管理机构电话',
            '家庭医生姓名',
            '家庭医生电话',
            '籍贯',
            '出生地'
        ].forEach(field => {
            const value = getInputValueByLabel(field);
            console.log(`• ${field}: ${value !== null ? value : '✗ 未找到'}`);
        });

        // 验证下拉选择框
        console.log('\nToSelector 验证:');
        [
            '健康档案管理状态',
            '紧急联系人与本人关系',
            '是否孕妇'
        ].forEach(field => {
            const value = getSelectValueByLabel(field);
            console.log(`• ${field}: ${value !== null ? value : '✗ 未找到'}`);
        });

        // 验证复选框/单选框
        console.log('\n🔘 复选框/单选框验证:');
        
        // 慢病/重点疾病
        const diseaseSection = findSectionContainer('慢病/重点疾病');
        if (diseaseSection) {
            const selected = Array.from(diseaseSection.querySelectorAll('.v-input--checkbox input[type="checkbox"]'))
                .filter(cb => cb.checked)
                .map(cb => cb.closest('.v-input--checkbox')?.querySelector('label')?.textContent.trim())
                .join(', ') || '未选择';
            console.log(`• 慢病/重点疾病: ${selected}`);
        } else {
            console.log('• 慢病/重点疾病: ✗ 未找到区块');
        }
        
        // 法定传染病
        const infectiousSection = findSectionContainer('法定传染病');
        if (infectiousSection) {
            const selected = Array.from(infectiousSection.querySelectorAll('.v-input--checkbox input[type="checkbox"]'))
                .filter(cb => cb.checked)
                .map(cb => cb.closest('.v-input--checkbox')?.querySelector('label')?.textContent.trim())
                .join(', ') || '未选择';
            console.log(`• 法定传染病: ${selected}`);
        } else {
            console.log('• 法定传染病: ✗ 未找到区块');
        }
        
        // 预防接种史
        const vaccineSection = findSectionContainer('预防接种史');
        if (vaccineSection) {
            const selected = vaccineSection.querySelector('.v-radio input[type="radio"]:checked + label')?.textContent.trim() || '未选择';
            console.log(`• 预防接种史: ${selected}`);
        } else {
            console.log('• 预防接种史: ✗ 未找到区块');
        }
        
        // 体重状况
        const weightSection = findSectionContainer('体重状况');
        if (weightSection) {
            const selected = weightSection.querySelector('.v-radio input[type="radio"]:checked + label')?.textContent.trim() || '未选择';
            console.log(`• 体重状况: ${selected}`);
        } else {
            console.log('• 体重状况: ✗ 未找到区块');
        }
        
        console.groupEnd();
    }, 500);

    console.log('✨ 所有操作已执行完成,500ms后将在控制台显示验证结果');
})();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值