在EntryController.cs中,当URL格式为 huashi_01101
或 jiangsu_01101
时,代码会将业务代码(如01101
)误认为是操作动作(action),导致以下错误:
华视读卡器操作【01101】错误!允许的操作为【init、readcard、continuous、status、close】
江苏医保操作【01101】错误!允许的操作为【init、readcard、readcard_pin、verifypin、changepin、status、reset、getpersoninfo、checkdll】
// 问题代码逻辑
string[] temps = param.Split(new char[] { '_' });
string type = temps[0];
string action = temps.Length > 1 ? temps[1] : "readcard";
// 当param="huashi_01101"时:
// type = "huashi"
// action = "01101" ← 这里把业务代码当作了操作动作
URL格式 | 期望行为 | 实际行为 | 问题 |
---|---|---|---|
huashi_readcard |
✅ 读取身份证 | ✅ 正常工作 | 无 |
huashi_01101 |
✅ 读取身份证(忽略业务代码) | ❌ 报错"操作01101错误" | 需要修复 |
jiangsu_readcard |
✅ 读取社保卡 | ✅ 正常工作 | 无 |
jiangsu_01101 |
✅ 读取社保卡(忽略业务代码) | ❌ 报错"操作01101错误" | 需要修复 |
qrcode_01101 |
✅ 电子凭证+业务代码 | ✅ 正常工作 | 无 |
sicard |
✅ 读取社保卡 | ✅ 正常工作 | 无 |
/// <summary>
/// 判断字符串是否是业务代码格式(5位数字)
/// </summary>
/// <param name="code">要检查的字符串</param>
/// <returns>是否是业务代码</returns>
private bool IsBusinessCode(string code)
{
if (string.IsNullOrEmpty(code) || code.Length != 5)
{
return false;
}
// 检查是否是5位数字
foreach (char c in code)
{
if (!char.IsDigit(c))
{
return false;
}
}
return true;
}
else if (type.Equals("huashi"))
{
// 华视读卡器调用
string action = temps.Length > 1 ? temps[1] : "readcard";
// 检查是否是业务代码格式(5位数字),如果是则默认为readcard操作
if (temps.Length > 1 && IsBusinessCode(temps[1]))
{
action = "readcard"; // 业务代码不影响华视读卡器操作,默认读卡
}
// ... 其余逻辑保持不变
}
else if (type.Equals("jiangsu"))
{
// 江苏医保社保卡读取
string action = temps.Length > 1 ? temps[1] : "readcard";
// 检查是否是业务代码格式(5位数字),如果是则默认为readcard操作
if (temps.Length > 1 && IsBusinessCode(temps[1]))
{
action = "readcard"; // 业务代码不影响江苏医保读卡操作,默认读卡
}
// ... 其余逻辑保持不变
}
URL | 修复前 | 修复后 |
---|---|---|
huashi_01101 |
❌ 报错"操作01101错误" | ✅ 等效于 huashi_readcard |
huashi_01201 |
❌ 报错"操作01201错误" | ✅ 等效于 huashi_readcard |
jiangsu_01101 |
❌ 报错"操作01101错误" | ✅ 等效于 jiangsu_readcard |
jiangsu_01201 |
❌ 报错"操作01201错误" | ✅ 等效于 jiangsu_readcard |
输入 | 是否业务代码 | 处理方式 |
---|---|---|
01101 |
✅ 是(5位数字) | 转为默认操作 readcard |
01201 |
✅ 是(5位数字) | 转为默认操作 readcard |
readcard |
❌ 否(非5位数字) | 作为操作动作处理 |
init |
❌ 否(非5位数字) | 作为操作动作处理 |
12345a |
❌ 否(包含字母) | 作为操作动作处理(会报错) |
123456 |
❌ 否(6位数字) | 作为操作动作处理(会报错) |
huashi_readcard
→ 继续正常工作huashi_init_1001
→ 继续正常工作jiangsu_readcard
→ 继续正常工作jiangsu_status
→ 继续正常工作sicard
→ 继续正常工作idcard
→ 继续正常工作qrcode_01101
→ 继续正常工作(电子凭证正确传递业务代码)idcard2_01101
→ 继续正常工作huashi_invalid
→ 继续报错(友好的错误信息)jiangsu_invalid
→ 继续报错(友好的错误信息)创建了 test_businesscode_fix.html
测试页面,包含:
// 1. 华视读卡器
testUrl('huashi_readcard'); // ✅ 应该成功
testUrl('huashi_01101'); // ✅ 应该成功(修复的)
testUrl('huashi_invalid'); // ❌ 应该报错
// 2. 江苏医保
testUrl('jiangsu_readcard'); // ✅ 应该成功
testUrl('jiangsu_01101'); // ✅ 应该成功(修复的)
testUrl('jiangsu_invalid'); // ❌ 应该报错
// 3. 其他功能
testUrl('sicard'); // ✅ 应该成功
testUrl('qrcode_01101'); // ✅ 应该成功
ThCardReader/EntryController.cs
IsBusinessCode(string code)
- 检测5位数字业务代码{type}_{bizcode}
URL格式{type}_{bizcode}
格式// 现在这些调用都能正常工作:
// 华视读卡器
fetch('http://localhost:8321/readcard/entry?param=huashi_readcard')
fetch('http://localhost:8321/readcard/entry?param=huashi_01101') // ← 修复的
fetch('http://localhost:8321/readcard/entry?param=huashi_01201') // ← 修复的
// 江苏医保
fetch('http://localhost:8321/readcard/entry?param=jiangsu_readcard')
fetch('http://localhost:8321/readcard/entry?param=jiangsu_01101') // ← 修复的
fetch('http://localhost:8321/readcard/entry?param=jiangsu_01201') // ← 修复的
// 电子凭证(业务代码正确传递,保持不变)
fetch('http://localhost:8321/readcard/entry?param=qrcode_01101')
// 这些调用现在都能正常工作
var result1 = await CallApi("huashi_readcard");
var result2 = await CallApi("huashi_01101"); // ← 修复的,等效于上面
var result3 = await CallApi("jiangsu_01101"); // ← 修复的
总结:本次修复通过智能识别5位数字业务代码,使华视读卡器和江苏医保功能能够正确处理标准的URL格式,提高了系统的兼容性和用户体验,同时保持了完全的向后兼容性。