Пример #1
0
        public async Task SendNewOrderMsgAsync(Order order)
        {
            var business = await GetAsync <Business>(order.BusinessId.Value);

            if (string.IsNullOrEmpty(business.NewOrderTemplateId))
            {
                return;
            }
            var msg = new WxEventMessage
            {
                template_id = business.NewOrderTemplateId
            };
            var productName = string.Empty;

            foreach (var item in order.Products)
            {
                productName += item.Name + " *" + (double)item.Quantity + "、";
            }
            productName = productName.Remove(productName.Length - 1);
            var keyword5 = (order.Status == OrderStatus.Distribution || order.Status == OrderStatus.DistributorReceipt) ? "待配送" : "已付款";

            msg.data = new
            {
                first    = new { value = $"流水号:   #{order.Identifier}      ¥{order.Price}" },
                keyword1 = new { value = productName },
                keyword2 = new { value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
                keyword3 = new { value = order.ReceiverAddress },
                keyword4 = new { value = "   " + order.GetUserCall() + "    " + order.Phone },
                keyword5 = new { value = keyword5, color = "#ff0000" },
                remark   = new { value = order.Remark }
            };
            var users = await Context.WxListenUsers.Where(a => a.BusinessId == order.BusinessId.Value).ToListAsync();

            if (users == null || users.Count == 0)
            {
                return;
            }
            var token = await GetTokenAsync(business.WeChatAppId, business.WeChatSecret);

            foreach (var item in users)
            {
                msg.touser = item.openid;
                var result = await WxHelper.SendEventMessageAsync(msg, token);

                var ret = JsonConvert.DeserializeObject <WxMessageReturn>(result);
                if (ret.errcode != 0)
                {
                    Log.Info($"公众号模版通知消息失败:[消息_{JsonConvert.SerializeObject(msg)},结果_{result}]");
                    await WechatHandlerErrorAsync(ret, msg);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// 公众号消息发送错误处理
        /// </summary>
        /// <param name="ret"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        private async Task WechatHandlerErrorAsync(WxMessageReturn ret, WxEventMessage msg)
        {
            if (ret.errcode == 43004)
            {
                // 用户已取消关注,则删除该用户
                var openId = msg.touser;
                var users  = await Context.WxListenUsers.Where(a => a.openid == openId).ToListAsync();

                if (users.Count > 0)
                {
                    Context.RemoveRange(users);
                    await Context.SaveChangesAsync();
                }
            }
        }
Пример #3
0
        /// <summary>
        /// 发送订单通知
        /// </summary>
        public static async Task <string> SendEventMessageAsync(WxEventMessage msg)
        {
            var token = await GetTokenAsync(WeChatAppId, WeChatSecret);

            var url = $"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={token}";

            using (var client = new HttpClient())
            {
                var p = new StringContent(JsonConvert.SerializeObject(msg));
                p.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                var result = await client.PostAsync(url, p);

                var content = await result.Content.ReadAsStringAsync();

                return(content);
            }
        }
Пример #4
0
        public async Task SendRefundMsgAsync(Order order)
        {
            var business = await GetAsync <Business>(order.BusinessId.Value);

            if (string.IsNullOrEmpty(business.RefundTemplateId))
            {
                return;
            }
            List <WxListenUser> users = await Context.WxListenUsers.Where(a => a.BusinessId == business.ID).ToListAsync();

            if (users == null || users.Count == 0)
            {
                return;
            }
            var msg = new WxEventMessage
            {
                template_id = business.RefundTemplateId,
                data        = new
                {
                    first    = new { value = $"退款订单编号-{order.OrderCode}" },
                    keyword1 = new { value = "¥" + order.Price.Value, color = "#ff0000" },
                    keyword2 = new { value = order.RefundReason, color = "#ff0000" },
                    keyword3 = new { value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
                    keyword4 = new { value = "线上" },
                    keyword5 = new { value = $"{order.ReceiverName} - {order.Phone}" },
                    remark   = new { value = "请尽快进入简单猫后台,订单管理 -> 实时订单 页处理。" }
                }
            };
            var token = await GetTokenAsync(business.WeChatAppId, business.WeChatSecret);

            foreach (var item in users)
            {
                msg.touser = item.openid;
                var result = await WxHelper.SendEventMessageAsync(msg, token);

                var ret = JsonConvert.DeserializeObject <WxMessageReturn>(result);
                if (ret.errcode != 0)
                {
                    Log.Info($"退款模版通知消息失败:[消息_{JsonConvert.SerializeObject(msg)},结果_{result}]");
                }
            }
        }
Пример #5
0
        /// <summary>
        /// 发送订单通知
        /// </summary>
        /// <param name="msg">消息内容</param>
        /// <param name="token">公众号访问token</param>
        /// <returns></returns>
        public static async Task <string> SendEventMessageAsync(WxEventMessage msg, string token)
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={token}";

            return(await RequestAsync(url, msg));
        }