123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Web.Http;
- namespace ThCardReader
- {
- /// <summary>
- /// 华视电子读卡器专用API控制器
- /// 独立于现有的EntryController,不影响原有医保业务
- /// </summary>
- public class HuaShiController : ApiController
- {
- /// <summary>
- /// 初始化华视读卡器
- /// </summary>
- /// <param name="port">端口号:1-16为串口,1001-1016为USB口</param>
- /// <returns>初始化结果</returns>
- [HttpPost]
- [Route("readcard/huashi/init")]
- public IHttpActionResult InitHuaShiReader([FromBody] dynamic request)
- {
- try
- {
- int port = 1001; // 默认USB端口1
-
- if (request != null && request.port != null)
- {
- port = (int)request.port;
- }
- var result = HuaShiIdCardBusiness.Initialize(port);
-
- if ((int)result["code"] == 200)
- {
- return Ok(result);
- }
- else
- {
- return BadRequest(result.ToString());
- }
- }
- catch (Exception ex)
- {
- var errorResult = new JObject
- {
- ["code"] = 1001,
- ["message"] = $"华视读卡器初始化异常: {ex.Message}",
- ["device"] = "华视电子身份证读卡器"
- };
- return BadRequest(errorResult.ToString());
- }
- }
- /// <summary>
- /// 读取身份证信息
- /// </summary>
- /// <returns>身份证信息</returns>
- [HttpPost]
- [Route("readcard/huashi/readcard")]
- public IHttpActionResult ReadIdCard([FromBody] dynamic request)
- {
- try
- {
- string savePath = "";
- bool autoClose = false;
-
- if (request != null)
- {
- if (request.savePath != null)
- {
- savePath = (string)request.savePath;
- }
- if (request.autoClose != null)
- {
- autoClose = (bool)request.autoClose;
- }
- }
- var result = HuaShiIdCardBusiness.ReadIdCard(savePath, autoClose);
-
- if ((int)result["code"] == 200)
- {
- return Ok(result);
- }
- else
- {
- return BadRequest(result.ToString());
- }
- }
- catch (Exception ex)
- {
- var errorResult = new JObject
- {
- ["code"] = 1001,
- ["message"] = $"华视读卡器读卡异常: {ex.Message}",
- ["device"] = "华视电子身份证读卡器"
- };
- return BadRequest(errorResult.ToString());
- }
- }
- /// <summary>
- /// 连续读卡模式
- /// </summary>
- /// <returns>身份证信息</returns>
- [HttpPost]
- [Route("readcard/huashi/readcard/continuous")]
- public IHttpActionResult ReadIdCardContinuous([FromBody] dynamic request)
- {
- try
- {
- string savePath = "";
- bool autoClose = false;
-
- if (request != null)
- {
- if (request.savePath != null)
- {
- savePath = (string)request.savePath;
- }
- if (request.autoClose != null)
- {
- autoClose = (bool)request.autoClose;
- }
- }
- var result = HuaShiIdCardBusiness.ReadIdCardContinuous(savePath, autoClose);
-
- if ((int)result["code"] == 200)
- {
- return Ok(result);
- }
- else
- {
- return BadRequest(result.ToString());
- }
- }
- catch (Exception ex)
- {
- var errorResult = new JObject
- {
- ["code"] = 1001,
- ["message"] = $"华视连续读卡异常: {ex.Message}",
- ["device"] = "华视电子身份证读卡器"
- };
- return BadRequest(errorResult.ToString());
- }
- }
- /// <summary>
- /// 获取设备状态
- /// </summary>
- /// <returns>设备状态信息</returns>
- [HttpGet]
- [Route("readcard/huashi/status")]
- public IHttpActionResult GetDeviceStatus()
- {
- try
- {
- var result = HuaShiIdCardBusiness.GetDeviceStatus();
-
- if ((int)result["code"] == 200)
- {
- return Ok(result);
- }
- else
- {
- return BadRequest(result.ToString());
- }
- }
- catch (Exception ex)
- {
- var errorResult = new JObject
- {
- ["code"] = 1001,
- ["message"] = $"获取华视设备状态异常: {ex.Message}",
- ["device"] = "华视电子身份证读卡器"
- };
- return BadRequest(errorResult.ToString());
- }
- }
- /// <summary>
- /// 关闭华视读卡器连接
- /// </summary>
- /// <returns>关闭结果</returns>
- [HttpPost]
- [Route("readcard/huashi/close")]
- public IHttpActionResult CloseReader()
- {
- try
- {
- var result = HuaShiIdCardBusiness.Close();
- return Ok(result);
- }
- catch (Exception ex)
- {
- var errorResult = new JObject
- {
- ["code"] = 1001,
- ["message"] = $"关闭华视读卡器异常: {ex.Message}",
- ["device"] = "华视电子身份证读卡器"
- };
- return BadRequest(errorResult.ToString());
- }
- }
- /// <summary>
- /// 获取华视API文档
- /// </summary>
- /// <returns>API文档信息</returns>
- [HttpGet]
- [Route("readcard/huashi/help")]
- public IHttpActionResult GetApiHelp()
- {
- var help = new JObject
- {
- ["device"] = "华视电子身份证读卡器",
- ["version"] = "V3.3.0.7",
- ["manufacturer"] = "深圳华视电子读写设备有限公司",
- ["apis"] = new JArray
- {
- new JObject
- {
- ["method"] = "POST",
- ["url"] = "/readcard/huashi/init",
- ["description"] = "初始化华视读卡器",
- ["parameters"] = new JObject
- {
- ["port"] = "端口号:1-16为串口COM1-COM16,1001-1016为USB口1-16"
- },
- ["example"] = new JObject
- {
- ["port"] = 1001
- }
- },
- new JObject
- {
- ["method"] = "POST",
- ["url"] = "/readcard/huashi/readcard",
- ["description"] = "读取身份证信息",
- ["parameters"] = new JObject
- {
- ["savePath"] = "照片保存路径(可选)",
- ["autoClose"] = "读卡后是否自动关闭连接(可选,默认false,URL调用建议true)"
- },
- ["example"] = new JObject
- {
- ["savePath"] = "C:/photos",
- ["autoClose"] = true
- }
- },
- new JObject
- {
- ["method"] = "POST",
- ["url"] = "/readcard/huashi/readcard/continuous",
- ["description"] = "连续读卡模式(不需要重新放置卡片)",
- ["parameters"] = new JObject
- {
- ["savePath"] = "照片保存路径(可选)",
- ["autoClose"] = "读卡后是否自动关闭连接(可选,默认false,URL调用建议true)"
- }
- },
- new JObject
- {
- ["method"] = "GET",
- ["url"] = "/readcard/huashi/status",
- ["description"] = "获取设备状态"
- },
- new JObject
- {
- ["method"] = "POST",
- ["url"] = "/readcard/huashi/close",
- ["description"] = "关闭读卡器连接"
- }
- },
- ["supportedCards"] = new JArray
- {
- "居民身份证",
- "外国人永久居留证",
- "港澳台居民居住证",
- "新版外国人居留证"
- },
- ["returnFields"] = new JObject
- {
- ["name"] = "姓名",
- ["sex"] = "性别",
- ["nation"] = "民族",
- ["birthday"] = "出生日期",
- ["idCode"] = "身份证号",
- ["address"] = "地址",
- ["department"] = "签发机关",
- ["startDate"] = "有效开始日期",
- ["endDate"] = "有效截止日期",
- ["sexCode"] = "性别代码",
- ["nationCode"] = "民族代码",
- ["certType"] = "证件类别"
- },
- ["notes"] = new JArray
- {
- "华视读卡器独立于医保业务,不影响SSCard.dll和NationECCode.dll的使用",
- "支持自动初始化:首次调用readcard/continuous/status时会自动初始化设备",
- "支持异常自愈:设备异常时会自动重置,下次调用时重新初始化",
- "手动初始化:可选择手动调用init接口指定特定端口",
- "需要部署termb.dll、sdtapi.dll、WltRs.dll、SysInfo.dll四个DLL文件",
- "卡认证循环间隔建议大于300ms",
- "查询卡片放置状态间隔建议大于600ms",
- "连续读卡模式可避免频繁拿起放下卡片",
- "现在使用起来和SSCard.dll一样简单:直接调用readcard即可"
- }
- };
- return Ok(help);
- }
- /// <summary>
- /// 健康检查
- /// </summary>
- /// <returns>服务健康状态</returns>
- [HttpGet]
- [Route("readcard/huashi/health")]
- public IHttpActionResult HealthCheck()
- {
- var health = new JObject
- {
- ["service"] = "华视电子读卡器API",
- ["status"] = "运行中",
- ["timestamp"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
- ["version"] = "1.0.0"
- };
- return Ok(health);
- }
- /// <summary>
- /// GET方式的简单调用接口(兼容现有URL调用模式)
- /// </summary>
- /// <param name="action">操作类型:init、readcard、continuous、status、close</param>
- /// <param name="port">端口号(仅用于init操作)</param>
- /// <returns>操作结果</returns>
- [HttpGet]
- [Route("readcard/huashi/simple")]
- public IHttpActionResult SimpleCall(string action = "readcard", int port = 1001)
- {
- try
- {
- JObject result;
-
- switch (action?.ToLower())
- {
- case "init":
- result = HuaShiIdCardBusiness.Initialize(port);
- break;
- case "readcard":
- result = HuaShiIdCardBusiness.ReadIdCard("", true);
- break;
- case "continuous":
- // SimpleCall也是URL调用,应该启用自动关闭
- result = HuaShiIdCardBusiness.ReadIdCardContinuous("", true);
- break;
- case "status":
- result = HuaShiIdCardBusiness.GetDeviceStatus();
- break;
- case "close":
- result = HuaShiIdCardBusiness.Close();
- break;
- default:
- result = new JObject
- {
- ["code"] = 1001,
- ["message"] = $"不支持的操作:{action},支持的操作:init、readcard、continuous、status、close",
- ["device"] = "华视电子身份证读卡器"
- };
- break;
- }
- if ((int)result["code"] == 200)
- {
- return Ok(result);
- }
- else
- {
- return BadRequest(result.ToString());
- }
- }
- catch (Exception ex)
- {
- var errorResult = new JObject
- {
- ["code"] = 1001,
- ["message"] = $"华视读卡器操作异常: {ex.Message}",
- ["device"] = "华视电子身份证读卡器"
- };
- return BadRequest(errorResult.ToString());
- }
- }
- }
- }
|