/// <summary> /// 发送微信消息.自动 /// </summary> /// <param name="iAppID"></param> /// <param name="strMsgType"></param> /// <param name="strContent"></param> /// <param name="lstContent"></param> /// <returns></returns> public static MassResult SendMsgToUser(int iAppID, string strMsgType, string strContent, List <ArticleInfoView> lstContent) { MassResult ret; var objConfig = WeChatCommonService.GetWeChatConfigByID(iAppID); if (objConfig.IsCorp.HasValue && objConfig.IsCorp.Value) { ret = SendMsg(iAppID, strMsgType, lstContent[0].ToUser.Replace(",", "|"), lstContent[0].ToDepartment.Replace(",", "|"), lstContent[0].ToTag.Replace(",", "|"), strContent, lstContent); } else { var retMP = WechatCommonMP.SendMsgMP(lstContent, new WeChat.Domain.ViewModelFront.SearchUserMPView() { Group = lstContent[0].NewsInfo.Group, City = lstContent[0].NewsInfo.City, Province = lstContent[0].NewsInfo.Province, Sex = lstContent[0].NewsInfo.Sex }, null); ret = new MassResult() { errcode = (ReturnCode_QY)retMP.errcode, errmsg = retMP.errmsg }; } return(ret); }
public static MassResult SendMsg(int iAppID, string strMsgType, string strUser, string strDept, string strTags, string strContent, List <ArticleInfoView> lstContent, bool isPreview = false) { var objConfig = WeChatCommonService.GetWeChatConfigByID(iAppID); if (objConfig.IsCorp.HasValue && !objConfig.IsCorp.Value) { var ret = WechatCommonMP.SendMsg(strMsgType, strContent, lstContent, strTags, strUser.Split(','), null); return(new MassResult() { errmsg = ret.errmsg, errcode = (ReturnCode_QY)ret.errcode }); } else { int isSafe = 0; if (lstContent != null && lstContent.Count > 0) { if (lstContent[0].NewsInfo != null && lstContent[0].NewsInfo.isSecurityPost.HasValue && lstContent[0].NewsInfo.isSecurityPost.Value) { isSafe = 1; } } return(SendMsgQY(iAppID, strMsgType, strUser, strDept, strTags, strContent, lstContent, isSafe, isPreview)); } }
/// <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); }