using System;
using System.Collections.Generic;
using Library;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
using Microsoft.AspNetCore.Http;
using System.Text;
using System.IO;
using System.Linq;
using MySystem;
using LitJson;
using System.Net;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Engines;

namespace MySystem
{
    public class WeChatFunction
    {
        private HttpContext _context;

        public string AppId = "wx6e074b8e07b6577e";
        public string AppSecret = "883f9ef7831d8ca82168c3fd6dcfb750";
        string MchId = "1613112281";
        string serialNo = "14E9FD7F0981BEA23F94137607521CB51C4F76EF";
        public string platSerialNo = "31CF8918DD2C48D8E6AC645233DCE5A27FA47344";

        public WeChatFunction(HttpContext context)
        {
            _context = context;
        }
        public WeChatFunction()
        {
        }

        #region 领取会员卡

        public Dictionary<string, object> GetUserCard(int MerchantId)
        {
            string token = new WeChat.WeChatPublicClass(_context).getWXToken(AppId, AppSecret);
            string resp = function.GetWebRequest("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + token + "&type=wx_card");
            JsonData jsonObj = JsonMapper.ToObject(resp);
            string ticket = jsonObj["ticket"].ToString();
            string timeStamp = function.ConvertDateTimeInt(DateTime.Now).ToString();
            string nonce_str = function.get_Random(12);
            var pay_param = new SortedList<string, string>
            {
                {"timeStamp", timeStamp},
                {"nonceStr", nonce_str},
                {"ticket", ticket}
            };
            var pay_param_str = String.Join("&", pay_param.Select(item => String.Format("{0}={1}", item.Key.Trim(), item.Value)).ToArray());
            string signature = new WeChat.WeChatPublicClass(_context).SHA1(pay_param_str);
            Dictionary<string, object> cardExt = new Dictionary<string, object>();
            cardExt.Add("timestamp", timeStamp);
            cardExt.Add("nonce_str", nonce_str);
            cardExt.Add("signature", signature);
            return cardExt;
        }
        #endregion

        #region 服务商平台JSAPI下单

        public Dictionary<string, string> Pay(string SubMchId, decimal PayMoney, string OrderNo, string Description, string OpenId, string Key, string NotifyUrl, bool IsAct = false)
        {
            Dictionary<string, string> return_result = new Dictionary<string, string>();
            int amount = (int)(PayMoney * 100);
            //具体请求参数
            SortedList<string, object> par = new SortedList<string, object>();
            par.Add("sp_appid", AppId); //服务商应用ID
            par.Add("sp_mchid", MchId); //服务商户号
            par.Add("sub_mchid", SubMchId); //子商户号
            par.Add("description", Description); //商品描述
            par.Add("out_trade_no", OrderNo); //商户订单号
            par.Add("notify_url", NotifyUrl); //服务商应用ID
            Dictionary<string, object> settle_info = new Dictionary<string, object>();
            settle_info.Add("profit_sharing", IsAct); //是否指定分账
            par.Add("settle_info", settle_info); //结算信息
            Dictionary<string, object> amountDic = new Dictionary<string, object>();
            amountDic.Add("total", amount); //总金额
            par.Add("amount", amountDic); //订单金额
            Dictionary<string, object> payer = new Dictionary<string, object>();
            payer.Add("sp_openid", OpenId); //用户在服务商appid下的唯一标识
            par.Add("payer", payer); //支付者

            string req = Newtonsoft.Json.JsonConvert.SerializeObject(par);
            function.WriteLog(req, "服务商平台JSAPI下单请求参数");
            try
            {
                function.WriteLog(serialNo, "服务商平台JSAPI下单请求参数");
                function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(par), "服务商平台JSAPI下单请求参数");
                string result = postJson("https://api.mch.weixin.qq.com/v3/pay/partner/transactions/jsapi", req, prikey, MchId, serialNo);
                function.WriteLog(result, "服务商平台JSAPI下单请求参数");
                JsonData jsonObj = JsonMapper.ToObject(result);
                string timeStamp = function.ConvertDateTimeInt(DateTime.Now).ToString();
                string nonce_str = function.get_Random(12);
                string package = "prepay_id=" + jsonObj["prepay_id"].ToString();

                var pay_param = new SortedList<string, string>
                {
                    {"appId", AppId},
                    {"timeStamp", timeStamp},
                    {"nonceStr", nonce_str},
                    {"package", package},
                    {"signType", "RSA"}
                };
                var pay_param_str = "";
                pay_param_str += AppId + "\n";
                pay_param_str += timeStamp + "\n";
                pay_param_str += nonce_str + "\n";
                pay_param_str += package + "\n";

                string paySign = Sign(pay_param_str, prikey);
                return_result.Add("appId", AppId);
                return_result.Add("timeStamp", timeStamp);
                return_result.Add("nonceStr", nonce_str);
                return_result.Add("package", package);
                return_result.Add("paySign", paySign);
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "服务商品台JSAPI下单异常");
            }
            return return_result;
        }
        
        #endregion

        #region 服务商平台申请退款API

        public Dictionary<string, string> Refund(string AppId, string MchId, string SubMchId, decimal RefundMoney, decimal TotalMoney, string OrderNo, string RefundNo, string Description, string OpenId, string Key, string NotifyUrl)
        {
            Dictionary<string, string> return_result = new Dictionary<string, string>();
            int refund = (int)(RefundMoney * 100);
            int amount = (int)(TotalMoney * 100);
            //具体请求参数
            SortedList<string, object> par = new SortedList<string, object>();
            par.Add("sub_mchid", SubMchId); //子商户号
            par.Add("out_trade_no", OrderNo); //商户订单号
            par.Add("out_refund_no", RefundNo); //商户退款单号
            Dictionary<string, object> amountDic = new Dictionary<string, object>();
            amountDic.Add("refund", refund); //退款金额
            amountDic.Add("total", amount); //原订单金额
            amountDic.Add("currency", "CNY"); //退款币种
            par.Add("amount", amountDic); //金额信息

            string req = Newtonsoft.Json.JsonConvert.SerializeObject(par);
            function.WriteLog(req, "服务商平台申请退款API");
            try
            {
                string merchantId = MchId;   //商户号
                string serialNo = "62B64854B768D344249FDB9E6EF4A1232C000E84";  //证书编号                
                string result = postJson("https://api.mch.weixin.qq.com/v3/refund/domestic/refunds", req, prikey, merchantId, serialNo);
                function.WriteLog(result, "服务商平台申请退款API");
                JsonData jsonObj = JsonMapper.ToObject(result);
                return_result.Add("refund_id", jsonObj["refund_id"].ToString()); //微信支付退款单号
                return_result.Add("channel", jsonObj["channel"].ToString()); //退款渠道
                return_result.Add("user_received_account", jsonObj["user_received_account"].ToString()); //退款入账账户
                return_result.Add("status", jsonObj["status"].ToString()); //退款状态
                return_result.Add("payer_total", jsonObj["amount"]["payer_total"].ToString()); //用户支付金额
                return_result.Add("payer_refund", jsonObj["amount"]["payer_refund"].ToString()); //用户退款金额
                return_result.Add("settlement_refund", jsonObj["amount"]["settlement_refund"].ToString()); //应结退款金额
                return_result.Add("settlement_total", jsonObj["amount"]["settlement_total"].ToString()); //应结订单金额
                return_result.Add("discount_refund", jsonObj["amount"]["discount_refund"].ToString()); //优惠退款金额
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "服务商平台申请退款API异常");
            }
            return return_result;
        }
        #endregion

        #region 服务商平台查询单笔退款API

        public Dictionary<string, string> QueryRefund(string RefundNo)
        {
            Dictionary<string, string> return_result = new Dictionary<string, string>();
            try
            {
                string result = function.GetWebRequest("https://api.mch.weixin.qq.com/v3/refund/domestic/refunds/" + RefundNo);
                JsonData jsonObj = JsonMapper.ToObject(result);
                return_result.Add("refund_id", jsonObj["refund_id"].ToString()); //微信支付退款单号
                return_result.Add("transaction_id", jsonObj["transaction_id"].ToString()); //微信支付订单号
                return_result.Add("out_trade_no", jsonObj["out_trade_no"].ToString()); //商户订单号
                return_result.Add("channel", jsonObj["channel"].ToString()); //退款渠道
                return_result.Add("user_received_account", jsonObj["user_received_account"].ToString()); //退款入账账户
                return_result.Add("status", jsonObj["status"].ToString()); //退款状态
                return_result.Add("total", jsonObj["amount"]["total"].ToString()); //订单金额
                return_result.Add("refund", jsonObj["amount"]["refund"].ToString()); //退款金额
                return_result.Add("payer_total", jsonObj["amount"]["payer_total"].ToString()); //用户支付金额
                return_result.Add("payer_refund", jsonObj["amount"]["payer_refund"].ToString()); //用户退款金额
                return_result.Add("settlement_refund", jsonObj["amount"]["settlement_refund"].ToString()); //应结退款金额
                return_result.Add("settlement_total", jsonObj["amount"]["settlement_total"].ToString()); //应结订单金额
                return_result.Add("discount_refund", jsonObj["amount"]["discount_refund"].ToString()); //优惠退款金额
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "服务商平台查询单笔退款API异常");
            }
            return return_result;
        }
        #endregion

        #region 服务商平台请求分账API

        public string ProfitShare(string SubMchId, string TradeNo, string OrderNo, List<ReceiverList> Receivers) //有活动分0.62%，没活动不分账
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            result.Add("sub_mchid", SubMchId); //子商户号
            result.Add("appid", AppId); //应用ID
            result.Add("transaction_id", TradeNo); //微信订单号
            result.Add("out_order_no", OrderNo); //商户分账单号
            result.Add("receivers", Receivers); //分账接收方列表
            result.Add("unfreeze_unsplit", true); //是否解冻剩余未分资金
            string req = Newtonsoft.Json.JsonConvert.SerializeObject(result);
            string resp = postJson("https://api.mch.weixin.qq.com/v3/profitsharing/orders", req, prikey, MchId, serialNo);
            return resp;
            //{"order_id":"30000104462023060249559178531","out_order_no":"2023060210583730173782834","receivers":[{"account":"1613112281","amount":3,"create_time":"2023-06-02T11:34:10+08:00","description":"服务费","detail_id":"36000104462023060269999025889","finish_time":"1970-01-01T08:00:00+08:00","result":"PENDING","type":"MERCHANT_ID"},{"account":"1645266943","amount":7,"create_time":"2023-06-02T11:34:10+08:00","description":"解冻给分账方","detail_id":"36000104462023060269999025890","finish_time":"1970-01-01T08:00:00+08:00","result":"PENDING","type":"MERCHANT_ID"}],"state":"PROCESSING","sub_mchid":"1645266943","transaction_id":"4200001833202306025522158377"}
        }
        #endregion

        #region 服务商平台请求分账回退API

        public string ProfitShareReturn(string SubMchId, string ReturnMchId, string OrderNo, string ReturnNo, decimal Amount)
        {
            int ReturnAmount = (int)(Amount * 100);
            Dictionary<string, object> result = new Dictionary<string, object>();
            result.Add("sub_mchid", SubMchId); //子商户号
            result.Add("out_order_no", OrderNo); //商户分账单号
            result.Add("out_return_no", ReturnNo); //商户回退单号
            result.Add("return_mchid", ReturnMchId); //回退商户号
            result.Add("amount", ReturnAmount); //回退金额
            result.Add("description", "用户退款"); //回退描述
            string req = Newtonsoft.Json.JsonConvert.SerializeObject(result);
            string resp = postJson("https://api.mch.weixin.qq.com/v3/profitsharing/return-orders", req, prikey, MchId, serialNo);
            return resp;
        }
        #endregion

        #region 服务商平台添加分账接收方API

        public string AddReceive(string SubMchId, string TypeName, string Account, string Name, string Relation)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            result.Add("sub_mchid", SubMchId); //子商户号
            result.Add("appid", AppId); //应用ID
            result.Add("type", TypeName); //分账接收方类型 MERCHANT_ID：商户ID  PERSONAL_OPENID：个人openid（由父商户APPID转换得到） PERSONAL_SUB_OPENID：个人sub_openid（由子商户APPID转换得到）
            result.Add("account", Account); //分账接收方账号
            if(!string.IsNullOrEmpty(Name))
            {
                result.Add("name", RSAEncrypt(Name)); //分账个人接收方姓名
            }
            result.Add("relation_type", Relation); //与分账方的关系类型 STORE：门店 STAFF：员工 STORE_OWNER：店主 PARTNER：合作伙伴 HEADQUARTER：总部 BRAND：品牌方 DISTRIBUTOR：分销商 USER：用户 SUPPLIER： 供应商 CUSTOM：自定义
            string req = Newtonsoft.Json.JsonConvert.SerializeObject(result);
            string resp = postJson("https://api.mch.weixin.qq.com/v3/profitsharing/receivers/add", req, prikey, MchId, serialNo);
            return resp;
            //{"account":"1613112281","name":"x+8m5jM4x2jSZLDOpN46Js9JyeNgUXkFSmGjCBN45QcSYUNPN86TOheRF64WlwDeO45E/walWPeZSksNN/LfJiB2fZRVun1CJuJ7zxePma8L7iDbKS92i+d38rvOmNTjLhKX64O8oJR4HMtFw1HryxJlUFgM5/ickJ79WGrX3GaVArWisgEzwOyxGBfGSL3ofJhi5TdTnrxHMzqYWv43DRvI9Ja5eMgFbgrGcn+JiJVKQcYj1L8tZ6xQeQCXkY6ubyDiV9wQps2nQA1Oh+UdFcS13Ex4FUSYLCczGFAl4EV8TBJQleZeC1p4ZJquJjje0uMjI2xB6S41yUBoN86gkA==","relation_type":"PARTNER","sub_mchid":"1645266943","type":"MERCHANT_ID"}
        }
        #endregion

        #region 服务商平台查询分账结果API

        public string QueryProfitShare(string SubMchId, string TradeNo, string OrderNo)
        {
            string resp = postJson("https://api.mch.weixin.qq.com/v3/profitsharing/orders/" + OrderNo + "?sub_mchid=" + SubMchId + "&transaction_id=" + TradeNo, "", prikey, MchId, serialNo, "GET");
            return resp;
            //{"order_id":"30000104462023060249559178531","out_order_no":"2023060210583730173782834","receivers":[{"account":"1613112281","amount":3,"create_time":"2023-06-02T11:34:10+08:00","description":"服务费","detail_id":"36000104462023060269999025889","finish_time":"2023-06-02T11:34:41+08:00","result":"SUCCESS","type":"MERCHANT_ID"},{"account":"1645266943","amount":7,"create_time":"2023-06-02T11:34:10+08:00","description":"解冻给分账方","detail_id":"36000104462023060269999025890","finish_time":"2023-06-02T11:34:41+08:00","result":"SUCCESS","type":"MERCHANT_ID"}],"state":"FINISHED","sub_mchid":"1645266943","transaction_id":"4200001833202306025522158377"}
        }
        #endregion

        #region 服务商平台解冻剩余资金API

        public string Unfreeze(string SubMchId, string TradeNo, string OrderNo, string Description)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            result.Add("sub_mchid", SubMchId); //子商户号
            result.Add("transaction_id", TradeNo); //微信订单号
            result.Add("out_order_no", OrderNo); //商户分账单号
            result.Add("description", Description); //分账描述
            string req = Newtonsoft.Json.JsonConvert.SerializeObject(result);
            string resp = postJson("https://api.mch.weixin.qq.com/v3/profitsharing/orders/unfreeze", req, prikey, MchId, serialNo);
            return resp;
        }
        #endregion




        #region 微信客服

        public string CustomerServiceChat()
        {
            return "需要对接";
        }

        #endregion






        #region 获取平台证书

        public string GetPlatCert()
        { 
            string merchantId = MchId;   //商户号
            string result = postJson("https://api.mch.weixin.qq.com/v3/certificates", "", prikey, merchantId, serialNo, "GET");
            return result;
            //{"data":[{"effective_time":"2022-01-23T14:02:08+08:00","encrypt_certificate":{"algorithm":"AEAD_AES_256_GCM","associated_data":"certificate","ciphertext":"R+Z85w96Lv7GjtL2zGcwTugGAQkmRUnbKqpQ3VYU8cNcdamTTcJIOgFSXLdHykB1OC70onaRcTxX2Tp1RxlSVm8fbiJBtWpHw28qBIvziJDT9n9R8kF3ayofYulh0mE+46a2jnwO3ioccTaqaDM1qVChElmdksZ3ir79JZn+QfyMcn+O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EfXJeXA6yr2tHJN1u3Qg5mS/n5maGkhBW0M9fkkNkoC4KGXmGuh5pnl3bPCeugAg7ggtkLD7SqwotdgVbx33Lng2NlsOozJSFGhIZCCicCKwY5BFec73KrQJgo3VtrKppujssB4ag3nA3VcPunPg/ezMTdYbX6sl9/fRxgMafrQzOcKq0JdujwAz4Ecw6HCUyY420+P7qpUijnOhvTNX2ug37OwxhOcqGMzd8d4C4j7246CRgS7dahYOShtV8iBYbiy+F+kzudQgzKY9aD93IMjpNeuUpujzv9KRgqJRwxD9OxwnjMiLz+Mo4psNvl0GTSF3J+AdS6oeF5l4BmhGrXEEMe/KUook3m7s86MkoIKDM4nszNY8HdzzXdjfw+6Qh0oQd7eW0nwFPPtac0yVSjqrLQzKQsqYs4F7zF0eKwWyhRuR5N23Rk4MX49YIYMjXbWB6iiricIRiO1f6HJ8Xf6F66RFJy4dYR1uwWdKGk6kc5ZMSn0HE6zqGBdZQV3BsEkz6wJZ1ZOpFThr0vA9poN0F32c2mJyXDtB1qMytUiEKWyPy2sJ6BIXSB6DsDXQ9U9QGBnKjQ8+yvCglocPjs6JA84cjhXFeR8ElWNmZJ/njUlmlHQ==","nonce":"92306d7e8732"},"expire_time":"2027-01-22T14:02:08+08:00","serial_no":"62B64854B768D344249FDB9E6EF4A1232C000E84"}]}
        }
        public string AesGcmDecrypt(string associatedData, string nonce, string ciphertext)
        {
            GcmBlockCipher gcmBlockCipher = new GcmBlockCipher(new AesEngine());
            AeadParameters aeadParameters = new AeadParameters(
                new KeyParameter(Encoding.UTF8.GetBytes(AppConfig.WeChatParam.AesGemKey)),
                128,
                Encoding.UTF8.GetBytes(nonce),
                Encoding.UTF8.GetBytes(associatedData));
            gcmBlockCipher.Init(false, aeadParameters);

            byte[] data = Convert.FromBase64String(ciphertext);
            byte[] plaintext = new byte[gcmBlockCipher.GetOutputSize(data.Length)];
            int length = gcmBlockCipher.ProcessBytes(data, 0, data.Length, plaintext, 0);
            gcmBlockCipher.DoFinal(plaintext, length);
            return Encoding.UTF8.GetString(plaintext);
        }

        public string GetPublicKey()
        {
            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<certModel>(GetPlatCert());
            var cmodel = result.data.OrderByDescending(t => t.expire_time).FirstOrDefault();
            string pubkey = AesGcmDecrypt(cmodel.encrypt_certificate.associated_data, cmodel.encrypt_certificate.nonce, cmodel.encrypt_certificate.ciphertext);
            pubkey = pubkey.Replace("-----BEGIN CERTIFICATE-----", "").Replace("-----END CERTIFICATE-----", "").Replace("\n", "");
            return pubkey;
        }

        #endregion

        #region 敏感信息加密

        string pubkey = "MIID3DCCAsSgAwIBAgIUYdmfchi0h3iONdbE4+05jgl50/YwDQYJKoZIhvcNAQELBQAwXjELMAkGA1UEBhMCQ04xEzARBgNVBAoTClRlbnBheS5jb20xHTAbBgNVBAsTFFRlbnBheS5jb20gQ0EgQ2VudGVyMRswGQYDVQQDExJUZW5wYXkuY29tIFJvb3QgQ0EwHhcNMjIwMTIzMDYwMjA4WhcNMjcwMTIyMDYwMjA4WjBuMRgwFgYDVQQDDA9UZW5wYXkuY29tIHNpZ24xEzARBgNVBAoMClRlbnBheS5jb20xHTAbBgNVBAsMFFRlbnBheS5jb20gQ0EgQ2VudGVyMQswCQYDVQQGDAJDTjERMA8GA1UEBwwIU2hlblpoZW4wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDSFtwof74G5sJuQya6OJKhfjMcVZ3PZk+uG0RWULyDwOZezvYjLp9jkkfTCWuWDkshRmrDhVj89eOPwy60QDYKaKQ1Mq48BqoQrOikSK5bxAMGHg1a/0UcPM3yL3T84nlZkFlySeXN8cqWkZnPIXeO/blYCyBxNizmUwIIJUlI1Fbhy4lQRsoLoRbeh+YUQ2AI9D2plzVDY5jkG9/FQWhyZr5K3rgjblb/pMZngmzSYCNBWfJP9EZfRuQvZokMEddytC3JoSPgvqA25RMAPvj1cnAiOypgm7BHhx+a1mpmz79ifWlDF9rglo6WDGAxQ4JDuPJTVOiQ9EsfS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eUn7yGmvz4QC+sFO32haYr1SaAzZvMhf3RQVuAiFOnw"; //私钥
        public string RSAEncrypt(string text)
        {
            byte[] publicKey = Convert.FromBase64String(pubkey);
            // var rsa = RSA.Create();
            // rsa.ImportRSAPublicKey(publicKey, out _);
            // var buff = rsa.Encrypt(Encoding.UTF8.GetBytes(text), RSAEncryptionPadding.OaepSHA1);
            // return Convert.ToBase64String(buff);
            using (var x509 = new X509Certificate2(publicKey))
            {
                using (var rsa = x509.GetRSAPublicKey())
                {
                    var buff = rsa.Encrypt(Encoding.UTF8.GetBytes(text), RSAEncryptionPadding.OaepSHA1);
                    return Convert.ToBase64String(buff);
                }
            }
        }
        public string postJson(string url, string postData, string privateKey, string merchantId, string serialNo, string method = "POST")
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = method;
            request.ContentType = "application/json;charset=UTF-8";
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36";
            request.Accept = "application/json";

            string Authorization = GetAuthorization(url, method, postData, privateKey, merchantId, serialNo);
            request.Headers.Add("Authorization", Authorization);
            request.Headers.Add("Wechatpay-Serial", platSerialNo);
            if (!string.IsNullOrEmpty(postData))
            {
                byte[] paramJsonBytes;
                paramJsonBytes = System.Text.Encoding.UTF8.GetBytes(postData);
                request.ContentLength = paramJsonBytes.Length;
                Stream writer;
                try
                {
                    writer = request.GetRequestStream();
                }
                catch (Exception)
                {
                    writer = null;
                    Console.Write("连接服务器失败!");
                }
                writer.Write(paramJsonBytes, 0, paramJsonBytes.Length);
                writer.Close();
            }

            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = ex.Response as HttpWebResponse;
            }
            Stream resStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(resStream);
            string text = reader.ReadToEnd();
            return text;
            //{\"code\":\"PARAM_ERROR\",\"message\":\"请确认待处理的消息是否为加密后的密文\"}
            //{\"applyment_id\":2000002247709762}
        }

        #endregion

        #region 图片上传

        public string GetMediaId(string imgPath)
        {
            if (string.IsNullOrEmpty(imgPath))
            {
                return "";
            }
            string key = "wechatpic:" + function.MD5_16(imgPath);
            string media_id = RedisDbconn.Instance.Get<string>(key);
            if (!string.IsNullOrEmpty(media_id))
            {
                return media_id;
            }
            string filePath = function.getPath(imgPath);
            var filename = Path.GetFileName(filePath);
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            Byte[] imgBytesIn = new Byte[fs.Length];
            fs.Read(imgBytesIn, 0, imgBytesIn.Length);
            fs.Close();

            byte[] hash = SHA256Managed.Create().ComputeHash(imgBytesIn);

            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                builder.Append(hash[i].ToString("x2"));
            }
            var sha256 = builder.ToString();
            string metaStr = "{\"filename\":\""+ filename + "\",\"sha256\":\"" + sha256 + "\"}";
            media_id = UploadImgApi(metaStr, imgBytesIn, filename);
            return media_id;
        }

        public string UploadImgApi(string metaStr, Byte[] imgBytesIn,string filename)
        {
            string url = "https://api.mch.weixin.qq.com/v3/merchant/media/upload";
            
            string merchantId = MchId;   //商户号
            string privateKey = prikey;
            #region 定义请求体中的内容 并转成二进制

            string boundary = "lc199aecd61b4653ef";
            string Enter = "\r\n";
            string campaignIDStr1
                = "--" + boundary
                + Enter
                + "Content-Disposition: form-data; name=\"meta\";"
                + Enter
                + "Content-Type:application/json;"
                + Enter
                + Enter
                + metaStr
                + Enter
                + "--" + boundary
                + Enter
                + "Content-Disposition:form-data;name=\"file\";filename=\""+ filename + "\";"
                + Enter
                + "Content-Type:image/jpeg"
                + Enter
                + Enter;
            byte[] byteData2
                = imgBytesIn;
            string campaignIDStr3 
                = Enter
                + "--" + boundary 
                + Enter;
            var byteData1 = System.Text.Encoding.UTF8.GetBytes(campaignIDStr1);

            var byteData3 = System.Text.Encoding.UTF8.GetBytes(campaignIDStr3);
            #endregion

            string transactionsResponse = UploadImg_postJson(url, byteData1, byteData2, byteData3, metaStr, privateKey, merchantId, serialNo, boundary, "POST");
            var result=JsonMapper.ToObject(transactionsResponse);
            return result["media_id"].ToString();
        }
        public string UploadImg_postJson(string url, byte[] b1, byte[] b2, byte[] b3, string metaStr, string privateKey, string merchantId, string serialNo, string boundary, string method = "POST")
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = method;
            //request.ContentType = "application/json;charset=UTF-8";
            request.ContentType = "multipart/form-data;boundary=" + boundary;
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36";
            request.Accept = "application/json";
            string Authorization = GetAuthorization(url, method, metaStr, privateKey, merchantId, serialNo);
            request.Headers.Add("Authorization", Authorization);

            Stream writer;
            try
            {
                writer = request.GetRequestStream();
            }
            catch (Exception)
            {
                writer = null;
            }
            writer.Write(b1, 0, b1.Length);
            writer.Write(b2, 0, b2.Length);
            writer.Write(b3, 0, b3.Length);
            writer.Close();

            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = ex.Response as HttpWebResponse;
            }
            Stream resStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(resStream);
            string text = reader.ReadToEnd();
            return text;
        }
        protected string GetAuthorization(string url, string method, string jsonParame, string privateKey, string merchantId, string serialNo)
        {
            var uri = new Uri(url);
            string urlPath = uri.PathAndQuery;
            string nonce = Guid.NewGuid().ToString();
            var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
            //数据签名     HTTP请求方法\n接口地址的url\n请求时间戳\n请求随机串\n请求报文主体\n
            method = string.IsNullOrEmpty(method) ? "" : method;
            string message = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n", method, urlPath, timestamp, nonce, jsonParame);
            string signTxt = Sign(message, privateKey);

            //Authorization和格式
            string authorzationTxt = string.Format("WECHATPAY2-SHA256-RSA2048 mchid=\"{0}\",nonce_str=\"{1}\",timestamp=\"{2}\",serial_no=\"{3}\",signature=\"{4}\"",
                merchantId,
                nonce,
                timestamp,
                serialNo,
                signTxt
                );
            return authorzationTxt;
        }

        protected string Sign(string message, string privateKey)
        {
            byte[] keyData = Convert.FromBase64String(privateKey);
            byte[] data = System.Text.Encoding.UTF8.GetBytes(message);
            var rsa = RSA.Create();
            rsa.ImportPkcs8PrivateKey(keyData, out _);
            return Convert.ToBase64String(rsa.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));

            // using (CngKey cngKey = CngKey.Import(keyData, CngKeyBlobFormat.Pkcs8PrivateBlob))
            // using (RSACng rsa = new RSACng(cngKey))
            // {
            //     return Convert.ToBase64String(rsa.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
            // }
        }

        #endregion

        #region 获取文件sha256

        public string GetSha256(FileStream stream)
        { 
            using (SHA256 mySHA256 = SHA256.Create())
            {
                byte[] hashValue = mySHA256.ComputeHash(stream);
                return Encoding.UTF8.GetString(hashValue);
            }
        }

        #endregion

        #region 判断长期

        public string CheckForever(DateTime? time)
        {
            if (time == null)
            {
                return "";
            }
            if (time.Value.Year >= 2050)
            {
                return "长期";
            }
            return time.Value.ToString("yyyy-MM-dd");
        }

        #endregion


    }
}