示例#1
0
        /// <summary>
        /// 重写推送方法
        /// </summary>
        /// <param name="accessTokenModel"></param>
        /// <param name="contentModel"></param>
        /// <returns></returns>
        public override string Push(U_AccessToken accessTokenModel, U_Content contentModel)
        {
            //获取AccessToken
            bool isSuccess = SetCorpAccount(accessTokenModel);

            // var isOk = 0;
            if (isSuccess)
            {
                var    payments = new List <Business_Enterprisepayment_Information>();
                string postUrl  = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/paywwsptrans2pocket";
                //List<U_WeChatUser> weChatUserList = SearchWeChatUserList();
                //string pushObject = GetPushObject(contentModel, weChatUserList);//13788907365|15618738991|18301914615
                string   pushObject = GetPushObject(contentModel);
                string[] pushObjs   = pushObject.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); //推送的人数
                int      money      = (int)(contentModel.RedpacketMoney * 100);                               //总金额(分)
                var      redPacket  = Enumerable.Repeat(money, pushObjs.Length).ToArray();
                for (int i = 0; i < pushObjs.Length; i++)
                {
                    var payment = new Business_Enterprisepayment_Information();
                    payment.Business_WeChatPushVguid = contentModel.VGUID;
                    WxPayData data = new WxPayData();
                    data.SetValue("appid", WxPayConfig.APPID);               //公众账号ID
                    data.SetValue("mch_id", WxPayConfig.MCHID);              //商户号
                    data.SetValue("nonce_str", WxPayApi.GenerateNonceStr()); //随机字符串
                    var outTradeNo = WxPayApi.GenerateOutTradeNo();
                    payment.OrderNumber = outTradeNo;
                    data.SetValue("partner_trade_no", outTradeNo);  //商户订单号
                    string openid   = ConvertToOpenidByUserId(_accessToken, pushObjs[i]);
                    var    openInfo = openid.JsonToModel <U_OpenInfo>();
                    data.SetValue("openid", openInfo.openid);                               //商户appid下,某用户的openid
                    data.SetValue("check_name", "NO_CHECK");                                //校验用户姓名选项(NO_CHECK:不校验真实姓名 FORCE_CHECK:强校验真实姓名)
                    //data.SetValue("re_user_name", "刘洋");    //收款用户真实姓名。 如果check_name设置为FORCE_CHECK,则必填用户真实姓名
                    data.SetValue("amount", redPacket[i]);                                  //金额,单位为分
                    payment.RedpacketMoney = (decimal)(redPacket[i] * 1.0 / 100);           //红包金额
                    data.SetValue("desc", contentModel.Message);                            //付款说明
                    data.SetValue("spbill_create_ip", "192.168.0.1");                       //Ip地址
                    data.SetValue("ww_msg_type", "NORMAL_MSG");                             //付款消息类型
                    data.SetValue("act_name", contentModel.Title);                          //项目名称
                    data.SetValue("workwx_sign", data.MakeWorkWxSign("payment"));           //企业微信签名
                    data.SetValue("sign", data.MakeSign());                                 //微信支付签名

                    payment.UserID = pushObjs[i];                                           //红包接收人的微信号
                    string    xml      = data.ToXml();
                    string    response = PostWebRequest(postUrl, xml, Encoding.UTF8, true); //调用HTTP通信接口提交数据到API
                    WxPayData result   = new WxPayData();
                    result.FromXml(response);
                    if (result.GetValue("return_code").ToString() == "SUCCESS" && result.GetValue("result_code").ToString() == "SUCCESS")
                    {
                        payment.RedpacketStatus = 1;  //成功
                        var paymentTime = result.GetValue("payment_time").ToString();
                        payment.CreatedDate = DateTime.Parse(paymentTime);
                    }
                    else
                    {
                        payment.RedpacketStatus = 2; //失败
                        payment.Reson           = result.GetValue("err_code_des").ToString();
                        LogManager.WriteLog(LogFile.Error, result.GetValue("err_code") + ":" + result.GetValue("err_code_des"));
                    }
                    payment.VGUID       = Guid.NewGuid();
                    payment.CreatedDate = DateTime.Now;
                    payment.CreatedUser = "******";
                    payments.Add(payment);
                }

                InsertPaymentInfos(payments);
                UpdatePushStatus(contentModel);
                return("推送成功!");
            }
            return("推送失败!");
        }
示例#2
0
        /// <summary>
        /// 重写推送方法
        /// </summary>
        /// <param name="accessTokenModel"></param>
        /// <param name="contentModel"></param>
        /// <returns></returns>
        public override string Push(U_AccessToken accessTokenModel, U_Content contentModel)
        {
            //获取AccessToken
            bool isSuccess = SetCorpAccount(accessTokenModel);

            if (isSuccess)
            {
                var listRedPacket = new List <Business_Redpacket_Push_Information>();
                //List<U_WeChatUser> weChatUserList = SearchWeChatUserList();
                //string pushObject = GetPushObject(contentModel, weChatUserList);//13788907365|15618738991|18301914615
                string   pushObject = GetPushObject(contentModel);
                string[] pushObjs   = pushObject.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); //推送的人数
                int[]    redPacket  = new int[pushObjs.Length - 1];
                try
                {
                    //固定金额红包推送 总金额固定,按照人数计算每个红包金额(每个红包金额需一致)
                    // 注:如果无法平均分配,则最后一人多出(例:10元分给3人,则金额为3.3、3.3、3.4)
                    if (contentModel.RedpacketType == 1)
                    {
                        int money = (int)(contentModel.RedpacketMoney * 100);  //总金额(分)
                        redPacket = GetAvgRedPacket(pushObjs.Length, money);
                    }
                    else if (contentModel.RedpacketType == 2)                 //红包总金额(RMB),推送给每人的金额随机。
                    {
                        int money = (int)(contentModel.RedpacketMoney * 100); //总金额(分)
                        redPacket = GetRandomRedPacket(pushObjs.Length, money);
                    }
                    else if (contentModel.RedpacketType == 3)  //输入单个红包随机金额区间(RMB),发给每人红包的金额在此区间内
                    {
                        int moneyFrom = (int)(contentModel.RedpacketMoneyFrom * 100);
                        int moneyTo   = (int)(contentModel.RedpacketMoneyTo * 100);
                        redPacket = GetSingleRandomRedPacket(pushObjs.Length, moneyFrom, moneyTo);
                    }
                    for (int i = 0; i < pushObjs.Length; i++)
                    {
                        WxPayData data = new WxPayData();
                        Business_Redpacket_Push_Information redpacketPushInfo = new Business_Redpacket_Push_Information();
                        redpacketPushInfo.Business_WeChatPushVguid = contentModel.VGUID; //推送主键
                        data.SetValue("nonce_str", WxPayApi.GenerateNonceStr());         //随机字符串
                        var outTradeNo = WxPayApi.GenerateOutTradeNo();
                        redpacketPushInfo.OrderNumber = outTradeNo;
                        data.SetValue("mch_billno", outTradeNo);     //商户订单号
                        data.SetValue("mch_id", WxPayConfig.MCHID);  //商户号
                        data.SetValue("wxappid", WxPayConfig.APPID); //公众账号ID
                        data.SetValue("sender_name", "大众交通出租分公司");   //商户名称
                        //string filePath = AppDomain.CurrentDomain.BaseDirectory + "logo.png";
                        //string mediaid = UploadTempResource(filePath);
                        data.SetValue("sender_header_media_id", "1G6nrLmr5EC3MMb_-zK1dDdzmd0p7cNliYu9V5w7o8K0"); //发送者头像,此id为微信默认的头像
                        string openid   = ConvertToOpenidByUserId(_accessToken, pushObjs[i]);
                        var    openInfo = JsonHelper.JsonToModel <U_OpenInfo>(openid);
                        JsonConvert.DeserializeObject <U_OpenInfo>(openid);
                        data.SetValue("re_openid", openInfo.openid);                                                 //用户openid
                        data.SetValue("total_amount", redPacket[i]);                                                 //付款金额,单位分
                        redpacketPushInfo.RedpacketMoney = (decimal)(redPacket[i] * 1.0 / 100);                      //红包金额
                        data.SetValue("wishing", contentModel.Message);                                              //红包祝福语
                        data.SetValue("act_name", contentModel.Title);                                               //活动名称
                        data.SetValue("remark", "快来抢");                                                              //备注
                        data.SetValue("scene_id", "PRODUCT_4");                                                      //场景(金额大于200元时必填)
                        data.SetValue("workwx_sign", data.MakeWorkWxSign("redPacket"));                              //企业微信签名
                        data.SetValue("sign", data.MakeSign());                                                      //微信支付签名
                        redpacketPushInfo.UserID = pushObjs[i];                                                      //红包接收人的微信号
                        string       xml      = data.ToXml();
                        const string postUrl  = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendworkwxredpack"; //发送企业红包接口地址
                        string       response = PostWebRequest(postUrl, xml, Encoding.UTF8, true);                   //调用HTTP通信接口提交数据到API
                        WxPayData    result   = new WxPayData();
                        result.FromXml(response);
                        if (result.GetValue("return_code").ToString() == "SUCCESS" && result.GetValue("result_code").ToString() == "SUCCESS")
                        {
                            redpacketPushInfo.RedpacketStatus = 2; //已发送待领取
                        }
                        else
                        {
                            redpacketPushInfo.RedpacketStatus = 3; //发送失败
                            redpacketPushInfo.Reson           = result.GetValue("err_code_des").ToString();
                            LogManager.WriteLog(LogFile.Error, result.GetValue("err_code") + ":" + result.GetValue("err_code_des"));
                        }
                        redpacketPushInfo.VGUID       = Guid.NewGuid();
                        redpacketPushInfo.CreatedDate = DateTime.Now;
                        redpacketPushInfo.CreatedUser = "******";
                        listRedPacket.Add(redpacketPushInfo);
                    }
                }
                catch (Exception ex)
                {
                    LogManager.WriteLog(LogFile.Error, ex.ToString());
                    return("推送失败!");
                }
                UpdatePushStatus(contentModel);
                InsertRedPacketInfo(listRedPacket);
                return("推送成功!");
            }
            return("推送失败!");
        }