public static SendResult SendMsgMP(List <ArticleInfoView> lstContent, SearchUserMPView searchCondition, string[] PreviewOpenids /*Preview use*/, bool isKefu = false) { string strTags = ""; //ToAll string[] userOpenIds = null; if (searchCondition.Group == null) { searchCondition.Group = -2; } if (searchCondition.Sex < 0 && searchCondition.Province == "-1") { if (searchCondition.Group == -2) { // ToAll strTags = "-1"; } else { // Send By Tag strTags = searchCondition.Group.ToString(); } } else { // Send By OpenId IWechatMPUserService _WechatMPUserService = EngineContext.Current.Resolve <IWechatMPUserService>(); var objConfig = WeChatCommonService.GetWeChatConfigByID(lstContent[0].AppId.Value); userOpenIds = _WechatMPUserService.GetUserBySearchCondition(searchCondition, objConfig.AccountManageId.Value).Select(u => u.OpenId).ToArray(); } string strType = ((WechatMessageLogType)lstContent[0].ContentType).ToString(); return(SendMsg(strType, "", lstContent, strTags, userOpenIds, PreviewOpenids, isKefu)); }
public ActionResult WechatServiceSendMessage(List <NewsInfoView> newsList, bool isPreview = false) { SysWechatConfig wechat = WeChatCommonService.lstSysWeChatConfig.Find(p => p.AccountManageId == AccountManageID && !p.IsCorp.Value); if (wechat == null) { return(Json(new { results = new { Data = 500 } })); } try { NewsInfoView news = newsList[0]; if ("video".Equals(news.NewsCate)) { news.ImageContent = SaveBase64ImageToServer(news.ImageContent); } string strSendType = string.Empty; SearchUserMPView searchCondition = new SearchUserMPView { Group = news.Group, Province = news.Province, City = news.City, Sex = news.Sex }; using (var transactionScope = new TransactionScope(TransactionScopeOption.Suppress, new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead })) { SendResult returnResult = null; List <NewsModel> articles = new List <NewsModel>(); var lstArticles = GetArticleList(newsList, isPreview, strSendType, searchCondition); if (!isPreview && news.PostType == (int)MessagePostTypeEnum.定时推送) { transactionScope.Complete(); } else { string[] openids = null; if (isPreview) { openids = news.SendToPerson; } //发送消息 returnResult = WechatCommonMP.SendMsgMP(lstArticles, searchCondition, openids); if (returnResult.errcode == Weixin.ReturnCode.请求成功) { transactionScope.Complete(); } } } } catch (Exception e) { _Logger.Error(e, "An error occurred while sending mp news."); // _Logger.Error(e.ToString()); return(Json(new { results = new { Data = 500 } })); } return(Json(new { results = new { Data = 200 } })); }
public List <WechatMPUserView> GetUserBySearchCondition(SearchUserMPView searchCondition, int AccountManageId) { if (searchCondition == null) { return(new List <WechatMPUserView> { }); } Expression <Func <WechatMPUser, bool> > predicate = GetExpressionBySearchUserMPView(searchCondition, AccountManageId); var users = Repository.Entities.Where(predicate).ToList().Select(a => (WechatMPUserView) new WechatMPUserView().ConvertAPIModel(a)).ToList(); //bool cityNotSearch = string.IsNullOrWhiteSpace(searchCondition.City) || (!string.IsNullOrWhiteSpace(searchCondition.City) && searchCondition.City == "-1"); //bool provinceNotSearch = string.IsNullOrWhiteSpace(searchCondition.Province) || (!string.IsNullOrWhiteSpace(searchCondition.Province) && searchCondition.Province == "-1"); //var users = Repository.Entities.Where(u => (!u.IsCanceled) // && (u.AccountManageId == AccountManageId) // && ((searchCondition.Group != null && searchCondition.Group.Value > 0) ? u.TagIdList == searchCondition.Group.ToString() : true) // && ((searchCondition.Sex != null && (searchCondition.Sex.Value == 1 || searchCondition.Sex.Value == 2 || searchCondition.Sex.Value == 0)) ? u.Sex == searchCondition.Sex.Value : true) // && (cityNotSearch ? true : (u.City.Equals(searchCondition.City) || searchCondition.City.Contains(u.City))) // && (provinceNotSearch ? true : (u.Province.Equals(searchCondition.Province) || searchCondition.Province.Contains(u.Province))) // && (searchCondition.Group != null && searchCondition.Group.Value == -1) ? (u.TagIdList == null || string.IsNullOrEmpty(u.TagIdList)) : true) // .ToList().Select(a => (WechatMPUserView)new WechatMPUserView().ConvertAPIModel(a)).ToList(); return(users); }
private Expression <Func <WechatMPUser, bool> > GetExpressionBySearchUserMPView(SearchUserMPView searchCondition, int accountManageId) { Expression <Func <WechatMPUser, bool> > predicate = p => !p.IsCanceled && p.AccountManageId == accountManageId; bool cityNotSearch = string.IsNullOrWhiteSpace(searchCondition.City) || (!string.IsNullOrWhiteSpace(searchCondition.City) && searchCondition.City == "-1"); bool provinceNotSearch = string.IsNullOrWhiteSpace(searchCondition.Province) || (!string.IsNullOrWhiteSpace(searchCondition.Province) && searchCondition.Province == "-1"); //选择分组 if (null != searchCondition.Group) { //-1:未分组, -2:所有用户 if (searchCondition.Group.Value < 0) { //未分组 if (searchCondition.Group.Value == -1) { predicate = predicate.AndAlso(u => u.TagIdList == null || string.IsNullOrEmpty(u.TagIdList.Trim())); } } //选择指定分组 else if (searchCondition.Group.Value > 0) { predicate = predicate.AndAlso(user => user.TagIdList.Equals(searchCondition.Group.Value.ToString())); } } //选择城市 if (!cityNotSearch) { predicate = predicate.AndAlso(user => searchCondition.City.Contains(user.City)); } //选择省 if (!provinceNotSearch) { predicate = predicate.AndAlso(user => searchCondition.Province.Contains(user.Province)); } //-1:不限制性别, 1:男, 2:女, 0:未知性别 if (null != searchCondition.Sex && searchCondition.Sex.Value > -1) { predicate = predicate.AndAlso(user => user.Sex == searchCondition.Sex.Value); } return(predicate); }
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); }