コード例 #1
0
        /// <summary>
        /// 拼团订单推送
        /// </summary>
        /// <param name="shopOrder"></param>
        /// <param name="wechatOptions"></param>
        /// <param name="receiverOpenId"></param>
        /// <param name="logger"></param>
        /// <returns></returns>
        static bool SendTemplateMessage(this ConglomerationOrder conglomerationOrder, WechatTemplateSendOptions wechatOptions,
                                        string receiverOpenId, ILogger logger)
        {
            logger.LogInformation($"推送拼团订单conglomerationOrderId:{conglomerationOrder.Id}");

            try
            {
                if (string.IsNullOrEmpty(receiverOpenId))
                {
                    return(false);
                }
                string accessToken = AccessTokenContainer.GetAccessTokenResult(wechatOptions.AppId).access_token;
                //logger.LogInformation("推送的accesstoken:{0}", accessToken);
                string url = wechatOptions.SendUrl + accessToken;

                TemplateData data = new TemplateData();
                #region 订单参数
                string remark = "";
                data.Add("first", new TemplateDataItem()
                {
                    value = "您好,您有新的支付订单请及时处理!",
                    color = TemplateSendColor
                });
                data.Add("keyword1", new TemplateDataItem()
                {
                    value = conglomerationOrder.OrderNumber.ToString(),
                    color = TemplateSendColor
                });
                data.Add("keyword2", new TemplateDataItem()
                {
                    value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    color = TemplateSendColor
                });
                data.Add("remark", new TemplateDataItem()
                {
                    value = remark,
                    color = TemplateSendColor
                });
                #endregion
                string detailUrl = $"http://manager.91huichihuihe.com//api/ShopOrderSetAPI/Manager/GetConglomerationOrderView?orderid={conglomerationOrder.Id}&openid={receiverOpenId}";
                ShopOrderTemplateModel templateModel = new ShopOrderTemplateModel()
                {
                    touser      = receiverOpenId,
                    template_id = wechatOptions.ServiceTemplateId,
                    url         = detailUrl,
                    data        = data
                };


                JsonSerializer jsonSerializer = new JsonSerializer();

                var responseWait = httpClient.PostAsync(url, new StringContent(JsonConvert.SerializeObject(templateModel)));
                responseWait.Wait();
                var response = responseWait.Result;

                if (response.IsSuccessStatusCode)
                {
                    var reContentWait = response.Content.ReadAsStringAsync();
                    reContentWait.Wait();
                    var reContent = reContentWait.Result;
                    if (JObject.Parse(reContent).TryGetValue("errcode", out JToken errcode))
                    {
                        if (errcode.Value <int>() == 0)
                        {
                            conglomerationOrder.IsSend = true;
                        }
                        else
                        {
                            conglomerationOrder.IsSend = false;
                        }
                    }
                    else
                    {
                        conglomerationOrder.IsSend = false;
                    }
                    return(true);
                }
                else
                {
                    var reContentWait = response.Content.ReadAsStringAsync();
                    reContentWait.Wait();
                    var reContent = reContentWait.Result;
                    logger.LogInformation("模板消息出错:{0}", reContent);
                    return(false);
                }
            }
            catch (Exception e)
            {
                logger.LogInformation("模板消息出错:{0}", e.Message);
                return(false);
            }
        }
コード例 #2
0
        static bool SendTemplateMessage(this ShopOrder shopOrder, ShopDbContext db, WechatTemplateSendOptions wechatOptions,
                                        string receiverOpenId, ILogger logger)
        {
            //logger.LogInformation("推送订单订单shopOrderId:{0}", shopOrder.Id);
            try
            {
                if (string.IsNullOrEmpty(receiverOpenId))
                {
                    return(false);
                }
                string accessToken = AccessTokenContainer.GetAccessTokenResult(wechatOptions.AppId).access_token;
                //logger.LogInformation("推送的accesstoken:{0}", accessToken);
                string       url  = wechatOptions.SendUrl + accessToken;
                TemplateData data = new TemplateData();
                string       templateId;
                string       remark = shopOrder.Remark ?? "";

                if (shopOrder.IsTakeOut)
                {
                    var takeOutInfo = db.Query <ShopOrderTakeout>().FirstOrDefault(m => m.ShopOrderId == shopOrder.Id);

                    MemberAddress address = null;
                    if (takeOutInfo != null && takeOutInfo.TakeWay == TakeWay.送货上门)
                    {
                        address = db.Query <MemberAddress>().FirstOrDefault(m => m.MemberId == shopOrder.MemberId);
                    }
                    templateId = wechatOptions.TakeOutTemplateId;
                    string customInfo = "", customAddress = "";
                    if (address != null)
                    {
                        customInfo    = $"{address.Name}{address.Sex}({address.Phone})";
                        customAddress = $"{address.Area}{address.Detail}";
                    }
                    else
                    {
                        customInfo    = "自提";
                        customAddress = "自提";
                    }
                    #region 订单参数
                    data.Add("first", new TemplateDataItem()
                    {
                        value = $"收到新的外卖订单!({DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")})",
                        color = TemplateSendColor
                    });
                    data.Add("keyword1", new TemplateDataItem()
                    {
                        value = shopOrder.OrderNumber,
                        color = TemplateSendColor
                    });
                    data.Add("keyword2", new TemplateDataItem()
                    {
                        value = db.GetSingle <Shop>(shopOrder.ShopId).Name,
                        color = TemplateSendColor
                    });
                    data.Add("keyword3", new TemplateDataItem()
                    {
                        value = customInfo,
                        color = TemplateSendColor
                    });
                    data.Add("keyword4", new TemplateDataItem()
                    {
                        value = customAddress,
                        color = TemplateSendColor
                    });
                    data.Add("keyword5", new TemplateDataItem()
                    {
                        value = $"¥{(shopOrder.Payment ?? 0) / 100f}",
                        color = TemplateSendColor
                    });
                    data.Add("remark", new TemplateDataItem()
                    {
                        value = remark,
                        color = TemplateSendColor
                    });
                    #endregion
                }
                else
                {
                    templateId = wechatOptions.ServiceTemplateId;
                    #region 订单参数
                    data.Add("first", new TemplateDataItem()
                    {
                        value = "您好,您有新的支付订单请及时处理!",
                        color = TemplateSendColor
                    });
                    data.Add("keyword1", new TemplateDataItem()
                    {
                        value = shopOrder.OrderNumber.ToString(),
                        color = TemplateSendColor
                    });
                    data.Add("keyword2", new TemplateDataItem()
                    {
                        value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        color = TemplateSendColor
                    });
                    data.Add("remark", new TemplateDataItem()
                    {
                        value = remark,
                        color = TemplateSendColor
                    });
                    #endregion
                }

                string detailUrl = $"http://manager.91huichihuihe.com/api/ShopOrderSetAPI/Manager/GetShopOrderView?orderid={shopOrder.Id}&openid={receiverOpenId}";
                ShopOrderTemplateModel templateModel = new ShopOrderTemplateModel()
                {
                    touser      = receiverOpenId,
                    template_id = templateId,
                    url         = detailUrl,
                    data        = data
                };


                JsonSerializer jsonSerializer = new JsonSerializer();

                var responseWait = httpClient.PostAsync(url, new StringContent(JsonConvert.SerializeObject(templateModel)));
                responseWait.Wait();
                var response = responseWait.Result;

                if (response.IsSuccessStatusCode)
                {
                    var reContentWait = response.Content.ReadAsStringAsync();
                    reContentWait.Wait();
                    var reContent = reContentWait.Result;
                    //if (JObject.Parse(reContent).TryGetValue("errcode", out JToken errcode))
                    //{
                    //    if (errcode.Value<int>() == 0)
                    //    {
                    //        shopOrder.IsSend = true;
                    //    }
                    //    else
                    //    {
                    //        shopOrder.IsSend = false;
                    //    }
                    //}
                    //else
                    //{
                    //    shopOrder.IsSend = false;
                    //}
                    return(true);
                }
                else
                {
                    var reContentWait = response.Content.ReadAsStringAsync();
                    reContentWait.Wait();
                    var reContent = reContentWait.Result;
                    logger.LogInformation("模板消息出错:{0}", reContent);
                    return(false);
                }
            }
            catch (Exception e)
            {
                logger.LogInformation("模板消息出错:{0}", e.Message);
                return(false);
            }
        }