JsonHelper.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /******************************************************************************
  2. * 文件名称: JsonHelper.cs
  3. * 文件说明: Json业务的封装,处理JSON串的封装,解析等
  4. * 当前版本: V1.0
  5. * 创建日期: 2022-04-14
  6. *
  7. * 2020-04-14: 增加 setIrisInpar 方法
  8. * 2020-04-14: 增加 setIrisInpar(重载) 方法
  9. * 2020-04-14: 增加 getIrisExceptionJson 方法
  10. * 2020-04-14: 增加 parseBusinessJson 方法
  11. ******************************************************************************/
  12. using Newtonsoft.Json.Linq;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using PTMedicalInsurance.Common;
  19. using System.IO;
  20. using PTMedicalInsurance.Variables;
  21. using Newtonsoft.Json;
  22. using GMCrypto.Lib;
  23. namespace PTMedicalInsurance.Helper
  24. {
  25. public class JsonHelper
  26. {
  27. static EncryptHelper encrypt = new EncryptHelper();
  28. public static string toJsonString(object obj,bool missingNull)
  29. {
  30. JsonSerializerSettings settings = new JsonSerializerSettings();
  31. if (missingNull)
  32. {
  33. settings.NullValueHandling = NullValueHandling.Ignore;
  34. }
  35. return JsonConvert.SerializeObject(obj, Formatting.Indented, settings);
  36. }
  37. public static string toJsonString(object obj)
  38. {
  39. return toJsonString(obj,true);
  40. }
  41. public static JObject toJObject(object obj)
  42. {
  43. string data = toJsonString(obj, true);
  44. return JObject.Parse(data);
  45. }
  46. public static object toObject(string json)
  47. {
  48. return JsonConvert.DeserializeObject(json);
  49. }
  50. public static T toObject<T>(JObject joObject)
  51. {
  52. return joObject.ToObject<T>();
  53. }
  54. /// <summary>
  55. /// 解析医保平台返回对象
  56. /// </summary>
  57. /// <typeparam name="T"></typeparam>
  58. /// <param name="response"></param>
  59. /// <returns></returns>
  60. public static T getOutput<T>(JObject response)
  61. {
  62. if (response.ContainsKey("output"))
  63. {
  64. return response["output"].ToObject<T>();
  65. }
  66. return response.ToObject<T>();
  67. }
  68. /// <summary>
  69. /// 解析IRIS系统返回对象
  70. /// </summary>
  71. /// <typeparam name="T"></typeparam>
  72. /// <param name="response"></param>
  73. /// <returns></returns>
  74. public static T getResult<T>(JObject response)
  75. {
  76. Response resp = response.ToObject<Response>();
  77. if (resp.errorCode == "0")
  78. {
  79. if (resp.result?.GetType() == typeof(JObject))
  80. {
  81. JObject ret = (JObject)resp.result;
  82. JArray lists = ret.Value<JArray>("rows");
  83. if (lists?.Count > 0)
  84. {
  85. return lists.ToObject<T>();
  86. }
  87. else {
  88. return ret.ToObject<T>();
  89. }
  90. }
  91. if (resp.result?.GetType() == typeof(JArray))
  92. {
  93. JArray lists = (JArray)resp.result;
  94. if (lists?.Count > 0)
  95. {
  96. return lists[0].ToObject<T>();
  97. }
  98. }
  99. }
  100. return default;
  101. }
  102. /// <summary>
  103. /// 根据JSonPath查找节点值,如果节点不存在则返回空值
  104. /// </summary>
  105. /// <param name="jo"></param>
  106. /// <param name="jsonPath"></param>
  107. /// <returns></returns>
  108. public static string getDestValue(JObject jo, string destPath)
  109. {
  110. JToken jt = jo.SelectToken("$." + destPath);
  111. if (jt != null)
  112. {
  113. return jt.ToString();
  114. }
  115. else
  116. {
  117. Global.writeLog(destPath + "的JToken属性值为空");
  118. return "";
  119. }
  120. }
  121. /// <summary>
  122. /// 将指定的json内容转为对象
  123. /// </summary>
  124. /// <typeparam name="T"></typeparam>
  125. /// <param name="jo"></param>
  126. /// <param name="destPath"></param>
  127. /// <returns></returns>
  128. public static T parseObject<T>(JObject jo, string destPath)
  129. {
  130. JToken jt = jo.SelectToken("$." + destPath);
  131. if (jt != null)
  132. {
  133. return jt.ToObject<T>();
  134. }
  135. else
  136. {
  137. Global.writeLog(destPath + "的JToken属性值为空");
  138. return default;
  139. }
  140. }
  141. public static string getDestProperty(JObject jo, string propertyName)
  142. {
  143. JProperty jp = jo.Property(propertyName);
  144. if (jp != null)
  145. {
  146. string jpStr = jp.ToString();
  147. string rtnResult = "{ " + jpStr + "}";
  148. return rtnResult;
  149. }
  150. else
  151. {
  152. Global.writeLog(propertyName + "的JProperty属性值为空");
  153. return "";
  154. }
  155. }
  156. /// <summary>
  157. /// 压缩JSON,占用体积减小(重载)
  158. /// </summary>
  159. /// <param name="json"></param>
  160. /// <returns></returns>
  161. public static string Compress(string json)
  162. {
  163. StringBuilder sb = new StringBuilder();
  164. using (StringReader reader = new StringReader(json))
  165. {
  166. int ch = -1;
  167. int lastch = -1;
  168. bool isQuoteStart = false;
  169. while ((ch = reader.Read()) > -1)
  170. {
  171. if ((char) lastch != '\\' && (char) ch == '\"')
  172. {
  173. if (!isQuoteStart)
  174. {
  175. isQuoteStart = true;
  176. }
  177. else
  178. {
  179. isQuoteStart = false;
  180. }
  181. }
  182. if (!Char.IsWhiteSpace((char) ch) || isQuoteStart)
  183. {
  184. sb.Append((char) ch);
  185. }
  186. lastch = ch;
  187. }
  188. }
  189. return sb.ToString();
  190. }
  191. public static string Compress(JObject jsonObj)
  192. {
  193. //https://blog.csdn.net/yangjiaosun/article/details/103717256
  194. return Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj);
  195. }
  196. /// <summary>
  197. /// 组织异常JSON串
  198. /// </summary>
  199. /// <param name="errorCode"></param>
  200. /// <param name="errorMessage"></param>
  201. /// <returns></returns>
  202. public static JObject setExceptionJson(int errorCode, string eventName, string errorMessage)
  203. {
  204. dynamic joRtn = new JObject();
  205. joRtn.errorCode = errorCode;
  206. joRtn.errorMessage = eventName + "提示:" + errorMessage;
  207. return joRtn;
  208. }
  209. /// <summary>
  210. /// 组织中心入参(明文)。部分代码是需要解析明文入参的,为了方便增加该方法
  211. /// </summary>
  212. /// <param name="infno"></param>
  213. /// <param name="input"></param>
  214. /// <returns></returns>
  215. public static string setCenterInpar_plain(string infno, string input)
  216. {
  217. return setCenterInpar_plain(infno, (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(input));
  218. }
  219. /// <summary>
  220. /// 组织中心入参(明文)。部分代码是需要解析明文入参的,为了方便增加该方法
  221. /// </summary>
  222. /// <param name="infno"></param>
  223. /// <param name="input"></param>
  224. /// <returns></returns>
  225. public static string setCenterInpar_plain(string infno, JObject joInput)
  226. {
  227. return setCenterInpar(infno, joInput);
  228. }
  229. /// <summary>
  230. /// 移动支付组织中心入参
  231. /// </summary>
  232. /// <param name="infno"></param>
  233. /// <param name="input"></param>
  234. /// <returns></returns>
  235. public static string setMPCenterInpar(string infno, JObject joInput)
  236. {
  237. return setMPCenterInpar(infno, joInput.ToString());
  238. }
  239. /// <summary>
  240. /// 移动支付组织中心入参
  241. /// </summary>
  242. /// <param name="infno"></param>
  243. /// <param name="input"></param>
  244. /// <returns></returns>
  245. public static string setMPCenterInpar(string infno, string txtData)
  246. {
  247. EncryptHelper encrypt = new EncryptHelper();
  248. string plainText = SignUtil.SortInput(JObject.Parse(txtData));
  249. Global.writeLog(infno + "【明文入参】:\r\n" + plainText);
  250. string signData = "";
  251. string output = encrypt.encrypt(txtData, ref signData);
  252. Global.writeLog(infno + "【密文入参】:\r\n" + output);
  253. return output;
  254. }
  255. public static string setCenterInpar(string infno, JObject joInput)
  256. {
  257. dynamic Jo = new JObject();
  258. Jo.infno = infno;
  259. Global.curEvt.msgid = Global.inf.hospitalNO + DateTime.Now.ToString("yyyyMMddHHmmssffff");
  260. Jo.msgid = Global.curEvt.msgid;
  261. Global.pat.mdtrtarea_admvs = Global.inf.areaCode;
  262. // 参保地医保区划
  263. Jo.insuplc_admdvs = Global.pat.insuplc_admdvs??"";
  264. // 就医地
  265. Jo.mdtrtarea_admvs = Global.pat.mdtrtarea_admvs;
  266. Jo.recer_sys_code = "SY";
  267. Jo.dev_no = "00FFF254308F";//设备编号
  268. Jo.dev_safe_info = "";
  269. Jo.signtype = "SM3"; ;
  270. Jo.infver = "v1.0";
  271. Jo.opter_type = Global.user.type; ;
  272. Jo.opter = Global.user.ID; ;
  273. Jo.opter_name = Global.user.name; ;
  274. Jo.inf_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  275. Jo.fixmedins_code = Global.inf.hospitalNO;
  276. Jo.fixmedins_name = Global.inf.hospitalName;
  277. Jo.sign_no = Global.curEvt.signno;
  278. Jo.cainfo = "";
  279. //Jo.cainfo = "0416509a2c98da94f3840cfb45c6119365a0e166f608d7d1002d70f377ceb46cb0f0f483a20df1eb75e96e8498b9184106d9d8361bf37550ae8d228d6ad3e7127eb0de8868606f18f7a840f238200ab201cd5f10ec4f1490529d9e4c599f2a78dd1917287003ada3699fcc1046e493db974572386f898f3c9c96c5e0ea348e7dc0";
  280. if (joInput != null)
  281. {
  282. Jo.Add("input", joInput);
  283. }
  284. else
  285. {
  286. Jo.Add("input", "");
  287. }
  288. var request = Utils.Wrapper(Jo);
  289. return JsonHelper.toJsonString(request);
  290. }
  291. /// <summary>
  292. /// 组织Iris入参
  293. /// </summary>
  294. /// <param name="code"></param>
  295. /// <param name="joParam"></param>
  296. /// <returns></returns>
  297. public static JObject setIrisInpar(string code, JObject joParam)
  298. {
  299. try
  300. {
  301. dynamic joInparam = new JObject();
  302. joInparam.code = code;
  303. dynamic joTmp = new JObject();
  304. JArray jaParam = new JArray();
  305. // 去掉包装
  306. JObject joInput = Utils.removeWrapper(joParam);
  307. jaParam.Add(joInput);
  308. joInparam.Add("params", JArray.FromObject(jaParam));
  309. joInparam.Add("session", Global.curEvt.jaSession);
  310. return joInparam;
  311. }
  312. catch (Exception ex)
  313. {
  314. return setExceptionJson(-1, "setIrisInpar:", ex.Message);
  315. }
  316. }
  317. /// <summary>
  318. /// 组织Iris入参
  319. /// </summary>
  320. /// <param name="code"></param>
  321. /// <param name="joParam"></param>
  322. /// <returns></returns>
  323. public static JObject setIrisInpar(string code, JArray jaParams)
  324. {
  325. try
  326. {
  327. dynamic joInparam = new JObject();
  328. joInparam.code = code;
  329. dynamic joTmp = new JObject();
  330. joInparam.Add("params", jaParams);
  331. joInparam.Add("session", Global.curEvt.jaSession);
  332. return joInparam;
  333. }
  334. catch (Exception ex)
  335. {
  336. return setExceptionJson(-1, "setIrisInpar:", ex.Message);
  337. }
  338. }
  339. /// <summary>
  340. /// 解析中心返参
  341. /// </summary>
  342. /// <param name="joRtn"></param>
  343. /// <param name="errorMsg"></param>
  344. /// <returns></returns>
  345. public static int parseCenterRtnValue(JObject joRtn, out string errorMsg)
  346. {
  347. try
  348. {
  349. string errcode = getDestValue(joRtn, "errcode");
  350. string errorCode = getDestValue(joRtn, "errorCode");
  351. string resultCode = getDestValue(joRtn, "resultcode");
  352. if (!string.IsNullOrEmpty(errcode))
  353. {
  354. errorMsg = getDestValue(joRtn, "errmsg");
  355. return -1;
  356. }
  357. else if (!string.IsNullOrEmpty(errorCode))
  358. {
  359. errorMsg = getDestValue(joRtn, "errorMessage");
  360. return -2;
  361. }
  362. else if (!string.IsNullOrEmpty(resultCode))
  363. {
  364. errorMsg = getDestValue(joRtn, "resulttext");
  365. return -3;
  366. }
  367. else
  368. {
  369. errorMsg = getDestValue(joRtn, "err_msg");
  370. return int.Parse(getDestValue(joRtn, "infcode"));
  371. }
  372. }
  373. catch (Exception ex)
  374. {
  375. errorMsg = "解析中心返参发生异常:" + ex.Message;
  376. return -1;
  377. }
  378. }
  379. /// <summary>
  380. /// 解析移动返参
  381. /// </summary>
  382. /// <param name="joRtn"></param>
  383. /// <param name="errorMsg"></param>
  384. /// <returns></returns>
  385. public static int parseMPRtnValue(JObject joRtn, out string errorMsg)
  386. {
  387. try
  388. {
  389. string success = getDestValue(joRtn, "success");
  390. string message = getDestValue(joRtn, "message");
  391. string encData = getDestValue(joRtn, "encData");
  392. if (string.IsNullOrEmpty(message)) {
  393. message = getDestValue(joRtn, "errorMessage");
  394. }
  395. //Global.writeLog("success:" + success);
  396. if (success != "True")
  397. {
  398. errorMsg = message;
  399. return -1;
  400. }
  401. else
  402. {
  403. errorMsg = joRtn.ToString();
  404. return 0;
  405. }
  406. }
  407. catch (Exception ex)
  408. {
  409. errorMsg = "解析中心返参发生异常:" + ex.Message;
  410. return -1;
  411. }
  412. }
  413. /// <summary>
  414. /// 组织IRIS返参
  415. /// </summary>
  416. /// <param name="errorCode"></param>
  417. /// <param name="errorMessage"></param>
  418. /// <param name="joResult"></param>
  419. /// <returns></returns>
  420. public static JObject setIrisReturnValue(int errorCode, string errorMessage, JObject joResult)
  421. {
  422. try
  423. {
  424. dynamic joRtn = new JObject();
  425. joRtn.errorCode = errorCode;
  426. joRtn.errorMessage = errorMessage;
  427. joRtn.Add("result", joResult);
  428. return joRtn;
  429. }
  430. catch (Exception ex)
  431. {
  432. return setExceptionJson(-1, "setIrisReturnValue:", ex.Message);
  433. }
  434. }
  435. /// <summary>
  436. /// 解析IRIS返参
  437. /// </summary>
  438. /// <param name="joRtn"></param>
  439. /// <param name="errorMsg"></param>
  440. /// <returns></returns>
  441. ///
  442. public static int parseIrisRtnValue(JObject joRtn, out string errorMsg)
  443. {
  444. try
  445. {
  446. errorMsg = getDestValue(joRtn, "errorMessage");
  447. return int.Parse(getDestValue(joRtn, "errorCode"));
  448. }
  449. catch (Exception ex)
  450. {
  451. errorMsg = "解析Iris返参发生异常:" + ex.Message;
  452. return -1;
  453. }
  454. }
  455. /// <summary>
  456. /// 解析业务JSON串(业务串固定格式为{errorCode = ,errorMessage = ,result ={}})
  457. /// </summary>
  458. /// <param name="json"></param>
  459. /// <param name="errorMsg"></param>
  460. /// <returns></returns>
  461. public static int parseCenterReturnJson(JObject json, out string errorMsg)
  462. {
  463. try
  464. {
  465. errorMsg = "";
  466. if (int.Parse(json["infcode"].ToString()) != 0)
  467. {
  468. errorMsg = json["err_msg"].ToString();
  469. }
  470. return int.Parse(json["infcode"].ToString());
  471. }
  472. catch (Exception ex)
  473. {
  474. errorMsg = "parseCenterReturnJson" + ex.Message;
  475. return -1;
  476. }
  477. }
  478. /// <summary>
  479. /// 组织中心入参-医保电子处方流转
  480. /// </summary>
  481. /// <param name="infno"></param>
  482. /// <param name="input"></param>
  483. /// <returns></returns>
  484. public static string setCenterInparPresCirNew(JObject data)
  485. {
  486. EncryptHelper encrypt = new EncryptHelper(Global.inf.appId, Global.inf.secretKey, Global.inf.publicKey, Global.inf.privateKey);
  487. string txtData = JsonHelper.toJsonString(data);
  488. string plainText = SignUtil.SortInput(data);
  489. Global.writeLog("【明文入参】:\r\n" + plainText);
  490. string signData = "";
  491. string output = encrypt.encrypt(txtData, ref signData);
  492. Global.writeLog("【密文入参】:\r\n" + output);
  493. return output;
  494. }
  495. /// <summary>
  496. /// 解析中心返参
  497. /// </summary>
  498. /// <param name="joRtn"></param>
  499. /// <param name="errorMsg"></param>
  500. /// <returns></returns>
  501. public static int parseEPCenterRtnValue(JObject joRtn, out string errorMsg)
  502. {
  503. try
  504. {
  505. string errcode = getDestValue(joRtn, "errcode");
  506. string errorCode = getDestValue(joRtn, "errorCode");
  507. if (errcode != "")
  508. {
  509. errorMsg = getDestValue(joRtn, "message");
  510. return -1;
  511. }
  512. else if (errorCode != "")
  513. {
  514. errorMsg = getDestValue(joRtn, "errorMessage");
  515. return -2;
  516. }
  517. else
  518. {
  519. errorMsg = getDestValue(joRtn, "message");
  520. return int.Parse(getDestValue(joRtn, "code"));
  521. }
  522. }
  523. catch (Exception ex)
  524. {
  525. errorMsg = "解析中心返参发生异常:" + ex.Message;
  526. return -1;
  527. }
  528. }
  529. }
  530. }