/// <summary> /// 服务号通用的发送消息 /// </summary> /// <param name="strMsgType"></param> /// <param name="strContent"></param> /// <param name="lstContent"></param> /// <param name="strTags"></param> /// <param name="strOpenids"></param> /// <param name="PreviewOpenids"></param> /// <returns></returns> public static SendResult SendMsg(string strMsgType, string strContent, List <ArticleInfoView> lstContent, string strTags, string[] strOpenids, string[] PreviewOpenids, bool isKefu = false /*是否客服消息*/) { SendResult returnResult = null; WxJsonResult retCustom = null; var objConfig = WeChatCommonService.GetWeChatConfigByID(lstContent[0].AppId.Value); //log.Debug("strUser:{0} strDept:{1} strTags:{2} Msg:{3} APPID:{4}", strUser, strDept, strTags, strContent, iAppID); // string strToken = Innocellence.Weixin.MP.CommonAPIs.AccessTokenContainer.GetToken(objConfig.WeixinCorpId, objConfig.WeixinCorpSecret); switch (strMsgType) { case "text": returnResult = MPSendMessage(objConfig, lstContent[0].NewsInfo.NewsContent, Weixin.MP.GroupMessageType.text, strTags, strOpenids, PreviewOpenids, "", "", isKefu); break; case "news": //因为历史原因,news和mpnews对应反了.此处实际就是mpnews业务 List <NewsModel> articles = new List <NewsModel>(); string host = CommonService.GetSysConfig("Content Server", ""); if (host.EndsWith("/")) { host = host.Substring(0, host.Length - 1); } for (int i = 0; i < lstContent.Count; i++) { var item = lstContent[i]; var filePath = ""; if (0 == i) { //不存在就创建 filePath = WechatCommon.doGetFileCover(item.ImageCoverUrl, "_B"); } else { filePath = WechatCommon.doGetFileCover(item.ImageCoverUrl, "_T"); } var ret0 = Innocellence.Weixin.MP.AdvancedAPIs.MediaApi.UploadTemporaryMedia(objConfig.WeixinAppId, objConfig.WeixinCorpSecret, Weixin.MP.UploadMediaFileType.thumb, HttpRuntime.AppDomainAppPath + filePath); var picUrl = (host + filePath).Replace("\\", "/"); var url = string.Format("{0}/MPNews/ArticleInfo/WxDetail/{1}?wechatid={2}&isPreview={3}", host, item.Id, item.AppId, PreviewOpenids == null ? false : true); articles.Add(new NewsModel() { title = item.ArticleTitle, content = WechatCommonMP.ImageConvert(item.ArticleContent, objConfig.Id), thumb_url = picUrl, thumb_media_id = ret0.thumb_media_id, content_source_url = url }); } var ret = Innocellence.Weixin.MP.AdvancedAPIs.MediaApi.UploadTemporaryNews(objConfig.WeixinAppId, objConfig.WeixinCorpSecret, 10000, articles.ToArray()); returnResult = MPSendMessage(objConfig, ret.media_id, Weixin.MP.GroupMessageType.mpnews, strTags, strOpenids, PreviewOpenids, "", "", isKefu); break; case "mpnews": //因为历史原因,news和mpnews对应反了。此处实际就是news业务 List <Article> articles1 = new List <Article>(); string host1 = CommonService.GetSysConfig("Content Server", ""); if (host1.EndsWith("/")) { host1 = host1.Substring(0, host1.Length - 1); } for (int i = 0; i < lstContent.Count; i++) { var item = lstContent[i]; var filePath = ""; if (0 == i) { //不存在就创建 filePath = WechatCommon.doGetFileCover(item.ImageCoverUrl, "_B"); } else { filePath = WechatCommon.doGetFileCover(item.ImageCoverUrl, "_T"); } // var ret0 = Innocellence.Weixin.MP.AdvancedAPIs.MediaApi.UploadTemporaryMedia(objConfig.WeixinAppId, objConfig.WeixinCorpSecret, Weixin.MP.UploadMediaFileType.thumb, filePath); var picUrl = (host1 + filePath).Replace("\\", "/"); var url = string.Format("{0}/MPNews/ArticleInfo/WxDetail/{1}?wechatid={2}&isPreview={3}", host1, item.Id, item.AppId, PreviewOpenids == null ? false : true); articles1.Add(new Article() { Title = item.ArticleTitle, PicUrl = picUrl, Url = url, Description = item.ArticleComment }); } // var ret1 = Innocellence.Weixin.MP.AdvancedAPIs.MediaApi.UploadTemporaryNews(objConfig.WeixinAppId, objConfig.WeixinCorpSecret, 10000, articles1.ToArray()); // retCustom=SendMsgKF(strMsgType, objConfig,strOpenids, articles1); WxJsonResult retCustom1; foreach (var openId in strOpenids) { retCustom1 = CustomApi.SendNews(objConfig.WeixinAppId, objConfig.WeixinCorpSecret, openId, articles1); if (retCustom1.errcode != ReturnCode.请求成功) { retCustom = retCustom1; } else if (retCustom == null) { retCustom = retCustom1; } } returnResult = new SendResult() { errcode = retCustom.errcode, errmsg = retCustom.errmsg }; // returnResult = MPSendMessage(objConfig, ret1.media_id, Weixin.MP.GroupMessageType.mpnews, strTags, strOpenids, PreviewOpenids, "", ""); break; case "image": lstContent[0].NewsInfo.MediaId = WechatCommon.DoNewsInfo(lstContent[0].NewsInfo, objConfig, AutoReplyContentEnum.IMAGE, lstContent[0].NewsInfo.ImageContent); //WechatCommon.GetMediaInfo(AutoReplyContentEnum.IMAGE, lstContent[0].NewsInfo, objConfig.Id); returnResult = MPSendMessage(objConfig, lstContent[0].NewsInfo.MediaId, Weixin.MP.GroupMessageType.image, strTags, strOpenids, PreviewOpenids, "", "", isKefu); break; case "video": //WechatCommon.GetMediaInfo(AutoReplyContentEnum.VIDEO, lstContent[0].NewsInfo, lstContent[0].NewsInfo.AppId); lstContent[0].NewsInfo.MediaId = WechatCommon.DoNewsInfo(lstContent[0].NewsInfo, objConfig, AutoReplyContentEnum.VIDEO, lstContent[0].NewsInfo.VideoContent); if (!isKefu) { var ret2 = Innocellence.Weixin.MP.AdvancedAPIs.GroupMessageApi.GetOpenIdVideoMediaIdResult(objConfig.WeixinAppId, objConfig.WeixinCorpSecret, lstContent[0].NewsInfo.MediaId, lstContent[0].NewsInfo.NewsTitle, lstContent[0].NewsInfo.NewsComment); lstContent[0].NewsInfo.MediaId = ret2.media_id; lstContent[0].NewsInfo.MediaCreateTime = ret2.created_at; } MPSendMessage(objConfig, lstContent[0].NewsInfo.MediaId, Weixin.MP.GroupMessageType.video, strTags, strOpenids, PreviewOpenids, lstContent[0].NewsInfo.NewsTitle, lstContent[0].NewsInfo.NewsComment, isKefu); break; case "voice": //WechatCommon.GetMediaInfo(AutoReplyContentEnum.VOICE, lstContent[0].NewsInfo, lstContent[0].NewsInfo.AppId); lstContent[0].NewsInfo.MediaId = WechatCommon.DoNewsInfo(lstContent[0].NewsInfo, objConfig, AutoReplyContentEnum.VOICE, lstContent[0].NewsInfo.SoundSrc); returnResult = MPSendMessage(objConfig, lstContent[0].NewsInfo.MediaId, Weixin.MP.GroupMessageType.voice, strTags, strOpenids, PreviewOpenids, "", "", isKefu); break; } return(returnResult); }
// 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"); }