using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Reflection; using System.Net; using System.Management; using System.Xml; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using MedicareComLib; namespace BJShouXinYB_Demo { public partial class MainForm : Form { MedOrdersShareClass MOS = new MedOrdersShareClass(); AnalysisXML AnXML = new AnalysisXML(); string LocalMac = "38-F3-AB-9F-5D-A4"; string LocalIP = "10.0.12.199"; System.Type ComType; object ComInstance; public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { rbg_Type.SelectedIndex = 0; } public string GetIP() { IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName()); //string myip = IpEntry.AddressList[0].ToString();//IP6 string myip = IpEntry.AddressList[1].ToString();//IP4 return myip; } public string GetMAC() { try { //获取网卡硬件地址 string mac = ""; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) { mac = mo["MacAddress"].ToString(); mo.Dispose();//释放资源 break; } } moc = null; mc = null; return mac; } catch { return "unknow"; } } /// /// XML转换JSON对象 /// /// /// /// public JObject ConvertXMLToObject(string sXML) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(sXML); string jsonStr = JsonConvert.SerializeXmlNode(xmlDoc); jsonStr = jsonStr.Replace("@", "").ToString(); //MessageBox.Show(jsonStr); return JObject.Parse(jsonStr); } public int GetReadCardObj(out System.Type OutComType, out object OutComInstance) { Object rtnObj = ""; string progID = "MedicareCom.Outpatient"; System.Type ComType; object ComInstance; ComType = Type.GetTypeFromProgID(progID); // 创建Com的实例 if (ComType != null) { //创建实例 ComInstance = Activator.CreateInstance(ComType); OutComType = ComType; OutComInstance = ComInstance; return 0; } else { OutComType = null; OutComInstance = ""; return -1; } } public void OpenOrClose(string FunName, System.Type ComType, object ComInstance, out string output) { try { //Type ComType = System.Type.GetTypeFromProgID("MedicareCom.Outpatient"); if (ComType != null) { //创建实例,不能再次创建,否则会提示没有初始化 //object ComInstance = System.Activator.CreateInstance(ComType); if (ComInstance == null) { output = "实例不存在!"; return; } //GlobalVariables.writeLog("入参infno:" + infno); //GlobalVariables.writeLog("入参input:" + input); //设置需要设置的参数值 object[] ParamArray = new object[1]; ParamArray[0] = ""; //ParamArray[1] = input; ParameterModifier[] ParamMods = new ParameterModifier[1]; ParamMods[0] = new ParameterModifier(1); // 初始化为接口参数的个数 ParamMods[0][0] = true; //object o = ShouXinComType.GetMethod(FunName, BindingFlags.Default | BindingFlags.InvokeMethod); ComType.InvokeMember(FunName, // 接口函数名 BindingFlags.Default | BindingFlags.InvokeMethod, null, ComInstance, // 调用的COM组件 ParamArray, // 参数数组 ParamMods, // 指定返回参数的ParameterModifier数组 null, null); output = ParamArray[0].ToString(); MessageBox.Show("Com输出:" + output); } else { output = "北京首信医保COM加载失败!"; //MessageBox.Show("COM加载失败!"); } } catch (Exception ex) { output = ex.Message; MessageBox.Show("COM加载异常!" + output); } } public void Myobject(string FunName, string input, out string output) { output = ""; try { Type ShouXinComType = System.Type.GetTypeFromProgID("MedicareCom.Outpatient"); if (ShouXinComType != null) { //创建实例,不能再次创建,否则会提示没有初始化 object ShouXinComInstance = System.Activator.CreateInstance(ShouXinComType); if (ShouXinComInstance != null) { MessageBox.Show(("函数FunName:" + FunName + "\n" + "入参:" + input)); } else { output = "实例不存在!"; return; } //GlobalVariables.writeLog("入参infno:" + infno); //GlobalVariables.writeLog("入参input:" + input); //设置需要设置的参数值 object[] ParamArray = new object[2]; ParamArray[0] = input; ParamArray[1] = input; ParameterModifier[] ParamMods = new ParameterModifier[1]; ParamMods[0] = new ParameterModifier(2); // 初始化为接口参数的个数 ParamMods[0][0] = true; ParamMods[0][1] = true; ShouXinComType.InvokeMember(FunName, // 接口函数名 BindingFlags.Default | BindingFlags.InvokeMethod, null, ShouXinComInstance, // 调用的COM组件 ParamArray, // 参数数组 ParamMods, // 指定返回参数的ParameterModifier数组 null, null); output = ParamArray[1].ToString(); } else { output = "北京首信医保COM加载失败!"; //MessageBox.Show("COM加载失败!"); } } catch (Exception ex) { output = ex.Message; MessageBox.Show("COM加载异常!" + output); } } public string GetinPar(string sInpar,int i) { string Str = ""; if (i == 0) { Str = @""; return Str + "" + sInpar + ""; } else if (i == 1) { Str = @""; return Str + "" + sInpar + ""; } else if (i == 2) { Str = @" "; return Str + "" + sInpar + ""; } else { return ""; } } private void btn_Open_Click(object sender, EventArgs e) { string AppMsg; ComType = null; ComInstance = null; if (GetReadCardObj(out ComType, out ComInstance) != 0) { MessageBox.Show("实例创建失败!"); return; } OpenOrClose("open", ComType, ComInstance, out AppMsg); sOutPut.Text = AppMsg; } private void btn_Close_Click(object sender, EventArgs e) { string AppMsg = ""; OpenOrClose("close", ComType, ComInstance, out AppMsg); sOutPut.Text = AppMsg; } private void btn_Exit_Click(object sender, EventArgs e) { //btn_Close_Click(sender, e); Close(); } public int ReadCard(out string outParam,out int iOtherProvLocal) { outParam = ""; iOtherProvLocal = 0; string FunName, Appmsg, AppRtnMsg, OutMsg, CloseOutMsg = string.Empty; //System.Type ComType; //object ComInstance; ReadCardClass rcl = new ReadCardClass(); ChooseCard cc = new ChooseCard(); cc.StartPosition = FormStartPosition.CenterScreen; try { if (cc.ShowDialog() == DialogResult.OK) { //【医保电子凭证】 if (cc.sL_CardType == 1) { Appmsg = "" + (cc.sL_CardType + 1) + "" + "" + cc.QrCode + "" + "" + cc.sYMYWLX + "" + "" + GetMAC().Replace("-", string.Empty) + "" + "" + GetIP() + "" + "007" + "CM" + "1001" + "收费处"; Appmsg = GetinPar(Appmsg, 0); } else { Appmsg = "" + (cc.sL_CardType + 1) + ""; Appmsg = GetinPar(Appmsg, 0); } //【社保卡-本地】 if (cc.iOtherProvLocal == 0) { FunName = "GetPersonInfo_Multimedia"; //FunName = "GetCardInfo"; } //【社保卡-异地】 else { FunName = "GetPersonInfoNation_Multimedia"; } iOtherProvLocal = cc.iOtherProvLocal; //创建首信医保Com实例 ComType = null; ComInstance = null; if (rcl.GetReadCardObj(out ComType, out ComInstance) != 0) { outParam = "实例创建失败!"; return -1; } else { //读卡前先Open if (cc.sL_CardType == 0) rcl.Open(ComType, ComInstance, out AppRtnMsg); sInput.Text = Appmsg; rcl.ReadCardObj(FunName, Appmsg, ComType, ComInstance, out AppRtnMsg); sOutPut.Text = AppRtnMsg; XmlDocument xmlDoc = AnXML.GetXmlDoc(AppRtnMsg); Boolean bPass = AnXML.CheckState(xmlDoc, out OutMsg); XmlNode dataNode = AnXML.GetNodeFromPath(xmlDoc.DocumentElement, "output"); //读卡结束一定要close不然再次读卡会报实例错 if (cc.sL_CardType == 0) rcl.Close(ComType, ComInstance, out CloseOutMsg); if (bPass == false) { outParam = "调用 " + FunName + " 获取参保信息失败,中心返回错误信息:" + OutMsg + "\r\n" + "Close输出:" + CloseOutMsg; return -1; } outParam = AppRtnMsg; return 0; } } else { outParam = "收款员取消读卡!"; return -1; } } catch (Exception ex) { if ((ComType != null) && (cc.sL_CardType == 0)) rcl.Close(ComType, ComInstance, out CloseOutMsg); outParam = "ReadCard异常:" + ex.Message; return -1; } } public int ShowPatInfo(string patInfo,int type, out string outParam) { outParam = ""; //弹框展示参保信息 ReadCardInfo frmPatientInfo = new ReadCardInfo(patInfo, type); frmPatientInfo.StartPosition = FormStartPosition.CenterScreen; try { if (frmPatientInfo.ShowDialog() != DialogResult.OK) { outParam = "收款员取消读卡"; return -1; } else { return 0; } } catch (Exception ex) { outParam = "异常:" + ex.Message; return -1; } } private void btn_GetPersonInfo_Click(object sender, EventArgs e) { string outParam; int iOtherProvLocal; OutpatientClass hObj = new OutpatientClass(); hObj.Open(out string sOut); hObj.GetPersonInfo(out outParam); sOutPut.Text = outParam; hObj.Close(out sOut); MessageBox.Show(outParam); return; if (ReadCard(out outParam, out iOtherProvLocal) != 0) { MessageBox.Show(outParam); return; } else { if (ShowPatInfo(outParam, iOtherProvLocal, out outParam) != 0) { MessageBox.Show(outParam); } } } private void uiButton1_Click(object sender, EventArgs e) { string sRtnXML = sInput.Text; if (sRtnXML == "") { MessageBox.Show("入参不能为空!"); return; } XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(sRtnXML); string jsonStr = JsonConvert.SerializeXmlNode(xmlDoc); jsonStr = jsonStr.Replace("@", "").ToString(); JObject joRtn = JObject.Parse(jsonStr); MedInsuFee frmFee = new MedInsuFee(); //XML转换成JSON数组 if (CountOccurrences(sInput.Text, "itemno") > 1) { frmFee.DtFee = (DataTable)joRtn["root"]["output"]["feeitemarray"]["feeitem"].ToObject(typeof(DataTable)); } else { JArray jaFee = new JArray(JObject.Parse(getDestValue(joRtn, "root.output.feeitemarray.feeitem"))); //jaFee.Add(JObject.Parse(getDestValue(joRtn, "root.output.feeitemarray.feeitem"))); frmFee.DtFee = (DataTable)jaFee.ToObject(typeof(DataTable)); } frmFee.ShowDialog(); } public static string getDestValue(JObject jo, string destPath) { JToken jt = jo.SelectToken("$." + destPath); if (jt != null) { return jt.ToString(); } else { // Global.writeLog(destPath + "的JToken属性值为空"); return ""; } } public static int CountOccurrences(string str, string subStr) { int count = 0; int index = -1; // 初始化为-1,因此会从第二个字符开始比较 while ((index = str.IndexOf(subStr, index+1)) != -1) { count++; index += subStr.Length; // 更新索引位置到下一个匹配项的起始位置 } return count; } private void btnConnectServer_Click(object sender, EventArgs e) { string FunName, Appmsg, AppRtnMsg, OutMsg, CloseOutMsg = string.Empty; //创建首信医保Com实例 ComType = null; ComInstance = null; if (MOS.GetMedOrdersShareObj(out ComType, out ComInstance) != 0) { MessageBox.Show("实例创建失败!"); return; } MOS.ConnectServer(ComType, ComInstance, out AppRtnMsg); sOutparam.Text = AppRtnMsg; } private void btnGetClinicHistoryTrade_Click(object sender, EventArgs e) { string FunName, Appmsg, AppRtnMsg, OutMsg, CloseOutMsg = string.Empty; string infotype; if (tbPsnNo.Text == "") { MessageBox.Show("医保编码不能为空!"); return; } infotype = "0" + rbg_Type.SelectedIndex.ToString(); //1.创建实例 if (MOS.GetMedOrdersShareObj(out ComType, out ComInstance) != 0) { MessageBox.Show("实例创建失败!"); return; } //2.初始化 MOS.ConnectServer(ComType, ComInstance, out AppRtnMsg); //3.业务调用 FunName = "GetClinicHistoryTrade"; Appmsg = "" + (tbPsnNo.Text) + "" + "" + "" + infotype + ""; Appmsg = GetinPar(Appmsg, 0); sInparam.Text = Appmsg; MOS.MedOrdersShareObj(FunName, Appmsg, ComType, ComInstance, out AppRtnMsg); //解析返回值 XmlDocument xmlDoc = AnXML.GetXmlDoc(AppRtnMsg); Boolean bPass = AnXML.CheckState(xmlDoc, out OutMsg); XmlNode dataNode = AnXML.GetNodeFromPath(xmlDoc.DocumentElement, "output"); if (bPass) { //sOutparam.Text = ConvertXMLToObject(AppRtnMsg).ToString(); JObject joRtn = new JObject(JObject.Parse(getDestValue(ConvertXMLToObject(AppRtnMsg), "root.output"))); sOutparam.Text = joRtn.ToString(); } else sOutparam.Text = AppRtnMsg; } private void btnPresUpload_Click(object sender, EventArgs e) { ReadCardClass rcl = new ReadCardClass(); string PutExRxInfo = sInput.Text, Appmsg = "", AppRtnMsg = "", OutMsg = "", OutMsg_Close = "", InsuOutMsg = "", outParam = ""; if (PutExRxInfo == "") { MessageBox.Show("外配处方入参不能为空!"); return; } //创建首信医保Com实例 ComType = null; ComInstance = null; if (rcl.GetReadCardObj(out ComType, out ComInstance) != 0) { outParam = "实例创建失败!"; return; } else { //读卡前先Open rcl.Open(ComType, ComInstance, out AppRtnMsg); Appmsg = "1"; Appmsg = GetinPar(Appmsg, 0); rcl.ReadCardObj("GetPersonInfo_ExRx_RQ", Appmsg, ComType, ComInstance, out AppRtnMsg); sOutPut.Text = AppRtnMsg; XmlDocument xmlDoc = AnXML.GetXmlDoc(AppRtnMsg); if (AnXML.CheckState(xmlDoc, out OutMsg) == false) { rcl.Close(ComType, ComInstance, out OutMsg_Close); MessageBox.Show("获取参保信息GetPersonInfo_ExRx_RQ失败,中心返回错误信息:" + OutMsg + "\r\n" + "调用【Close】关闭读卡设备输出:" + OutMsg_Close); return; } else { MessageBox.Show(AppRtnMsg); //4.调用提交外配处方信息 rcl.ReadCardObj("PutExRxInfo", PutExRxInfo, ComType, ComInstance, out AppRtnMsg); xmlDoc = AnXML.GetXmlDoc(AppRtnMsg); if (AnXML.CheckState(xmlDoc, out OutMsg) == false) { rcl.Close(ComType, ComInstance, out OutMsg_Close); MessageBox.Show("提交外配处方信息PutExRxInfo失败,中心返回错误信息:" + OutMsg + "\r\n" + "调用【Close】关闭读卡设备输出:" + OutMsg_Close); return; } else { xmlDoc = null; rcl.Close(ComType, ComInstance, out OutMsg_Close); MessageBox.Show("提交外配处方信息成功:" + AppRtnMsg); return; } } } } private void uiButton3_Click(object sender, EventArgs e) { sOutPut.Text = ConvertXMLToObject(sInput.Text).ToString(); } private void uiButton4_Click(object sender, EventArgs e) { int iOtherProvLocal = 0; string FunName, Appmsg, AppRtnMsg, OutMsg, CloseOutMsg = string.Empty; //System.Type ComType; //object ComInstance; //创建首信医保Com实例 ComType = null; ComInstance = null; ReadCardClass rcl = new ReadCardClass(); ChooseCard cc = new ChooseCard(); cc.StartPosition = FormStartPosition.CenterScreen; try { if (cc.ShowDialog() == DialogResult.OK) { //【医保电子凭证】 if (cc.sL_CardType == 1) { Appmsg = "" + (cc.sL_CardType + 1) + "" + "" + cc.QrCode + "" + "" + cc.sYMYWLX + "" + "" + GetMAC().Replace("-", string.Empty) + "" + "" + GetIP() + "" + "007" + "CM" + "1001" + "收费处"; Appmsg = GetinPar(Appmsg, 0); } else { Appmsg = "" + (cc.sL_CardType + 1) + ""; Appmsg = GetinPar(Appmsg, 0); } if (rcl.GetReadCardObj(out ComType, out ComInstance) != 0) { MessageBox.Show("实例创建失败"); return; } //【社保卡-本地】 if (cc.iOtherProvLocal == 0) { FunName = "GetPersonInfo_Multimedia"; } else //【社保卡-异地】 { FunName = "GetPersonInfoNation_Multimedia"; } iOtherProvLocal = cc.iOtherProvLocal; //读卡前先Open rcl.Open(ComType, ComInstance, out AppRtnMsg); sInput.Text = Appmsg; rcl.ReadCardObj("GetPersonClass_Multimedia", Appmsg, ComType, ComInstance, out AppRtnMsg); XmlDocument xmlDoc = AnXML.GetXmlDoc(AppRtnMsg); Boolean bPass = AnXML.CheckState(xmlDoc, out OutMsg); XmlNode dataNode = AnXML.GetNodeFromPath(xmlDoc.DocumentElement, "output"); if (bPass == false) { MessageBox.Show("调用GetPersonClass_Multimedia获取参保类别信息失败,中心返回错误信息:" + OutMsg + "\r\n" + "Close输出:" + CloseOutMsg); return; } else { MessageBox.Show("调用GetPersonClass_Multimedia获取参保类别信息成功返回:" + AppRtnMsg); return; } rcl.ReadCardObj(FunName, Appmsg, ComType, ComInstance, out AppRtnMsg); sOutPut.Text = AppRtnMsg; xmlDoc = AnXML.GetXmlDoc(AppRtnMsg); bPass = AnXML.CheckState(xmlDoc, out OutMsg); dataNode = AnXML.GetNodeFromPath(xmlDoc.DocumentElement, "output"); //读卡结束一定要close不然再次读卡会报实例错 rcl.Close(ComType, ComInstance, out CloseOutMsg); if (bPass == false) { MessageBox.Show("调用 " + FunName + " 获取参保信息失败,中心返回错误信息:" + OutMsg + "\r\n" + "Close输出:" + CloseOutMsg); return; } MessageBox.Show(AppRtnMsg); return; } else { MessageBox.Show("收款员取消读卡"); return; } } catch (Exception ex) { if ((ComType != null) && (cc.sL_CardType == 0)) rcl.Close(ComType, ComInstance, out CloseOutMsg); MessageBox.Show("ReadCard异常:" + ex.Message); return; } } } }