/******************************************************************************
  * 文件名称: JsonHelper.cs
 * 文件说明: Json业务的封装，处理JSON串的封装，解析等
 * 当前版本: V1.0
 * 创建日期: 2022-04-14
 *
 * 2020-04-14: 增加 setIrisInpar 方法
 * 2020-04-14: 增加 setIrisInpar（重载） 方法
 * 2020-04-14: 增加 getIrisExceptionJson 方法
 * 2020-04-14: 增加 parseBusinessJson 方法

 ******************************************************************************/
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PTMedicalInsurance.Common;
using System.IO;
using PTMedicalInsurance.Variables;
using System.Windows.Forms;
using SM2Crypto.Lib;

namespace PTMedicalInsurance.Helper
{
    class JsonHelper
    {
        public static string toJsonString(object obj, bool missingNull)
        {
            JsonSerializerSettings settings = new JsonSerializerSettings();
            if (missingNull)
            {
                settings.NullValueHandling = NullValueHandling.Ignore;
            }
            return JsonConvert.SerializeObject(obj, Formatting.Indented, settings);
        }

        public static string toJsonString(object obj)
        {
            return toJsonString(obj, true);
        }

        /// <summary>
        /// 根据JSonPath查找节点值，如果节点不存在则返回空值
        /// </summary>
        /// <param name="jo"></param>
        /// <param name="jsonPath"></param>
        /// <returns></returns>
        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 "";
            }
        }

        /// <summary>
        /// 解析医保平台返回对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="response"></param>
        /// <returns></returns>
        public static T getOutput<T>(JObject response)
        {
            if (response.ContainsKey("output"))
            {
                return response["output"].ToObject<T>();
            }

            return response.ToObject<T>();
        }

        public static string getDestProperty(JObject jo, string propertyName)
        {
            JProperty jp = jo.Property(propertyName);
            if (jp != null)
            {
                string jpStr = jp.ToString();
                string rtnResult = "{ " + jpStr + "}";
                return rtnResult;
            }
            else
            {
                Global.writeLog(propertyName + "的JProperty属性值为空");
                return "";
            }

        }
        /// <summary>
        /// 压缩JSON，占用体积减小(重载)
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static string Compress(string json)
        {
            StringBuilder sb = new StringBuilder();
            using (StringReader reader = new StringReader(json))
            {
                int ch = -1;
                int lastch = -1;
                bool isQuoteStart = false;
                while ((ch = reader.Read()) > -1)
                {
                    if ((char)lastch != '\\' && (char)ch == '\"')
                    {
                        if (!isQuoteStart)
                        {
                            isQuoteStart = true;
                        }
                        else
                        {
                            isQuoteStart = false;
                        }
                    }
                    if (!Char.IsWhiteSpace((char)ch) || isQuoteStart)
                    {
                        sb.Append((char)ch);
                    }
                    lastch = ch;
                }
            }
            return sb.ToString();
        }
        public static string Compress(JObject jsonObj)
        {
            //https://blog.csdn.net/yangjiaosun/article/details/103717256
            return Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj);
        }

        /// <summary>
        /// 组织异常JSON串
        /// </summary>
        /// <param name="errorCode"></param>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        public static JObject setExceptionJson(int errorCode, string eventName, string errorMessage)
        {
            dynamic joRtn = new JObject();
            joRtn.errorCode = errorCode;
            joRtn.errorMessage = eventName + "提示：" + errorMessage;
            return joRtn;
        }

        /// <summary>
        /// 组织中心入参(明文)。部分代码是需要解析明文入参的，为了方便增加该方法
        /// </summary>
        /// <param name="infno"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string setCenterInpar_plain(string infno, string input)
        {
            dynamic Jo = new JObject();
            Jo.infno = infno;
            Global.curEvt.msgid = Global.inf.hospitalNO + DateTime.Now.ToString("yyyyMMddHHmmssffff");
            Jo.msgid = Global.curEvt.msgid;

            Jo.mdtrtarea_admvs = "620100";
            Global.pat.mdtrtarea_admvs = "620100";

            Jo.insuplc_admdvs = Global.pat.insuplc_admdvs;
            Jo.recer_sys_code = Global.inf.recivedSystem;
            Jo.dev_safe_info = Global.inf.dev_safe_info; //设备编号
            Jo.cainfo = Global.inf.cainfo;//设备安全信息
            Jo.signtype = Global.inf.signatureType; ;
            Jo.infver = Global.inf.version; ;
            Jo.opter_type = Global.user.type; ;
            Jo.opter = Global.user.ID; ;
            Jo.opter_name = Global.user.name; ;
            Jo.inf_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            Jo.fixmedins_code = Global.inf.hospitalNO;
            Jo.fixmedins_name = Global.inf.hospitalName;
            Jo.sign_no = Global.curEvt.signno;

            //测试环境
            //Global.inf.privateKey = "2af5cca6-de33-461d-a086-6423b92abbd6";
            //Global.inf.Secret = "d82e88f67505b976a5781b725e9eed6da9b43601f21e7daae730a0d72c724126";
            //Global.inf.centerURL = "http://10.126.40.47/tst/hsa/hgs/";
            //Global.inf.cardURL = "http://10.126.105.66/hsa-hgs-adapt/api/card/initDll";   //测试环境

            //正式环境
            Global.inf.privateKey = "0d2caf81-3c1b-4a7f-8955-d807eee0720e";
            Global.inf.Secret = "dc80fc02f4feac82bb0c4a6e778292c0c33e8e0389a41f5c3865963b569841ec";
            Global.inf.cardURL = "http://10.126.34.69/hsa-hgs-adapt/api/card/initDll";

            JObject joInput = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(input);
            Jo.Add("input", JObject.FromObject(joInput));
            return Jo.ToString();
        }

        /// <summary>
        /// 组织中心入参(明文)。部分代码是需要解析明文入参的，为了方便增加该方法
        /// </summary>
        /// <param name="infno"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string setCenterInpar_plain(string infno, JObject joInput)
        {
            dynamic Jo = new JObject();
            Jo.infno = infno;
            Global.curEvt.msgid = Global.inf.hospitalNO + DateTime.Now.ToString("yyyyMMddHHmmssffff");
            Jo.msgid = Global.curEvt.msgid;

            Jo.insuplc_admdvs = Global.pat.insuplc_admdvs;
            Jo.mdtrtarea_admvs = "620100";
            Global.pat.mdtrtarea_admvs = "620100";
            Jo.recer_sys_code = Global.inf.recivedSystem;
            Jo.dev_no = Global.inf.dev_no;
            Jo.dev_safe_info = Global.inf.dev_safe_info; //设备编号
            Jo.cainfo = Global.inf.cainfo;//设备安全信息
            Jo.signtype = Global.inf.signatureType; ;
            Jo.infver = Global.inf.version; ;
            Jo.opter_type = Global.user.type; ;
            Jo.opter = Global.user.ID; ;
            Jo.opter_name = Global.user.name; ;
            Jo.inf_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            Jo.fixmedins_code = Global.inf.hospitalNO;
            Jo.fixmedins_name = Global.inf.hospitalName;
            Jo.sign_no = Global.curEvt.signno;

            //测试环境
            //Global.inf.privateKey = "2af5cca6-de33-461d-a086-6423b92abbd6";
            //Global.inf.Secret = "d82e88f67505b976a5781b725e9eed6da9b43601f21e7daae730a0d72c724126";
            //Global.inf.centerURL = "http://10.126.40.47/tst/hsa/hgs/";
            //Global.inf.cardURL = "http://10.126.105.66/hsa-hgs-adapt/api/card/initDll";   //测试环境

            //正式环境
            Global.inf.privateKey = "0d2caf81-3c1b-4a7f-8955-d807eee0720e";
            Global.inf.Secret = "dc80fc02f4feac82bb0c4a6e778292c0c33e8e0389a41f5c3865963b569841ec";
            Global.inf.cardURL = "http://10.126.34.69/hsa-hgs-adapt/api/card/initDll";

            Jo.Add("input", joInput);
            return Jo.ToString();
        }

        /// <summary>
        /// 组织中心入参--签到
        /// </summary>
        /// <param name="infno"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string setCenterInpar(string infno, string input)
        {
            dynamic Jo = new JObject();
            Jo.infno = infno;
            Global.curEvt.msgid = Global.inf.hospitalNO + DateTime.Now.ToString("yyyyMMddHHmmssffff");
            Jo.msgid = Global.curEvt.msgid;

            if (Global.inf.areaCode == "")
            {
                Global.inf.areaCode = "620100";
            }

            if (Global.pat.insuplc_admdvs == "")
            {
                Global.pat.insuplc_admdvs = Global.inf.areaCode;
            }

            Jo.insuplc_admdvs = Global.pat.insuplc_admdvs;
            Jo.mdtrtarea_admvs = "620100";
            Global.pat.mdtrtarea_admvs = "620100";
            Jo.recer_sys_code = "GanSu";
            Jo.dev_no = Global.curEvt.mac;
            Jo.dev_safe_info = Global.inf.dev_safe_info; //设备编号
            Jo.cainfo = Global.inf.cainfo;
            Jo.signtype = "SM3"; ;
            Jo.infver = "v1.0";
            Jo.opter_type = Global.user.type; ;
            Jo.opter = Global.user.ID; ;
            Jo.opter_name = Global.user.name; ;
            Jo.inf_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            Jo.fixmedins_code = Global.inf.hospitalNO;
            Jo.fixmedins_name = Global.inf.hospitalName;
            Jo.sign_no = Global.curEvt.signno;

            //测试环境
            //Global.inf.privateKey = "2af5cca6-de33-461d-a086-6423b92abbd6";
            //Global.inf.Secret = "d82e88f67505b976a5781b725e9eed6da9b43601f21e7daae730a0d72c724126";
            //Global.inf.centerURL = "http://10.126.40.47/tst/hsa/hgs/";
            //Global.inf.cardURL = "http://10.126.105.66/hsa-hgs-adapt/api/card/initDll";   //测试环境

            //正式环境
            Global.inf.privateKey = "0d2caf81-3c1b-4a7f-8955-d807eee0720e";
            Global.inf.Secret = "dc80fc02f4feac82bb0c4a6e778292c0c33e8e0389a41f5c3865963b569841ec";
            Global.inf.cardURL = "http://10.126.34.69/hsa-hgs-adapt/api/card/initDll";

            if (input != "")
            {
                JObject joInput = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(input);
                Jo.cainfo = "";
                Jo.Add("input", JObject.FromObject(joInput));
            }
            else
            {
                Jo.cainfo = "";
                Jo.Add("input", "");
            }

            return Jo.ToString();
        }

        /// <summary>
        /// 组织中心入参
        /// </summary>
        /// <param name="infno"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string setCenterInpar(string infno, JObject joInput)
        {
            dynamic Jo = new JObject();
            Jo.infno = infno;
            Global.curEvt.msgid = Global.inf.hospitalNO + DateTime.Now.ToString("yyyyMMddHHmmssffff");
            Jo.msgid = Global.curEvt.msgid;

            if (Global.pat.insuplc_admdvs == "")
            {
                Global.pat.insuplc_admdvs = Global.inf.areaCode;
            }
            Jo.insuplc_admdvs = Global.pat.insuplc_admdvs;
            Jo.mdtrtarea_admvs = "620100";   //郑州 市医保、省内异地、跨省异地就医地都传419900
            Global.pat.mdtrtarea_admvs = "620100";
            Jo.recer_sys_code = "GanSu";
            Jo.dev_no = Global.curEvt.mac;
            Jo.dev_safe_info = Global.inf.dev_safe_info; //设备编号
            Jo.cainfo = Global.inf.cainfo;//
            Jo.signtype = "asn1"; ;
            Jo.infver = "v1.0";
            Jo.opter_type = Global.user.type; ;
            Jo.opter = Global.user.ID;
            Jo.opter_name = Global.user.name; ;
            Jo.inf_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            Jo.fixmedins_code = Global.inf.hospitalNO;
            Jo.fixmedins_name = Global.inf.hospitalName;
            Jo.sign_no = Global.curEvt.signno;
            Jo.cainfo = "";
            Jo.Add("input", JObject.FromObject(joInput));

            //测试环境
            //Global.inf.privateKey = "2af5cca6-de33-461d-a086-6423b92abbd6";
            //Global.inf.Secret = "d82e88f67505b976a5781b725e9eed6da9b43601f21e7daae730a0d72c724126";
            //Global.inf.centerURL = "http://10.126.40.47/tst/hsa/hgs/";
            //Global.inf.cardURL = "http://10.126.105.66/hsa-hgs-adapt/api/card/initDll";   //测试环境

            //正式环境
            Global.inf.privateKey = "0d2caf81-3c1b-4a7f-8955-d807eee0720e";
            Global.inf.Secret = "dc80fc02f4feac82bb0c4a6e778292c0c33e8e0389a41f5c3865963b569841ec";
            Global.inf.cardURL = "http://10.126.34.69/hsa-hgs-adapt/api/card/initDll";

            return Jo.ToString();
        }

        /// <summary>
        /// 组织中心入参-医保电子处方流转
        /// </summary>
        /// <param name="infno"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string setCenterInparPresCirNew(JObject data)
        {
            #region【测试环境】
            /*
            //应用ID
            Global.inf.appId = "6582E8493AFE477BB7C6F3A46B5CEB1A"; 
            //Global.inf.appId = "57E2D561E97141A3871DEFB410ADD920"; 
            //应用秘钥
            Global.inf.Secret = "AD3731D4509A4186A177B8701DB4069F";
            //Global.inf.Secret = "F30D0D261BA04063A0BCAEF19F8ADBCC";
            //应用私钥
            Global.inf.privateKey = "ANxsfa0qU4Oc4WBG/oA41Z4+goGfJW/ovzz47VQcunKd";    //中心邮件下发 
            //Global.inf.privateKey = "AKyhfvPbT+tIJHJkZSDILrw7t+FOG1U58UcupYYTzCOj";  //中心Demo
            //应用公钥
            //Global.inf.publicKey = "BKRejz4MwM8G8Krc4hyIIvhphvwrQpnHIsGWTd0sn0TJYr2eKJGAv87AKnrT8pL50H6hdVHdU0FFaL18GApDEbU"; //中心邮件下发
            Global.inf.publicKey = "BIFd2+2CgjuPAj5FMj5L/L3azTWu86suPtlIJkCo8zjQ44R7SQUUkTgZGdVelRQCM5pW+x9tZGzDPaUNbfD499w=";  //中心Demo
           
            Global.inf.cardURL = "http://10.126.105.66/hsa-hgs-adapt/api/card/initDll";   //测试环境
            Global.inf.presCenterURL = "http://10.126.40.12:8080/epc/api";
             */
            #endregion

            #region【正式环境】
            if (Global.inf.interfaceDr == 21) //兰2
            {
                //应用ID
                Global.inf.appId = "4D967CAB5DA44E3583F4246D7FE897F0";
                //应用秘钥
                Global.inf.Secret = "586BD24D03E5481C8AE4FE9DD8424FFE";
                //应用私钥
                Global.inf.privateKey = "XTbX+Tt1np5Fma0+twhYi+vtM4J0UJ8VX5fFEZ7t/ro=";
                //应用公钥            
                Global.inf.publicKey = "BIFd2+2CgjuPAj5FMj5L/L3azTWu86suPtlIJkCo8zjQ44R7SQUUkTgZGdVelRQCM5pW+x9tZGzDPaUNbfD499w=";
            }
            else if (Global.inf.interfaceDr == 25) //兰1
            {
                //应用ID
                Global.inf.appId = "6C9B786AF7E848B29B0EF5783D630263";
                //应用秘钥
                Global.inf.Secret = "6055AE5A9CAD4D9BA8A98EB20431364E";
                //应用私钥
                Global.inf.privateKey = "AKReZmOYcTOdbrnlZZFIXn8LQSHPzdAK1Q0CXDnE4Jhk";
                //应用公钥            
                Global.inf.publicKey = "BIFd2+2CgjuPAj5FMj5L/L3azTWu86suPtlIJkCo8zjQ44R7SQUUkTgZGdVelRQCM5pW+x9tZGzDPaUNbfD499w=";
            }

            Global.inf.presCenterURL = "http://10.126.34.36:8080/epc/api";
            #endregion

            Global.writeLog("密钥id：" + Global.inf.appId);
            Global.writeLog("密钥key：" + Global.inf.Secret);
            Global.writeLog("定点医药机构私钥：" + Global.inf.privateKey);
            Global.writeLog("医保电子处方中心公钥：" + Global.inf.publicKey);

            //Global.curEvt.msgid = Global.inf.hospitalNO + DateTime.Now.ToString("yyyyMMddHHmmssffff");
            string timestamp = ((long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds).ToString();
            Global.curEvt.msgid = timestamp;

            JObject requestData = new JObject();
            requestData.Add("appId", Global.inf.appId);
            //requestData.Add("version", "2.0.1");
            requestData.Add("encType", "SM4");
            requestData.Add("signType", "SM2");
            requestData.Add("timestamp", timestamp);

            //加密
            String encData = SMUtil.encrypt(JsonConvert.SerializeObject(data, Formatting.None), Global.inf.appId, Global.inf.Secret);
            //Global.writeLog("入参加密前：" + "\r\n" + JsonConvert.DeserializeObject(data.ToString()) + "\r\n" + "入参加密后：" + "\r\n" + encData);

            JObject signDto = (JObject)JObject.Parse(JsonConvert.SerializeObject(requestData, Formatting.None));
            signDto.Add("data", data);

            //加签
            String signData = SMUtil.sign(signDto, Global.inf.Secret, Global.inf.privateKey);
            Global.writeLog("签名结果：" + "\r\n" + signData);

            //报文
            requestData.Add("encData", encData);
            requestData.Add("signData", signData);

            dynamic Jo = new JObject();
            Jo.appId = Global.inf.appId;
            //Jo.version = "2.0.1";
            Jo.timestamp = timestamp; 
            Jo.encType = "SM4";
            Jo.signType = "SM2";
            Jo.signData = signDto.ToString();
            Jo.encData = data.ToString();

            Global.writeLog("加密前报文：" + "\r\n" + Jo.ToString() + "\r\n" + "加密后报文：" + "\r\n" + JsonConvert.SerializeObject(requestData, Formatting.None));

            //本地解密
            //String decData = SMUtil.decrypt(encData, Global.inf.appId, Global.inf.Secret);
            //Global.writeLog("解密：" + decData);
            //使用应用公钥（publicKey示例默认值）对本地自签名的数据进行验签示例
            //Boolean isVerify = SMUtil.verify(signDto, Global.inf.Secret, Global.inf.publicKey, signData);
            //Global.writeLog("验签：" + isVerify);

            return requestData.ToString();
        }

        /// <summary>
        /// 组织Iris入参
        /// </summary>
        /// <param name="code"></param>
        /// <param name="joParam"></param>
        /// <returns></returns>
        public static JObject setIrisInpar(string code, JObject joParam)
        {
            try
            {
                dynamic joInparam = new JObject();
                joInparam.code = code;
                dynamic joTmp = new JObject();
                JArray jaParam = new JArray();
                jaParam.Add(joParam);
                joInparam.Add("params", JArray.FromObject(jaParam));
                joInparam.Add("session", Global.curEvt.jaSession);
                return joInparam;
            }
            catch (Exception ex)
            {
                return setExceptionJson(-1, "setIrisInpar:", ex.Message);
            }

        }

        /// <summary>
        /// 组织Iris入参
        /// </summary>
        /// <param name="code"></param>
        /// <param name="joParam"></param>
        /// <returns></returns>
        public static JObject setIrisInpar(string code, JArray jaParams)
        {
            try
            {
                dynamic joInparam = new JObject();
                joInparam.code = code;
                dynamic joTmp = new JObject();
                joInparam.Add("params", jaParams);
                joInparam.Add("session", Global.curEvt.jaSession);
                return joInparam;
            }
            catch (Exception ex)
            {
                return setExceptionJson(-1, "setIrisInpar:", ex.Message);
            }

        }

        /// <summary>
        /// 解析中心返参
        /// </summary>
        /// <param name="joRtn"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public static int parseCenterRtnValuePresCir(JObject joRtn, out string errorMsg)
        {
            try
            {
                string errcode = getDestValue(joRtn, "errcode");
                string errorCode = getDestValue(joRtn, "errorCode");
                if (errcode != "")
                {
                    errorMsg = getDestValue(joRtn, "message");
                    return -1;
                }
                else if (errorCode != "")
                {
                    errorMsg = getDestValue(joRtn, "errorMessage");
                    return -2;
                }
                else
                {
                    errorMsg = getDestValue(joRtn, "message");
                    return int.Parse(getDestValue(joRtn, "code"));
                }
            }
            catch (Exception ex)
            {
                errorMsg = "解析中心返参发生异常：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 解析中心返参
        /// </summary>
        /// <param name="joRtn"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public static int parseCenterRtnValue(JObject joRtn, out string errorMsg)
        {
            try
            {
                string errcode = getDestValue(joRtn, "errcode");
                string errorCode = getDestValue(joRtn, "errorCode");
                if (errcode != "")
                {
                    errorMsg = getDestValue(joRtn, "errmsg");
                    return -1;
                }
                else if (errorCode != "")
                {
                    errorMsg = getDestValue(joRtn, "errorMessage");
                    return -2;
                }
                else
                {
                    errorMsg = getDestValue(joRtn, "err_msg");
                    return int.Parse(getDestValue(joRtn, "infcode"));
                }
            }
            catch (Exception ex)
            {
                errorMsg = "解析中心返参发生异常：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 组织IRIS返参
        /// </summary>
        /// <param name="errorCode"></param>
        /// <param name="errorMessage"></param>
        /// <param name="joResult"></param>
        /// <returns></returns>
        public static JObject setIrisReturnValue(int errorCode, string errorMessage, JObject joResult)
        {
            try
            {
                dynamic joRtn = new JObject();
                joRtn.errorCode = errorCode;
                joRtn.errorMessage = errorMessage;
                joRtn.Add("result", joResult);
                return joRtn;
            }
            catch (Exception ex)
            {
                return setExceptionJson(-1, "setIrisReturnValue:", ex.Message);
            }
        }

        /// <summary>
        /// 解析IRIS返参
        /// </summary>
        /// <param name="joRtn"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public static int parseIrisRtnValue(JObject joRtn, out string errorMsg)
        {
            try
            {
                errorMsg = getDestValue(joRtn, "errorMessage");
                return int.Parse(getDestValue(joRtn, "errorCode"));
            }
            catch (Exception ex)
            {
                errorMsg = "解析Iris返参发生异常：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 解析业务JSON串（业务串固定格式为{errorCode = ,errorMessage = ,result ={}}）
        /// </summary>
        /// <param name="json"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public static int parseCenterReturnJson(JObject json, out string errorMsg)
        {
            try
            {
                errorMsg = "";
                if (int.Parse(json["infcode"].ToString()) != 0)
                {
                    errorMsg = json["err_msg"].ToString();
                }
                return int.Parse(json["infcode"].ToString());
            }
            catch (Exception ex)
            {
                errorMsg = "parseCenterReturnJson" + ex.Message;
                return -1;
            }

        }

        /// <summary>
        /// 移动支付组织中心入参
        /// </summary>
        /// <param name="infno"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string setMPCenterInpar(string infno, string txtData)
        {
            EncryptHelper encrypt = new EncryptHelper();
            string plainText = SignUtil.SortInput(JObject.Parse(txtData));
            string signData = "";
            string output = encrypt.encrypt(txtData, ref signData);
            return output;
        }

        public static string setMPCenterInpar(string infno, JObject joInput)
        {
            return setMPCenterInpar(infno, joInput.ToString());
        }

        /// <summary>
        /// 解析移动返参
        /// </summary>
        /// <param name="joRtn"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public static int parseMPRtnValue(JObject joRtn, out string errorMsg)
        {
            try
            {
                string success = getDestValue(joRtn, "success");
                string message = getDestValue(joRtn, "message");
                string encData = getDestValue(joRtn, "encData");
                //Global.writeLog("success:" + success);
                if (success != "True")
                {
                    errorMsg = message;
                    return -1;
                }
                else
                {
                    errorMsg = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                errorMsg = "解析中心返参发生异常：" + ex.Message;
                return -1;
            }

        }
    }
}
