Common.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Management;
  7. using System.Net;
  8. using System.IO;
  9. using System.Runtime.InteropServices;
  10. using Newtonsoft.Json.Linq;
  11. using MedicalInsurance.Business;
  12. using System.Windows.Forms;
  13. using System.Reflection;
  14. namespace ChengDuMedInsu2
  15. {
  16. class Common
  17. {
  18. public string GetMAC()
  19. {
  20. try
  21. {
  22. //获取网卡硬件地址
  23. string mac = "";
  24. ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  25. ManagementObjectCollection moc = mc.GetInstances();
  26. foreach (ManagementObject mo in moc)
  27. {
  28. if ((bool)mo["IPEnabled"] == true)
  29. {
  30. mac = mo["MacAddress"].ToString();
  31. mo.Dispose();//释放资源
  32. break;
  33. }
  34. }
  35. moc = null;
  36. mc = null;
  37. return mac;
  38. }
  39. catch
  40. {
  41. return "unknow";
  42. }
  43. }
  44. public string GetIP()
  45. {
  46. IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
  47. //string myip = IpEntry.AddressList[0].ToString();//IP6
  48. string myip = IpEntry.AddressList[1].ToString();//IP4
  49. return myip;
  50. }
  51. }
  52. class Log
  53. {
  54. public string DirectoryPath { get; set; }
  55. public string LogName{ get; set; }
  56. public Log(string DirectoryPath,string LogName)
  57. {
  58. this.DirectoryPath = DirectoryPath;
  59. this.LogName = LogName;
  60. }
  61. //创建文件夹
  62. //参数:path 文件夹路径
  63. public bool CreateFolder(string path)
  64. {
  65. try
  66. {
  67. if (Directory.Exists(path))
  68. {
  69. return true;
  70. }
  71. if (!Directory.Exists(path.Substring(0, path.LastIndexOf("\\"))))
  72. { //若路径中无“\”则表示路径错误
  73. return false;
  74. }
  75. else
  76. {
  77. //创建文件夹
  78. DirectoryInfo dirInfo = Directory.CreateDirectory(path);
  79. return true;
  80. }
  81. }
  82. catch (Exception ex)
  83. {
  84. return false;
  85. }
  86. }
  87. //创建文件
  88. //参数:path 文件路径
  89. public void CreateFile(string path)
  90. {
  91. try
  92. {
  93. if (CreateFolder(path.Substring(0, path.LastIndexOf("\\"))))
  94. {
  95. if (!File.Exists(path))
  96. {
  97. FileStream fs = File.Create(path);
  98. fs.Close();
  99. }
  100. }
  101. }
  102. catch (Exception ex)
  103. {
  104. return;
  105. }
  106. }
  107. //删除文件
  108. //参数:path 文件夹路径
  109. public void DeleteFile(string path)
  110. {
  111. try
  112. {
  113. if (!File.Exists(path))
  114. {
  115. return;
  116. }
  117. else
  118. {
  119. File.Delete(path);
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. return;
  125. }
  126. }
  127. /// <summary>
  128. /// 将即时日志保存入日志文件
  129. /// </summary>
  130. public void Write(string content)
  131. {
  132. if (!Directory.Exists(this.DirectoryPath))
  133. {
  134. CreateFolder(this.DirectoryPath);
  135. }
  136. try
  137. {
  138. //写入新的文件
  139. string filePath = this.DirectoryPath + "\\" + this.LogName;
  140. FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write);
  141. StreamWriter sw = new StreamWriter(fs);
  142. sw.WriteLine(content);
  143. sw.Close();
  144. fs.Close();
  145. }
  146. catch (Exception ex)
  147. {
  148. }
  149. }
  150. }
  151. class IniFile
  152. {
  153. public string FullPath; //INI全路径
  154. [DllImport("kernel32")]
  155. private static extern long WritePrivateProfileString(string section, string key,
  156. string val, string filePath);
  157. [DllImport("kernel32")]
  158. private static extern int GetPrivateProfileString(string section, string key, string def,
  159. StringBuilder retVal, int size, string filePath);
  160. public IniFile(string path)
  161. {
  162. this.FullPath = path;
  163. }
  164. //写INI文件
  165. public void WriteValue(string Section, string Key, string Value)
  166. {
  167. WritePrivateProfileString(Section, Key, Value, this.FullPath);
  168. }
  169. //读取INI文件指定
  170. public string ReadValue(string Section, string Key)
  171. {
  172. StringBuilder temp = new StringBuilder(255);
  173. int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.FullPath);
  174. return temp.ToString();
  175. }
  176. }
  177. public static class GlobalVariables
  178. {
  179. #region 全局变量
  180. //当前路径
  181. public static String currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  182. //医院的基本信息
  183. public static String hospitalID = "";
  184. public static String hospitalName = "";
  185. public static int hospitalDr = 0;
  186. public static string hospitalLevel = "";
  187. //接口信息
  188. public static string InterfaceID = "";
  189. public static int interfaceDr = 0;
  190. //医保环境信息
  191. public static String signno = "";
  192. public static String centerURL = "";
  193. public static String ecURL = "";
  194. public static String hospitalNO = "";
  195. public static String hospitalAreaCode = "";
  196. public static String patientAreaCode = "";
  197. public static String recivedSystemCode = "";
  198. public static String certificationAuthorityInfo = "";
  199. public static String signatureType = "";
  200. public static String interfaceVersion = "";
  201. public static String deviceSafetyInfo = "";
  202. public static String businessDllName = "";
  203. public static String businessDllNameSpace = "";
  204. //IRIS信息
  205. public static String irisServiceIP = "";
  206. public static String irisServiceURL = "";
  207. public static String irisServiceAuthorization = "";
  208. //登录用户信息
  209. public static String operatoType = "";
  210. public static String operatorNO = "";
  211. public static String operatorName = "";
  212. //医保患者信息
  213. public static string insuplc_admdvs = "";
  214. public static string mdtrt_cert_type = "";
  215. public static string mdtrt_cert_no = "";
  216. public static string psn_cert_type = "";
  217. public static string certno = "";
  218. public static string psn_name = "";
  219. public static string card_sn = "";
  220. public static string psn_no = "";
  221. public static string card_token = "";
  222. //
  223. //public static int errorcode = 0;
  224. //public static string errormessage = "";
  225. #endregion
  226. public static void printGlobalVariablesVlaue()
  227. {
  228. var varList = new List<string>();
  229. string outParam = "";
  230. varList.Add("currentDirectory =" + currentDirectory);
  231. varList.Add("hospitalID =" + hospitalID);
  232. varList.Add("hospitalName =" + hospitalName);
  233. varList.Add("hospitalDr =" + hospitalDr);
  234. varList.Add("InterfaceID =" + InterfaceID);
  235. varList.Add("interfaceDr =" + interfaceDr);
  236. varList.Add("signno =" + signno);
  237. varList.Add("centerURL =" + centerURL);
  238. varList.Add("ecURL =" + ecURL);
  239. varList.Add("hospitalNO =" + hospitalNO);
  240. varList.Add("hospitalAreaCode =" + hospitalAreaCode);
  241. varList.Add("patientAreaCode =" + patientAreaCode);
  242. varList.Add("recivedSystemCode =" + recivedSystemCode);
  243. varList.Add("certificationAuthorityInfo =" + certificationAuthorityInfo);
  244. varList.Add("signatureType =" + signatureType);
  245. varList.Add("interfaceVersion =" + interfaceVersion);
  246. varList.Add("deviceSafetyInfo =" + deviceSafetyInfo);
  247. varList.Add("businessDllName =" + businessDllName);
  248. varList.Add("businessDllNameSpace =" + businessDllNameSpace);
  249. varList.Add("irisServiceIP =" + irisServiceIP);
  250. varList.Add("irisServiceURL =" + irisServiceURL);
  251. varList.Add("irisServiceAuthorization =" + irisServiceAuthorization);
  252. varList.Add("operatoType =" + operatoType);
  253. varList.Add("operatorNO =" + operatorNO);
  254. varList.Add("operatorName =" + operatorName);
  255. for (int i = 0; i < varList.Count; i++)
  256. outParam = outParam + (varList[i] + "\r\n");
  257. writeLog("printGlobalVariablesVlaue", "", outParam);
  258. }
  259. public static int Init(string inParam)
  260. {
  261. string exMsg = string.Empty;
  262. string outParam = "";
  263. var varList = new List<string>();
  264. int iResult = -1;
  265. try
  266. {
  267. //获取配置文件里的IRIS服务信息
  268. IniFile ini = new IniFile(currentDirectory + @"\INSUConfigure.ini");
  269. irisServiceIP = ini.ReadValue("HIS", "ip");
  270. varList.Add("irisServiceIP =" + irisServiceIP);
  271. irisServiceURL = ini.ReadValue("HIS", "url");
  272. varList.Add("irisServiceURL =" + irisServiceURL);
  273. irisServiceAuthorization = ini.ReadValue("HIS", "authorization");
  274. varList.Add("irisServiceAuthorization =" + irisServiceAuthorization);
  275. businessDllNameSpace = ini.ReadValue("INTERFACEINFO", "businessDllNameSpace");//后面统一为MedicalInsurance
  276. varList.Add("businessDllNameSpace =" + businessDllNameSpace);
  277. //获取his(或者说壳程序)传过来的基本信息
  278. JObject joInParam = JObject.Parse(inParam);
  279. JArray jaParams = JArray.FromObject(joInParam["params"]);
  280. JObject joParam = JObject.FromObject(jaParams[0]);
  281. hospitalDr = (int)joParam["hospitalDr"];
  282. varList.Add("hospitalDr =" + hospitalDr);
  283. hospitalName = (string)joParam["hospitalName"];
  284. varList.Add("hospitalName =" + hospitalName);
  285. operatorNO = (string)joParam["operatorNO"];
  286. varList.Add("operatorNO =" + operatorNO);
  287. operatorName = (string)joParam["operatorName"];
  288. varList.Add("operatorName =" + operatorName);
  289. InterfaceID = (string)joParam["InterfaceID"];
  290. varList.Add("InterfaceID =" + InterfaceID);
  291. //MessageBox.Show(joParam["InterfaceID"].ToString());
  292. //if (int.TryParse(joParam["InterfaceDr"].ToString(), out interfaceDr))
  293. //{ MessageBox.Show("2"); }
  294. //else
  295. //{ MessageBox.Show("2"); }
  296. interfaceDr = (int)joParam["InterfaceDr"];
  297. operatoType = "3";
  298. varList.Add("operatoType =" + operatoType);
  299. //通过irsi服务获取接口信息09010003
  300. //IrisInterfaceService iris = new IrisInterfaceService();
  301. //JObject joInterface = iris.getCurrentInterface();
  302. //centerURL = (string)joInterface["CenterURL"];
  303. //hospitalAreaCode = (string)joInterface["AreaCode"];
  304. //businessDllName = (string)joInterface["DLLName"];
  305. //hospitalNO = (string)joInterface["HospitalNO"];
  306. centerURL = (string)joParam["centerURL"];
  307. varList.Add("centerURL =" + centerURL);
  308. hospitalAreaCode = (string)joParam["areaCode"];
  309. varList.Add("hospitalAreaCode =" + hospitalAreaCode);
  310. businessDllName = (string)joParam["dllName"];
  311. varList.Add("businessDllName =" + businessDllName);
  312. hospitalNO = (string)joParam["hospitalNO"];
  313. varList.Add("hospitalNO =" + hospitalNO);
  314. interfaceVersion = "v1.0";
  315. varList.Add("interfaceVersion =" + interfaceVersion);
  316. signatureType = "sm3";
  317. varList.Add("signatureType =" + signatureType);
  318. recivedSystemCode = "YBXT";
  319. varList.Add("recivedSystemCode =" + recivedSystemCode);
  320. iResult = 0;
  321. for (int i = 0; i < varList.Count; i++)
  322. outParam = outParam + (varList[i]) + "\r\n";
  323. writeLog("getInterfaceInfo", inParam, outParam);
  324. return iResult;
  325. }
  326. catch (Exception ex)
  327. {
  328. exMsg = "全局变量初始化异常:" + ex.Message;
  329. return iResult;
  330. }
  331. finally
  332. {
  333. if (iResult != 0)
  334. {
  335. writeLog("全局变量初始化", inParam, "GlobalVariables.Init异常:" + exMsg);
  336. }
  337. else
  338. {
  339. writeLog("全局变量初始化", inParam, outParam);
  340. }
  341. }
  342. }
  343. /// <summary>
  344. /// 初始化接口信息(包含医院信息),初始化后会那啥
  345. /// </summary>
  346. /// <param name="joInterface"></param>
  347. public static void getInterfaceInfo(JObject joInterface)
  348. {
  349. var varList = new List<string>();//获取his(或者说壳程序)传过来的基本信息
  350. hospitalDr = (int)joInterface["hospitalDr"];
  351. varList.Add("hospitalDr =" + hospitalDr);
  352. hospitalName = (string)joInterface["hospitalName"];
  353. varList.Add("hospitalName =" + hospitalName);
  354. operatorNO = (string)joInterface["operatorNO"];
  355. varList.Add("operatorNO =" + operatorNO);
  356. operatorName = (string)joInterface["operatorName"];
  357. varList.Add("operatorName =" + operatorName);
  358. InterfaceID = (string)joInterface["InterfaceID"];
  359. varList.Add("InterfaceID =" + InterfaceID);
  360. interfaceDr = (int)joInterface["InterfaceDr"];
  361. varList.Add("interfaceDr =" + interfaceDr);
  362. operatoType = "3";
  363. varList.Add("operatoType =" + operatoType);
  364. centerURL = (string)joInterface["centerURL"];
  365. varList.Add("centerURL =" + centerURL);
  366. hospitalAreaCode = (string)joInterface["areaCode"];
  367. varList.Add("hospitalAreaCode =" + hospitalAreaCode);
  368. businessDllName = (string)joInterface["dllName"];
  369. varList.Add("businessDllName =" + businessDllName);
  370. hospitalNO = (string)joInterface["hospitalNO"];
  371. varList.Add("hospitalNO =" + hospitalNO);
  372. interfaceVersion = "v1.0";
  373. varList.Add("interfaceVersion =" + interfaceVersion);
  374. signatureType = "sm3";
  375. varList.Add("signatureType =" + signatureType);
  376. recivedSystemCode = "YBXT";
  377. varList.Add("recivedSystemCode =" + recivedSystemCode);
  378. string outParam = "";
  379. for (int i = 0; i < varList.Count; i++)
  380. outParam = outParam + (varList[i]) + "\r\n";
  381. writeLog("getInterfaceInfo", joInterface.ToString(), outParam);
  382. }
  383. public static void getInterfaceInfoByIris(JObject joInterface)
  384. {
  385. var varList = new List<string>();//获取his(或者说壳程序)传过来的基本信息
  386. interfaceDr = (int)joInterface["ID"];
  387. varList.Add("interfaceDr =" + interfaceDr);
  388. InterfaceID = (string)joInterface["InterfaceID"];
  389. varList.Add("InterfaceID =" + InterfaceID);
  390. hospitalNO = (string)joInterface["HospitalNO"];
  391. varList.Add("hospitalNO =" + hospitalNO);
  392. hospitalDr = (int)joInterface["HospitalDr"];
  393. varList.Add("hospitalDr =" + hospitalDr);
  394. hospitalLevel = (string)joInterface["HospitalLevel"];
  395. varList.Add("hospitalLevel =" + hospitalLevel);
  396. centerURL = (string)joInterface["CenterURL"];
  397. varList.Add("centerURL =" + centerURL);
  398. hospitalAreaCode = (string)joInterface["AreaCode"];
  399. varList.Add("hospitalAreaCode =" + hospitalAreaCode);
  400. businessDllName = (string)joInterface["DLLName"];
  401. varList.Add("businessDllName =" + businessDllName);
  402. operatoType = "3";
  403. varList.Add("operatoType =" + operatoType);
  404. interfaceVersion = "v1.0";
  405. varList.Add("interfaceVersion =" + interfaceVersion);
  406. signatureType = "sm3";
  407. varList.Add("signatureType =" + signatureType);
  408. recivedSystemCode = "YBXT";
  409. varList.Add("recivedSystemCode =" + recivedSystemCode);
  410. string outParam = "";
  411. for (int i = 0; i < varList.Count; i++)
  412. outParam = outParam + (varList[i]) + "\r\n";
  413. writeLog("getInterfaceInfoByIris", joInterface.ToString(), outParam);
  414. }
  415. public static void getIrisConfig()
  416. {
  417. var varList = new List<string>();
  418. //获取配置文件里的IRIS服务信息
  419. IniFile ini = new IniFile(currentDirectory + @"\INSUConfigure.ini");
  420. irisServiceIP = ini.ReadValue("HIS", "ip");
  421. varList.Add("irisServiceIP =" + irisServiceIP);
  422. irisServiceURL = ini.ReadValue("HIS", "url");
  423. varList.Add("irisServiceURL =" + irisServiceURL);
  424. irisServiceAuthorization = ini.ReadValue("HIS", "authorization");
  425. varList.Add("irisServiceAuthorization =" + irisServiceAuthorization);
  426. string outParam = "";
  427. for (int i = 0; i < varList.Count; i++)
  428. outParam = outParam + (varList[i]);
  429. writeLog("getIrisConfig:" + outParam);
  430. }
  431. public static void getInterfaceByHISParam(JObject joInParam)
  432. {
  433. hospitalDr = int.Parse(joInParam["hospitalDr"].ToString());
  434. interfaceDr = int.Parse(joInParam["InterfaceDr"].ToString());
  435. hospitalName = joInParam["hospitalName"].ToString();
  436. operatorNO = joInParam["hospitalDr"].ToString();
  437. operatorName = joInParam["hospitalDr"].ToString();
  438. }
  439. public static void writeLog(string content)
  440. {
  441. string logDir = GlobalVariables.currentDirectory + "\\Log", logName = DateTime.Now.ToString("yyyy-MM-dd") + "_YW.Log";
  442. if (!Directory.Exists(logDir))
  443. {
  444. //创建文件夹
  445. DirectoryInfo dirInfo = Directory.CreateDirectory(logDir);
  446. }
  447. if (!File.Exists(logDir + "\\" + logName))
  448. {
  449. FileStream fs1 = File.Create(logDir + "\\" + logName);
  450. fs1.Close();
  451. }
  452. FileStream fs = new FileStream(logDir + "\\" + logName, FileMode.Append, FileAccess.Write);
  453. StreamWriter sw = new StreamWriter(fs);
  454. string timeLine = "**********" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff") + "***********" + "\r\n";
  455. sw.WriteLine(timeLine + content);
  456. sw.Close();
  457. fs.Close();
  458. }
  459. public static void writeLog(string tradeName, string inParam, string outParam)
  460. {
  461. string logDir = GlobalVariables.currentDirectory + "\\Log", logName = DateTime.Now.ToString("yyyy-MM-dd") + "_YW.Log";
  462. string statrTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff");
  463. if (!Directory.Exists(logDir))
  464. {
  465. //创建文件夹
  466. DirectoryInfo dirInfo = Directory.CreateDirectory(logDir);
  467. }
  468. if (!File.Exists(logDir + "\\" + logName))
  469. {
  470. FileStream fs1 = File.Create(logDir + "\\" + logName);
  471. fs1.Close();
  472. }
  473. FileStream fs = new FileStream(logDir + "\\" + logName, FileMode.Append, FileAccess.Write);
  474. StreamWriter sw = new StreamWriter(fs);
  475. string content = "****************************交易开始(" + statrTime + ")****************************" + "\r\n";
  476. content = content + "交易名称:" + tradeName + "\r\n";
  477. content = content + "交易入参:" + inParam + "\r\n";
  478. content = content + "交易出参:" + outParam + "\r\n";
  479. //content = content + "****************************交易结束(" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff") + ")****************************" + "\r\n";
  480. sw.WriteLine(content);
  481. sw.Close();
  482. fs.Close();
  483. }
  484. public static string SetJsonParam(string infno, string input)
  485. {
  486. dynamic Jo = new JObject();
  487. Jo.infno = infno;
  488. Jo.msgid = GlobalVariables.hospitalNO + DateTime.Now.ToString("yyyyMMddHHmmssffff");
  489. Jo.mdtrtarea_admvs = GlobalVariables.hospitalAreaCode;
  490. Jo.insuplc_admdvs = GlobalVariables.patientAreaCode;
  491. Jo.recer_sys_code = GlobalVariables.recivedSystemCode;
  492. Common common = new Common();
  493. Jo.dev_no = common.GetMAC();
  494. Jo.dev_safe_info = GlobalVariables.deviceSafetyInfo;
  495. Jo.cainfo = GlobalVariables.certificationAuthorityInfo; ;
  496. Jo.signtype = GlobalVariables.signatureType; ;
  497. Jo.infver = GlobalVariables.interfaceVersion; ;
  498. Jo.opter_type = GlobalVariables.operatoType; ;
  499. Jo.opter = GlobalVariables.operatorNO; ;
  500. Jo.opter_name = GlobalVariables.operatorName; ;
  501. Jo.inf_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  502. Jo.fixmedins_code = GlobalVariables.hospitalNO;
  503. Jo.fixmedins_name = GlobalVariables.hospitalName;
  504. Jo.sign_no = GlobalVariables.signno;
  505. JObject joInput = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(input);
  506. Jo.Add("input", JObject.FromObject(joInput));
  507. return Jo.ToString();
  508. }
  509. }
  510. }