コード例 #1
0
        private List <ArticleInfoView> GetArticleList(List <NewsInfoView> newsList, bool isPreview, string strSendType = null, SearchUserMPView searchCondition = null, bool toDB = true)
        {
            List <ArticleInfoView> lstArticles = new List <ArticleInfoView>();

            int i = 0;

            var codeArt = Guid.NewGuid();

            newsList.ForEach(a => { a.Id = i++; a.NewsCode = codeArt.ToString(); });
            var content = JsonConvert.SerializeObject(newsList);


            foreach (var a in newsList)
            {
                var entity = a.ConvertToEntityArticle();
                entity.NewsInfo = a;
                // entity.UserName = User.Identity.Name;
                if (a.NewsCate.Equals("news", StringComparison.CurrentCultureIgnoreCase))
                {
                    if (a.materialId.HasValue && a.materialId.Value > 0)
                    {
                        entity.Id = a.materialId.Value;
                    }
                    entity.SecurityLevel = a.SecurityLevel;
                    if (!string.IsNullOrEmpty(strSendType))
                    {
                        switch (strSendType)
                        {
                        case "ToAll":
                        case "ByTag":
                            entity.ToTag = entity.Group.Value.ToString();
                            break;

                        case "ByOpenId":
                            string[] userOpenIds = _WechatMPUserService.GetUserBySearchCondition(searchCondition, AccountManageID).Select(u => u.OpenId).ToArray();
                            entity.ToUser = string.Join(",", userOpenIds);
                            break;

                        default:
                            break;
                        }
                    }
                }
                if (a.PostType == (int)MessagePostTypeEnum.定时推送)
                {
                    entity.ScheduleSendTime = DateTime.Parse(a.ScheduleSendTime.Date)
                                              .AddHours(a.ScheduleSendTime.Hour)
                                              .AddMinutes(a.ScheduleSendTime.Minute);
                    var canPass = false;
                    if (entity.ScheduleSendTime.HasValue)
                    {
                        var      now = DateTime.Now;
                        TimeSpan ts  = entity.ScheduleSendTime.Value - now;
                        canPass = ts.Minutes >= 30;
                    }
                    if (!canPass)
                    {
                        throw new Exception("定时推送时间必须比当前时间延后30分钟!");
                    }
                }
                entity.ArticleURL         = a.ArticleURL;
                entity.ArticleContentEdit = content;
                entity.ArticleType        = 1; //标记是消息
                entity.IsLike             = a.IsLike;
                entity.ShowComment        = a.ShowComment;
                entity.ShowReadCount      = a.ShowReadCount;
                entity.IsWatermark        = a.IsWatermark;
                entity.NoShare            = a.NoShare;
                entity.OrderID            = a.Id;
                entity.ArticleCode        = codeArt;

                if (a.PostType != (int)MessagePostTypeEnum.定时推送 && !isPreview)
                {
                    entity.ArticleStatus = "Published";
                    entity.PublishDate   = DateTime.Now;
                }
                else
                {
                    if (isPreview)
                    {
                        entity.PreviewStartDate = DateTime.Now;
                    }
                    entity.ArticleStatus = "Saved";
                }

                if (toDB)
                {
                    if (entity.Id == 0)
                    {
                        _objServiceArticle.InsertView(entity);
                    }
                    else
                    {
                        //通过选择素材进行的图文类型消息均更新到图文列表中,而不需要出现在消息列表中
                        if (a.NewsCate.Equals("news", StringComparison.CurrentCultureIgnoreCase))
                        {
                            entity.ArticleType = 0;
                        }
                        _objServiceArticle.UpdateView(entity);
                    }
                }
                lstArticles.Add(entity);
            }

            return(lstArticles);
        }
コード例 #2
0
        // private IArticleInfoService _articleInfoService = EngineContext.Current.Resolve<IArticleInfoService>();

        public void Execute()
        {
            log.Debug("TimerMessageJob Execute Start");

            //查询7分钟之内的待发消息
            DateTime dtStart = DateTime.Now.AddMinutes(-2);
            DateTime dtEnd   = DateTime.Now.AddSeconds(30);

            Expression <Func <ArticleInfo, bool> > predicate = (a) => a.IsDeleted == false && a.ArticleStatus != "Published" && a.ScheduleSendTime > dtStart && a.ScheduleSendTime < dtEnd;
            var list = articleInfoService.GetListWithContent <ArticleInfoView>(predicate, new Infrastructure.Utility.Data.PageCondition()
            {
                PageSize = 100, PageIndex = 1
            });

            log.Debug("TimerMessageJob Execute:{0}", list.Count);
            System.Net.IPAddress myIPAddress = (System.Net.IPAddress)System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName()).Where(aa => aa.AddressFamily.ToString() == "InterNetwork").ToList().FirstOrDefault();

            //已经发送成功的信息不会重新发布
            list.RemoveAll(a => lstSent.Exists(b => b == a.Id));

            //保存到已经发送列表
            lstSent.AddRange(list.Select(aa => aa.Id).ToArray());

            //消息分组
            var listArticle = list.GroupBy(a => a.ArticleCode);

            foreach (var a in listArticle)
            {
                //消息排序
                var lstTemp = a.ToList().OrderBy(c => c.OrderID).ToList();
                var content = JsonConvert.DeserializeObject <List <NewsInfoView> >(lstTemp[0].ArticleContentEdit);
                lstTemp.ForEach(b => b.NewsInfo = content.Find(c => c.Id == b.OrderID));

                // System.Net.IPAddress myIPAddress = (System.Net.IPAddress)System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName()).Where(aa => aa.AddressFamily.ToString() == "InterNetwork");

                //消息发送
                using (var transactionScope = new TransactionScope(TransactionScopeOption.Suppress,
                                                                   new TransactionOptions {
                    IsolationLevel = IsolationLevel.RepeatableRead
                }))
                {
                    Weixin.QY.AdvancedAPIs.Mass.MassResult result = null;
                    try
                    {
                        result = WechatCommon.SendMsgToUser(lstTemp[0].AppId.Value, ((WechatMessageLogType)lstTemp[0].ContentType.Value).ToString(), "", lstTemp);

                        //如果发送失败,把已经发送列表中数据去除,这样可以重新发送。
                        if (result.errcode != Weixin.ReturnCode_QY.请求成功)
                        {
                            lstSent.RemoveAll(bb => lstTemp.Exists(aa => aa.Id == bb));
                        }

                        foreach (var b in lstTemp)
                        {
                            b.PublishDate   = DateTime.Now;
                            b.ArticleStatus = "Published";

                            //记录IP
                            if (myIPAddress != null)
                            {
                                b.Previewers = myIPAddress.ToString();
                            }
                            articleInfoService.UpdateView(b, new List <string>()
                            {
                                "PublishDate", "ArticleStatus"
                            });
                        }

                        if (result != null && result.errcode == Weixin.ReturnCode_QY.请求成功)
                        {
                            transactionScope.Complete();
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex, "Auto SendMsg error");

                        //如果发送失败,把已经发送列表中数据去除,这样可以重新发送。
                        if (result == null || result.errcode != Weixin.ReturnCode_QY.请求成功)
                        {
                            lstSent.RemoveAll(bb => lstTemp.Exists(aa => aa.Id == bb));
                        }
                    }
                }
            }

            log.Debug("TimerMessageJob Execute");
        }