using Newtonsoft.Json.Linq; using PTMedicalInsurance.Common; using PTMedicalInsurance.Variables; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.ExceptionServices; using System.Runtime.InteropServices; using System.Security; using System.Text; using System.Threading.Tasks; using System.Net; namespace PTMedicalInsurance.Helper { class InvokeDllCenter : IInvokeCenter { public int Business(string inputData, ref string outputData) { outputData = ""; JObject joRtn = new JObject(); try { //创建一个HTTP请求 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Global.curEvt.URL); //Post请求方式 request.Method = "POST"; //string nonce = Guid.NewGuid().ToString(); //非重复的随机字符串(十分钟内不能重复) string timestamp = TimeStamp.get10().ToString(); //当前时间戳(秒) //string timestamp = Math.round(new Date().getTime() / 1000).toString(); string name = Global.inf.apiName; string version = "1.0.0"; string access_key = Global.inf.AK; string secretKey = Global.inf.SK; //签名 string sTemp = "_api_access_key=" + access_key + "&_api_name=" + name + "&_api_timestamp=" + timestamp + "&_api_version=" + version; //Sha256 加密生成的签名 x-tif-signature = sha256(x-tif-timestamp + secretKey+ x-tif-nonce + xtif-timestamp) string signature = Encrypt.SHA256EncryptStr(sTemp); //nonce = GetRandomString(32); // 随机数 //timestamp = GetCurrentUnixSeconds().ToString(); //signature = GetSHA256Str($"{timestamp}{secretKey}{nonce}{timestamp}"); // 签名 //Global.writeLog($"nonce:{nonce}\ntimestamp:{timestamp}\nsecretKey:{secretKey}\npassid:{passid}\nsignature:{signature}"); //内容类型 request.ContentType = "application/json"; //增加头部信息 request.Headers.Add("_api_timestamp", timestamp); request.Headers.Add("_api_name", Global.inf.apiName); request.Headers.Add("_api_version", "1.0.0"); request.Headers.Add("_api_access_key", Global.inf.AK); request.Headers.Add("_api_signature", signature); //设置参数,并进行URL编码 string paraUrlCoded = inputData;//System.Web.HttpUtility.UrlEncode(jsonParas); byte[] payload; //将Json字符串转化为字节 payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); //设置请求的ContentLength request.ContentLength = payload.Length; //发送请求,获得请求流 Stream writer; writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象 //将请求参数写入流 writer.Write(payload, 0, payload.Length); writer.Close();//关闭请求流 HttpWebResponse response = null; try { //获得响应流 response = (HttpWebResponse)request.GetResponse(); } catch (WebException ex) { HttpWebResponse res = (HttpWebResponse)ex.Response; Stream myResponseStream = res.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); string retString = myStreamReader.ReadToEnd(); outputData = JsonHelper.setExceptionJson(-99, "异常返回", retString).ToString(); return -1; } outputData = getResponseData(response); joRtn = JObject.Parse(outputData);//返回Json数据 return 0; } catch (Exception ex) { joRtn.Add("infcode", -1); joRtn.Add("err_msg", "调用中心服务异常invokeCenterService(1):" + ex.Message); outputData = JsonHelper.toJsonString(joRtn); return -1; } } public int DownloadFile(string inputData, ref string outputData) { outputData = ""; string error = string.Empty; int errorCode = 0; try { JObject jsonInParam = JObject.Parse(inputData); string fileName = (string)jsonInParam["input"]["fsDownloadIn"]["filename"]; string filePath = Global.curEvt.path + "\\YBDLOAD\\" + fileName; //如果不存在目录,则创建目录 if (!Directory.Exists(Global.curEvt.path + "\\YBDLOAD")) { //创建文件夹 DirectoryInfo dirInfo = Directory.CreateDirectory(Global.curEvt.path + "\\YBDLOAD"); } if (File.Exists(filePath)) { File.Delete(filePath); } int iRes = Business(inputData, ref outputData); Global.writeLog("DLL下载文件返回:["+iRes+"]",inputData,outputData); if (iRes == 0) { dynamic joReturn = new JObject(); joReturn.errorCode = errorCode; joReturn.errorMessage = outputData; joReturn.filePath = filePath; outputData = joReturn.ToString(); } else { errorCode = -100; dynamic joReturn = new JObject(); joReturn.errorCode = errorCode; joReturn.errorMessage = outputData; outputData = joReturn.ToString(); return -1; } } catch (Exception ex) { errorCode = -100; error = ex.Message; dynamic joReturn = new JObject(); joReturn.errorCode = errorCode; joReturn.errorMessage = error; outputData = joReturn.ToString(); return -1; } finally { Global.writeLog("DownloadCenterFile" + "(" + Global.curEvt.URL + ")", inputData, outputData); } return 0; } public int UploadFile(string inputData, ref string outputData, ref string pErrMsg) { pErrMsg = ""; outputData = ""; JObject joRtn = new JObject(); try { inputData = inputData.Replace("\n", "").Replace("\t", "").Replace("\r", ""); StringBuilder errmsgSb = new StringBuilder(4096); StringBuilder outSb = new StringBuilder(40960); //调用业务函数 int pRtn = 0; // UploadFile(Global.inf.hospitalNO, Global.inf.CreditID, Global.inf.BusinessID, Global.inf.fileName, inputData, outSb, errmsgSb); if (pRtn != 0) { outputData = outSb.ToString(); pErrMsg = errmsgSb.ToString(); return -1; } else { outputData = outSb.ToString(); return 0; } } catch (Exception ex) { pErrMsg = "invokeUploadFileByDLL.UploadFile 异常:" + ex.Message; return -1; } finally { Global.writeLog("invokeUploadFileByDLL.UploadFile医保动态库上传业务函数", inputData, outputData); } } public int Init(ref string pErrMsg) { int pRtn = -1; pErrMsg = ""; JObject jo = new JObject(); try { jo.Add("IP", "10.58.77.20"); jo.Add("PORT", "8086"); jo.Add("TIMEOUT", "120"); jo.Add("LOG_PATH", "D:\\log\\"); jo.Add("EC_URL", "http://10.58.33.207:10086/localcfc/api/hsecfc/localQrCodeQuery"); jo.Add("CARD_PASSTYPE", "1"); jo.Add("API_NAME", "hssServives"); jo.Add("API_VERSION", "1.0.0"); jo.Add("ACCESS_KEY", Global.inf.AK); jo.Add("SECRETKEY", Global.inf.SK); jo.Add("ORG_ID", Global.inf.hospitalNO); jo.Add("EXT", ""); jo.Add("AREA_CODE", Global.inf.areaCode); StringBuilder sbInputData = new StringBuilder(jo.ToString()); StringBuilder sbOut = new StringBuilder(2048); pRtn = Init(sbInputData, sbOut); pErrMsg = sbOut.ToString(); return pRtn; } catch (Exception ex) { pErrMsg = "Init 异常:" + ex.Message; return -1; } finally { Global.writeLog("Init(" + Global.inf.centerURL + ")", jo.ToString(), pRtn.ToString() + pErrMsg); } } public int BusinessExt(string inputData, ref string outputData) { outputData = ""; string method = Global.curEvt.funNo; try { int pRtn = -1; JObject joRtn = new JObject(); try { StringBuilder sbInputData = new StringBuilder(inputData); StringBuilder sbOut = new StringBuilder(40960); pRtn = Busi(sbInputData, sbOut); outputData = sbOut.ToString(); Global.writeLog("DLL返回:" + outputData); return pRtn; } catch (Exception ex) { outputData = "Busi 异常:" + ex.Message; return -1; } finally { Global.writeLog("Busi(" + Global.inf.centerURL + ")", Global.inf.CreditID + ":" + Global.inf.BusinessID, pRtn.ToString()); } } catch (Exception ex) { outputData = "业务函数[" + method + "]异常:" + ex.Message; return -1; } finally { Global.writeLog("业务函数[" + method + "]", inputData, outputData); } } private string getResponseData(HttpWebResponse response) { string data = ""; if (response != null) { Stream s = response.GetResponseStream(); StreamReader sRead = new StreamReader(s, Encoding.GetEncoding("UTF-8")); data = sRead.ReadToEnd(); sRead.Close(); response.Close(); } return data; } #region【泰州医疗保障平台动态库调用】 /// /// 初始化 /// /// /// /// [DllImport("HeaSecReadInfo.dll", EntryPoint = "Init", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] static extern int Init(StringBuilder pInitInfo, StringBuilder pErrMsg); /// /// 读社保卡基本信息 /// /// /// /// [DllImport("HeaSecReadInfo.dll", EntryPoint = "ReadCardBas", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] static extern int ReadCardBas(StringBuilder pInitInfo, StringBuilder pBusiCardInfo); /// /// 检验PIN码 /// /// /// [DllImport("HeaSecReadInfo.dll", EntryPoint = "VerifyPIN", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] static extern int VerifyPIN(StringBuilder pOutBuff); /// /// 修改PIN码 /// /// /// [DllImport("HeaSecReadInfo.dll", EntryPoint = "ChangePIN", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] static extern int ChangePIN(StringBuilder pOutBuff); /// /// 读身份证信息 /// /// /// /// /// [DllImport("HeaSecReadInfo.dll", EntryPoint = "ReadSFZ", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] static extern int ReadSFZ(StringBuilder pPath, StringBuilder pOutBuff, StringBuilder pOutBusiBuff); /// /// 电子凭证解码 /// /// /// /// /// [DllImport("HeaSecReadInfo.dll", EntryPoint = "EcQuery", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] static extern int EcQuery(StringBuilder InData, StringBuilder OutData); /// /// 电子社保卡二维码解码 /// /// /// [DllImport("HeaSecReadInfo.dll", EntryPoint = "EcCardQuery", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] static extern int EcCardQuery(StringBuilder OutData); /// /// 四合一介质获得个人信息 /// /// /// [DllImport("HeaSecReadInfo.dll", EntryPoint = "GetPersonInfo", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] static extern int GetPersonInfo(StringBuilder OutData); /// /// 动态库日签到函数 /// /// /// /// [DllImport("HeaSecReadInfo.dll", EntryPoint = "DailySinIn", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] static extern int DailySinIn(StringBuilder pInData, StringBuilder pOutData); /// /// 业务函数 /// /// /// /// [DllImport("HeaSecReadInfo.dll", EntryPoint = "Busi", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] static extern int Busi(StringBuilder pInData, StringBuilder pOutData); #endregion } }