# 江苏医保社保卡读取最佳实践建议 ## 🎯 **智能重置策略(推荐方案)** ### 📋 **策略概述** 基于用户使用场景(网页点击按钮读卡),采用智能重置策略: ``` 1. 系统启动时 → 初始化一次 2. 患者A → 点击读卡 → 成功 3. 患者B → 点击读卡 → 成功 4. 患者C → 点击读卡 → 成功 5. 如果连续失败 → 自动重置 → 重新初始化 6. 继续正常读卡... ``` ### 🔄 **核心实现** #### JavaScript智能读卡函数 ```javascript let consecutiveFailures = 0; const MAX_FAILURES = 3; // 连续失败3次后自动重置 async function smartReadCard() { try { // 调用读卡API const response = await fetch('/api/jiangsu/readcard', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({}) }); const result = await response.json(); if (result.success) { consecutiveFailures = 0; // 成功后重置失败计数 console.log('读卡成功:', result.data); return result; } else { throw new Error(result.message); } } catch (error) { consecutiveFailures++; console.log(`读卡失败 ${consecutiveFailures}/${MAX_FAILURES}:`, error.message); if (consecutiveFailures >= MAX_FAILURES) { console.log('连续失败达到阈值,自动重置系统...'); try { // 自动重置系统 await fetch('/api/jiangsu/reset', { method: 'POST' }); // 重新初始化 const initResponse = await fetch('/api/jiangsu/init', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ ip: "192.168.1.100", port: "8080", timeout: "30000", logPath: "C:\\logs\\jiangsu_medical.log", ecUrl: "https://fuwu.nhsa.gov.cn", orgCode: "H32010000001", deviceNo: "设备编号001", operId: "操作员001", operName: "系统操作员", officeId: "科室001", officeName: "门诊部", machineCode: "终端001" }) }); if (initResponse.ok) { consecutiveFailures = 0; // 重置成功后清零 console.log('系统重置并重新初始化完成'); // 重置后再次尝试读卡 return await readCardDirect(); // 直接调用读卡,避免递归 } else { throw new Error('系统重置失败'); } } catch (resetError) { console.error('自动重置过程失败:', resetError); throw new Error('系统重置失败,请手动重启应用'); } } throw error; // 未达到重置阈值,直接抛出错误 } } // 直接读卡函数(避免重置逻辑递归) async function readCardDirect() { const response = await fetch('/api/jiangsu/readcard', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({}) }); const result = await response.json(); if (!result.success) { throw new Error(result.message); } return result; } ``` ### 🌐 **完整的HTML页面实现** ```html