类别:程序开发
日期:2020-04-13 浏览:2104 评论:0
1、CacheHelper.cs 缓存文件
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Caching; namespace SysFrameWeb.View.ViewChat { public class CacheHelper { /// <summary> /// 读取缓存数据 /// </summary> /// <param name="cacheKey">键</param> /// <returns></returns> public static object GetCache(string cacheKey) { var objCache = HttpRuntime.Cache.Get(cacheKey); return objCache; } /// <summary> /// 设置缓存数据 /// </summary> /// <param name="cacheKey">键</param> /// <param name="content">值</param> public static void SetCache(string cacheKey, object content) { var objCache = HttpRuntime.Cache; objCache.Insert(cacheKey, content); } /// <summary> /// 设置缓存数据并且设置默认过期时间 /// </summary> /// <param name="cacheKey"></param> /// <param name="content"></param> /// <param name="timeOut"></param> public static void SetCache(string cacheKey, object content, int timeOut = 3600) { try { if (content == null) { return; } var objCache = HttpRuntime.Cache; //设置绝对过期时间 //绝对时间过期。DateTime.Now.AddSeconds(10)表示缓存在3600秒后过期,TimeSpan.Zero表示不使用平滑过期策略。 objCache.Insert(cacheKey, content, null, DateTime.Now.AddSeconds(timeOut), TimeSpan.Zero, CacheItemPriority.High, null); //相对过期 //DateTime.MaxValue表示不使用绝对时间过期策略,TimeSpan.FromSeconds(10)表示缓存连续10秒没有访问就过期。 //objCache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null); } catch (Exception) { throw; } } /// <summary> /// 移除指定缓存 /// </summary> /// <param name="cacheKey">键</param> public static void RemoveAllCache(string cacheKey) { var objCache = HttpRuntime.Cache; objCache.Remove(cacheKey); } /// <summary> /// 删除全部缓存 /// </summary> public static void RemoveAllCache() { var objCache = HttpRuntime.Cache; var cacheEnum = objCache.GetEnumerator(); while (cacheEnum.MoveNext()) { objCache.Remove(cacheEnum.Key.ToString()); } } } }
2、CommonJs 签名工具类
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Web; namespace HealthCardSDKWeb { public static class CommonJs { /// <summary> /// 获取时间戳 /// </summary> /// <returns></returns> public static string GetTimeStamp() { TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds).ToString(); } //获取签名 #region 获取签名 public static string getSign(IDictionary<String, Object> commonIn, IDictionary<String, Object> req, string secret) { SortedDictionary<String, Object> sortParamDic = new SortedDictionary<String, Object>(commonIn); foreach (KeyValuePair<string, Object> kv in req) { sortParamDic.Add(kv.Key, kv.Value); } string paramStr = getParamStr(sortParamDic); Console.WriteLine("param str:" + paramStr); byte[] bytes = Encoding.UTF8.GetBytes(paramStr + secret); byte[] hash = SHA256Managed.Create().ComputeHash(bytes); string sign = Convert.ToBase64String(hash); return sign; } private static string getParamStr(SortedDictionary<String, Object> sortParamDic) { StringBuilder stringBuilder = new StringBuilder(); foreach (KeyValuePair<string, Object> kv in sortParamDic) { if (kv.Value == null) { continue; } string val; if (isBaseType(kv.Value)) { val = kv.Value.ToString(); } else { val = JsonConvert.SerializeObject(kv.Value); Console.WriteLine(val); } if (val.Trim().Equals("")) { continue; } if (stringBuilder.Length > 0) { stringBuilder.Append("&"); } stringBuilder.Append(kv.Key); stringBuilder.Append("="); stringBuilder.Append(val); } return stringBuilder.ToString(); } private static bool isBaseType(Object obj) { Type type = obj.GetType(); return type.IsPrimitive || type.Equals(typeof(string)); } #endregion public static long GetTimeStampUnix() { return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; } } }
3、HealthOpenAuth 电子健康卡的函数类
using com.tencent.healthcard.HealthCardOpenAPI; using com.tencent.healthcard.HealthOpenAuth; using Newtonsoft.Json; using SysFrameWeb.View.ViewChat; using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Runtime.Serialization.Formatters.Binary; using System.Security.Cryptography; using System.Text; using System.Web; namespace HealthCardSDKWeb { public class HealthOpenAuth { private string appToken { set; get; } private com.tencent.healthcard.HealthOpenAuth.ISVOpenInterfaceProxy iSVOpenInterfaceProxy { set; get; } private com.tencent.healthcard.HealthCardOpenAPI.ISVOpenInterfaceProxy proxy { set; get; } #region 签名规则 public HealthOpenAuth() { iSVOpenInterfaceProxy = new com.tencent.healthcard.HealthOpenAuth.ISVOpenInterfaceProxy { domain = "p-healthopen.tengmed.com", appSecret = LxAppConfig.GetAppSecret() }; proxy = new com.tencent.healthcard.HealthCardOpenAPI.ISVOpenInterfaceProxy { domain = "p-healthopen.tengmed.com", appSecret = LxAppConfig.GetAppSecret() }; } /// <summary> /// 初始化,序列化 /// </summary> public void Initialize() { object msg = CacheHelper.GetCache("NlxfybjyApptoken"); if (msg == null || msg == "") { var tokenResult = iSVOpenInterfaceProxy.GetAppToken(new com.tencent.healthcard.HealthOpenAuth.CommonIn { requestId = System.Guid.NewGuid().ToString(), hospitalId = LxAppConfig.GetHospitalId() }, new GetAppTokenReq { appId = LxAppConfig.GetAppid() }); var commonOut = (com.tencent.healthcard.HealthOpenAuth.CommonOut) (tokenResult["commonOut"]); var rsp = (GetAppTokenRsp)tokenResult["rsp"]; appToken = rsp.appToken; CacheHelper.SetCache("NlxfybjyApptoken", rsp.appToken, 7000); } else { appToken = msg.ToString(); } } /// <summary> /// 获取签名规则 /// </summary> /// <returns></returns> public string getSign() { //获取签名规则 string appId = LxAppConfig.GetAppid(); string secret = LxAppConfig.GetAppSecret(); string apk = appToken; //示例:注册健康卡接口的完整请求参数如下 IDictionary<string, Object> commonIn = new Dictionary<string, Object>(); commonIn.Add("hospitalId", LxAppConfig.GetHospitalId()); commonIn.Add("appToken", apk); commonIn.Add("requestId", LxAppConfig.GetRequestId()); commonIn.Add("timestamp", CommonJs.GetTimeStamp()); IDictionary<string, Object> req = new Dictionary<string, Object>(); /* 复杂参数 SortedDictionary<string,Object> cardInfo = new SortedDictionary<string,Object>(); cardInfo.Add("name","张三"); cardInfo.Add("idCard","430203188808084321"); cardInfo.Add("gender","男"); cardInfo.Add("nation","汉族"); cardInfo.Add("birthday", "1999-03-23"); cardInfo.Add("address", "腾讯大厦"); req.Add("cardInfo", cardInfo);*/ req.Add("appId", appId); string sign = CommonJs.getSign(commonIn, req, secret); return sign; //commonIn.Add("sign", sign); //Console.WriteLine("sign:" + sign); //IDictionary<string, Object> paramDic = new Dictionary<string, Object>(); //paramDic.Add("commonIn", commonIn); //paramDic.Add("req", req); //string jsonParam = JsonConvert.SerializeObject(paramDic); //return jsonParam } #endregion /// <summary> /// 获取appToken /// </summary> /// <returns></returns> public string GetAppToken() { var iSVOpenInterfaceProxy = new com.tencent.healthcard.HealthOpenAuth.ISVOpenInterfaceProxy { domain = "p-healthopen.tengmed.com", appSecret = LxAppConfig.GetAppSecret() }; var tokenResult = iSVOpenInterfaceProxy.GetAppToken(new com.tencent.healthcard.HealthOpenAuth.CommonIn { requestId = System.Guid.NewGuid().ToString(), hospitalId = LxAppConfig.GetHospitalId() }, new GetAppTokenReq { appId = LxAppConfig.GetAppid() }); var commonOut = (com.tencent.healthcard.HealthOpenAuth.CommonOut) (tokenResult["commonOut"]); var rsp = (GetAppTokenRsp)tokenResult["rsp"]; appToken = rsp.appToken; return appToken; } #region GET请求 public static T HttpGet<T>(string url) { try { string retString = ""; //创建Request对象 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; request.Timeout = 30000; request.ReadWriteTimeout = 30000; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { //输出响应流 Stream stream = response.GetResponseStream(); using (StreamReader streamReader = new StreamReader(stream, Encoding.UTF8)) { retString = streamReader.ReadToEnd().ToString(); } } //JSON序列化 return JsonConvert.DeserializeObject<T>(retString); } catch (Exception e) { string mse = e.Message; return default(T); } } #endregion #region POST请求 /// <summary> /// POST请求 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="url">请求Url地址</param> /// <param name="postParameters">post提交参数</param> /// <returns></returns> public static T HttpPostStr<T>(string url, string js) { try { string retString = ""; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; //注意:输入特定格式的时候头文件上下文需说明,如JSON字符串声明 //为:"application/json;" request.ContentType = "application/x-www-form-urlencoded;charset:utf-8"; //POST参数 //编码要跟服务器编码统一 byte[] bt = Encoding.UTF8.GetBytes(js); string responseData = String.Empty; request.ContentLength = bt.Length; using (Stream reqStream = request.GetRequestStream()) { reqStream.Write(bt, 0, bt.Length); reqStream.Close(); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Stream stream = response.GetResponseStream(); using (StreamReader streamReader = new StreamReader(stream, Encoding.UTF8)) { retString = streamReader.ReadToEnd().ToString(); } } return JsonConvert.DeserializeObject<T>(retString); } catch (Exception e) { return default(T); } } #endregion /// <summary> /// 注册单个注册健康卡 /// </summary> /// <param name="lxbirthday"></param> /// <param name="lxidtype"></param> /// <param name="lxgender"></param> /// <param name="lxidCard"></param> /// <param name="lxname"></param> /// <param name="lxnation"></param> /// <param name="lxaddress"></param> /// <param name="lxphone1"></param> /// <param name="lxwechatcode"></param> /// <returns></returns> public RegisterHealthCardRsp Lx_registerHealthCard(string lxbirthday, string lxidtype, string lxgender, string lxidCard, string lxname, string lxnation, string lxaddress, string lxphone1, string lxwechatcode) { var result = proxy.RegisterHealthCard( new RegisterHealthCardReq { birthday = lxbirthday, idType = lxidtype, gender = lxgender, idNumber = lxidCard, name = lxname, nation = lxnation, phone1 = lxphone1, address=lxaddress, wechatCode = lxwechatcode }, new com.tencent.healthcard.HealthCardOpenAPI.CommonIn { requestId = System.Guid.NewGuid().ToString(), hospitalId = LxAppConfig.GetHospitalId(), //channelNum = 0, appToken = appToken } ); var commonOut =(com.tencent.healthcard.HealthCardOpenAPI.CommonOut)result["commonOut"]; var rsp = (RegisterHealthCardRsp)result["rsp"]; return rsp; //return JsonConvert.SerializeObject(rsp); } /// <summary> /// 注册批量健康卡 /// </summary> /// <returns></returns> public string Lx_RegisterBatchHealthCard() { #region 测试和程序代码 //BatchHealthCardItem[] items = new BatchHealthCardItem[2]; // for (int i = 0; i < 2; i++) // { // BatchHealthCardItem item = new BatchHealthCardItem // { // //birthday = "1996-01-23", // //idType = "01", // //gender = "男", // //idCard = "432901196310056419", // //name = "蒋建正", // //nation = "汉族", // //phone1 = "18656130435", // //wechatCode = "13B8AC208CEDACE7DD1DF57963150664" // birthday = lxbirthday, // idType = lxidtype, // gender = lxgender, // idCard = lxidCard, // name = lxname, // nation = lxnation, // phone1 = lxphone1, // wechatCode = lxwechatcode // }; // items[i] = item; // } #endregion BatchHealthCardItem[] items = new BatchHealthCardItem[2]; for (int i = 0; i < 2; i++) { BatchHealthCardItem item = new BatchHealthCardItem { birthday = "1996-01-23", idType = "01", gender = "男", idCard = "432901196310056419", name = "蒋建正", nation = "汉族", phone1 = "18656130435", wechatCode = "13B8AC208CEDACE7DD1DF57963150664" }; items[i] = item; } var result = proxy.RegisterBatchHealthCard( new RegisterBatchHealthCardReq { healthCardItems = items }, new com.tencent.healthcard.HealthCardOpenAPI.CommonIn { requestId = System.Guid.NewGuid().ToString(), hospitalId = "20006", appToken = appToken } ); var commonOut = (com.tencent.healthcard.HealthCardOpenAPI.CommonOut)result["commonOut"]; var rsp = (RegisterBatchHealthCardRsp)result["rsp"]; return JsonConvert.SerializeObject(rsp); } /// <summary> /// ocrInfo 获取身份证信息 /// </summary> /// <returns></returns> public string Lx_ocrInfo(string base64) { var result = proxy.OcrInfo( new OcrInfoReq { imageContent = base64//"base64" }, new com.tencent.healthcard.HealthCardOpenAPI.CommonIn { requestId = System.Guid.NewGuid().ToString(), hospitalId = LxAppConfig.GetHospitalId(), channelNum = 0, appToken = GetAppToken() } ); var commonOut = (com.tencent.healthcard.HealthCardOpenAPI.CommonOut)result["commonOut"]; var rsp = (OcrInfoRsp)result["rsp"]; return JsonConvert.SerializeObject(rsp); } /// <summary> /// 根据二维码(静态、动态) 获取用户数据 /// </summary> /// <returns></returns> public string Lx_GetHospitalInfoByEcardNo() { var result = proxy.GetHealthCardByQRCode( new GetHealthCardByQRCodeReq { // qrCodeText = "56656569FCAC6D78FC04BC0A4F75D96ABD8E16E77666639ABE66564EC0E6B623:1" qrCodeText = "56656569FCAC6D78FC04BC0A4F75D96ABD8E16E77666639ABE66564EC0E6B623:0:A2939649EB16FAD3CAD38EB126E4DE48" }, new com.tencent.healthcard.HealthCardOpenAPI.CommonIn { requestId = System.Guid.NewGuid().ToString(), hospitalId = "30649", appToken = appToken } ); var commonOut = (com.tencent.healthcard.HealthCardOpenAPI.CommonOut)result["commonOut"]; var rsp = (GetHealthCardByQRCodeRsp)result["rsp"]; return JsonConvert.SerializeObject(rsp); } /// <summary> /// 获取动态健康卡二维码接口 /// </summary> /// <returns></returns> public string Lx_GetDynamicQRCode() { var result = proxy.GetDynamicQRCode( new GetDynamicQRCodeReq { healthCardId = "56656569FCAC6D78FC04BC0A4F75D96ABD8E16E77666639ABE66564EC0E6B623", idType = "01", idNumber = "410221198908203453", codeType = 0 }, new com.tencent.healthcard.HealthCardOpenAPI.CommonIn { requestId = System.Guid.NewGuid().ToString(), hospitalId = LxAppConfig.GetHospitalId(), appToken = appToken } ); var commonOut = (com.tencent.healthcard.HealthCardOpenAPI.CommonOut)result["commonOut"]; var rsp = (GetDynamicQRCodeRsp)result["rsp"]; return JsonConvert.SerializeObject(rsp); } /// <summary> /// 更具动态二维码接口获取用户数据 /// </summary> /// <returns></returns> public string Lx_bindCardRelation() { var result = proxy.BindCardRelation( new BindCardRelationReq { patid="123456", qrCodeText = "56656569FCAC6D78FC04BC0A4F75D96ABD8E16E77666639ABE66564EC0E6B623:0:A2939649EB16FAD3CAD38EB126E4DE48" }, new com.tencent.healthcard.HealthCardOpenAPI.CommonIn { requestId = System.Guid.NewGuid().ToString(), hospitalId = "30649", appToken = appToken } ); var commonOut = (com.tencent.healthcard.HealthCardOpenAPI.CommonOut)result["commonOut"]; var rsp = (BindCardRelationRsp)result["rsp"]; return JsonConvert.SerializeObject(rsp); } /// <summary> /// 通过授权获取健康卡接口 /// </summary> /// <param name="lxHealthCode"></param> /// <returns></returns> public GetHealthCardByHealthCodeRsp Lx_GetHealthCardByHealthCode(string lxHealthCode) { //https://health.tengmed.com/open/getHealthCardList?redirect_uri=${redirect_uri} 获取 var result = proxy.GetHealthCardByHealthCode( new GetHealthCardByHealthCodeReq { healthCode = lxHealthCode }, new com.tencent.healthcard.HealthCardOpenAPI.CommonIn { requestId = System.Guid.NewGuid().ToString(), hospitalId = LxAppConfig.GetHospitalId(), appToken = appToken } ); var commonOut = (com.tencent.healthcard.HealthCardOpenAPI.CommonOut)result["commonOut"]; var rsp = (GetHealthCardByHealthCodeRsp)result["rsp"]; return rsp;// JsonConvert.SerializeObject(rsp); } /// <summary> /// 用卡数据监测接口 /// </summary> /// <returns></returns> public string Lx_reportHISData(string qrCode, string idNumber, string name, string time, string scene, string cardType, string cardChannel, string department, string cardCostTypes) { var result = proxy.ReportHISData( new ReportHISDataReq { qrCodeText = qrCode, idCardNumber = idNumber, name = name, time = time, scene = scene, cardType = cardType, cardChannel = cardChannel, department = department, cardCostTypes = cardCostTypes, hospitalCode = LxAppConfig.GetHospitalId() }, new com.tencent.healthcard.HealthCardOpenAPI.CommonIn { requestId = System.Guid.NewGuid().ToString(), hospitalId = LxAppConfig.GetHospitalId(), appToken = appToken } ); var commonOut = (com.tencent.healthcard.HealthCardOpenAPI.CommonOut)result["commonOut"]; var rsp = (ReportHISDataRsp)result["rsp"]; return JsonConvert.SerializeObject(rsp); } /// <summary> /// 获取卡包订单ID接口 /// </summary> /// <param name="appid"></param> /// <param name="qrCodeText"></param> /// <returns></returns> public string Lx_GetOrderIdByOutAppId(string qrCodeText) { var result = proxy.GetOrderIdByOutAppId( new GetOrderIdByOutAppIdReq { appId = LxAppConfig.GetAppid(), qrCodeText = qrCodeText }, new com.tencent.healthcard.HealthCardOpenAPI.CommonIn { requestId = System.Guid.NewGuid().ToString(), hospitalId = LxAppConfig.GetHospitalId(), appToken = appToken } ); var commonOut = (com.tencent.healthcard.HealthCardOpenAPI.CommonOut)result["commonOut"]; var rsp = (GetOrderIdByOutAppIdRsp)result["rsp"]; return JsonConvert.SerializeObject(rsp); } /// <summary> /// 校验动态二维码接口 /// </summary> /// <returns></returns> public string Lx_verifyQRCode() { var result = proxy.VerifyQRCode( new VerifyQRCodeReq { qrCodeText = "", terminalId = "", time = "", medicalStep = "", channelCode = "", useCityCode = "", useCityName = "", hospitalCode = "", hospitalName = "", orgId = "", name = "", idCard = "", useType = "01" }, new com.tencent.healthcard.HealthCardOpenAPI.CommonIn { requestId = System.Guid.NewGuid().ToString(), hospitalId = LxAppConfig.GetHospitalId(), appToken = appToken } ); var commonOut = (com.tencent.healthcard.HealthCardOpenAPI.CommonOut)result["commonOut"]; var rsp = (VerifyQRCodeReq)result["rsp"]; return JsonConvert.SerializeObject(rsp); } /// <summary> /// 注册人脸订单ID接口 /// </summary> /// <returns></returns> public string Lx_registerOrder() { var result = proxy.RegisterOrder( new RegisterOrderReq { name = "", idCardNumber = "" }, new com.tencent.healthcard.HealthCardOpenAPI.CommonIn { requestId = System.Guid.NewGuid().ToString(), hospitalId = LxAppConfig.GetHospitalId(), appToken = appToken } ); var commonOut = (com.tencent.healthcard.HealthCardOpenAPI.CommonOut)result["commonOut"]; var rsp = (RegisterOrderReq)result["rsp"]; return JsonConvert.SerializeObject(rsp); } /// <summary> /// 校验人脸识别数据接口 /// </summary> /// <returns></returns> public string Lx_verifyFaceIdentity() { var result = proxy.VerifyFaceIdentity( new VerifyFaceIdentityReq { orderId = "", verifyResult = "" }, new com.tencent.healthcard.HealthCardOpenAPI.CommonIn { requestId = System.Guid.NewGuid().ToString(), hospitalId = LxAppConfig.GetHospitalId(), appToken = appToken } ); var commonOut = (com.tencent.healthcard.HealthCardOpenAPI.CommonOut)result["commonOut"]; var rsp = (VerifyFaceIdentityReq)result["rsp"]; return JsonConvert.SerializeObject(rsp); } } }
4、LxAppConfig 配置文件
using com.tencent.healthcard.HealthCardOpenAPI; using com.tencent.healthcard.HealthOpenAuth; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Runtime.Serialization.Formatters.Binary; using System.Security.Cryptography; using System.Text; using System.Web; namespace HealthCardSDKWeb { public static class LxAppConfig { public static string GetAppid() { return "0a993d9f7af0e37b0446d205860e3850"; } public static string GetAppSecret() { return "431afead2cc8165349db873f92ace0d0"; } public static string GetHospitalId() { return "30649"; //宁陵妇幼保健院 //return "30683"; //宁陵中医院 } public static string GetTimeStamp() { return CommonJs.GetTimeStamp(); } public static string GetRequestId() { return System.Guid.NewGuid().ToString(); } } }
别忘记最重要的引用微信提供的类库,官网有自行下载。https://open.tengmed.com/
有问题可以留言,我有空会回复的。
发表评论 / 取消回复