/// <summary> /// 微信图文推送。该函数不建议使用,建议使用PublishMessage函数代替。 /// </summary> /// <param name="iAppID"></param> /// <param name="strMsgType"></param> /// <param name="strUser"></param> /// <param name="strDept"></param> /// <param name="strTags"></param> /// <param name="strContent"></param> /// <param name="lstContent"></param> /// <returns></returns> public static MassResult SendMsg(int iAppID, string strMsgType, string strUser, string strDept, string strTags, string strContent, List <Article> lstContent) { MassResult objResult = null; int IsSec = 0; var objConfig = WeChatCommonService.GetWeChatConfig(iAppID); LogManager.GetLogger(typeof(WechatCommon)).Debug("strUser:{0} strDept:{1} strTags:{2} Msg:{3}", strUser, strDept, strTags, strContent); string strToken = AccessTokenContainer.TryGetToken(objConfig.WeixinCorpId, objConfig.WeixinCorpSecret); switch (strMsgType) { case "text": objResult = MassApi.SendText(strToken, strUser, strDept, strTags, iAppID.ToString(), strContent, IsSec); break; case "image": Dictionary <string, Stream> dic = new Dictionary <string, Stream>(); // for (int i = 0; i < Request.Files.Count; i++) { var stream = System.IO.File.OpenRead(HttpContext.Current.Server.MapPath("~/") + "/style/images/meteorshower.jpg"); dic.Add("meteorshower.jpg", stream); } var ret = MediaApi.Upload(strToken, UploadMediaFileType.file, dic, ""); objResult = MassApi.SendImage(strToken, strUser, strDept, strTags, iAppID.ToString(), ret.media_id, IsSec); break; case "news": objResult = MassApi.SendNews(strToken, strUser, strDept, strTags, iAppID.ToString(), lstContent, IsSec); break; case "file": Dictionary <string, Stream> dic1 = new Dictionary <string, Stream>(); for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++) { var stream = System.IO.File.OpenRead(HttpContext.Current.Server.MapPath("~/") + "/style/images/meteorshower.jpg"); dic1.Add("meteorshower.jpg", stream); } var ret1 = MediaApi.Upload(strToken, UploadMediaFileType.file, dic1, ""); objResult = MassApi.SendFile(strToken, strUser, strDept, strTags, iAppID.ToString(), ret1.media_id, IsSec); break; } return(objResult); }
//文件消息 public void SendFile(string filePath, string strAPPID, string strUserS = "@all") { try { if (strUserS == "") { return; } if (Qyinfo.IsUseWX == "Y") { Senparc.Weixin.QY.AdvancedAPIs.Media.UploadTemporaryResultJson md = MediaApi.Upload(GetToken(), Senparc.Weixin.QY.UploadMediaFileType.file, filePath); if (md.media_id != "") { MassApi.SendFile(GetToken(), strUserS, "", "", strAPPID, md.media_id); } } } catch { } }
public static MassResult SendMsgQY(int iAppID, string strMsgType, string strUser, string strDept, string strTags, string strContent, List <ArticleInfoView> lstContent, int IsSec, bool isPreview = false) { MassResult objResult = null; try { var objConfig = WeChatCommonService.GetWeChatConfigByID(iAppID); log.Warn("SendMsgQY strUser:{0} strDept:{1} strTags:{2} Msg:{3} APPID:{4} strMsgType:{5} IsSec:{6} MsgCount:{7}", strUser, strDept, strTags, strContent, iAppID, strMsgType, IsSec, lstContent.Count); string strToken = (objConfig.IsCorp != null && !objConfig.IsCorp.Value) ? Innocellence.Weixin.MP.CommonAPIs.AccessTokenContainer.GetToken(objConfig.WeixinCorpId, objConfig.WeixinCorpSecret) : AccessTokenContainer.TryGetToken(objConfig.WeixinCorpId, objConfig.WeixinCorpSecret); var news = lstContent[0].NewsInfo; //保密图文消息需要用mpnews发送 strMsgType = strMsgType == "news" && IsSec == 1 ? "mpnews" : strMsgType; switch (strMsgType) { case "text": objResult = MassApi.SendText(strToken, strUser, strDept, strTags, objConfig.WeixinAppId.ToString(), lstContent[0].ArticleContent, IsSec); break; case "image": news.MediaId = DoNewsInfo(news, objConfig, AutoReplyContentEnum.IMAGE, news.ImageContent); objResult = MassApi.SendImage(strToken, strUser, strDept, strTags, objConfig.WeixinAppId.ToString(), news.MediaId, IsSec); break; case "news": #region news var wechatBaseUrl = CommonService.GetSysConfig("WeChatUrl", ""); var lstArticle = new List <Article>(); int i = 0; foreach (var objModel in lstContent) { if (i == 0) //位置不同,缩略图的比例不一样 { objModel.ImageCoverUrl = doGetFileCover(objModel.ImageCoverUrl, "_B"); } else { objModel.ImageCoverUrl = doGetFileCover(objModel.ImageCoverUrl, "_T"); } i++; lstArticle.Add(new Article() { Title = objModel.ArticleTitle, Description = objModel.ArticleComment, // PicUrl = aiv.ThumbImageId == null ? wechatBaseUrl+"/Content/img/LogoRed.png" : string.Format("{0}/Common/PushFile?id={1}&FileName={2}", wechatBaseUrl, aiv.ThumbImageId, aiv.ThumbImageUrl), //PicUrl = objModel.ImageCoverUrl == null ? wechatBaseUrl + "Content/img/LogoRed.png" : string.Format("{0}{1}", wechatBaseUrl, objModel.ImageCoverUrl), PicUrl = string.Format("{0}{1}", wechatBaseUrl, objModel.ImageCoverUrl).Replace("/\\", "/").Replace("\\", "/"), Url = string.Format("{0}/News/ArticleInfo/WxDetail/{1}?wechatid={2}&isPreview={3}", wechatBaseUrl, objModel.Id, objModel.AppId, isPreview) }); } //var lstArticle = lstContent.Select(objModel => new Article() //{ // Title = objModel.ArticleTitle, // Description = objModel.ArticleComment, // // PicUrl = aiv.ThumbImageId == null ? wechatBaseUrl+"/Content/img/LogoRed.png" : string.Format("{0}/Common/PushFile?id={1}&FileName={2}", wechatBaseUrl, aiv.ThumbImageId, aiv.ThumbImageUrl), // //PicUrl = objModel.ImageCoverUrl == null ? wechatBaseUrl + "Content/img/LogoRed.png" : string.Format("{0}{1}", wechatBaseUrl, objModel.ImageCoverUrl), // PicUrl = string.Format("{0}{1}", wechatBaseUrl, objModel.ImageCoverUrl).Replace("/\\", "/").Replace("\\", "/"), // Url = string.Format("{0}/News/ArticleInfo/WxDetail/{1}?wechatid={2}&isPreview={3}", wechatBaseUrl, objModel.Id, objModel.AppId, isPreview) //}).ToList(); objResult = MassApi.SendNews(strToken, strUser, strDept, strTags, objConfig.WeixinAppId.ToString(), lstArticle, IsSec); #endregion break; case "mpnews": #region mpnews var wechatBaseUrl1 = CommonService.GetSysConfig("WeChatUrl", ""); var lstMpArticle = new List <MpNewsArticle>(); int ii = 0; foreach (var objModel in lstContent) { if (ii == 0) //位置不同,缩略图的比例不一样 { objModel.ImageCoverUrl = doGetFileCover(objModel.ImageCoverUrl, "_B"); } else { objModel.ImageCoverUrl = doGetFileCover(objModel.ImageCoverUrl, "_T"); } ii++; lstMpArticle.Add(new MpNewsArticle() { title = objModel.ArticleTitle, digest = objModel.ArticleComment, content = objModel.ArticleContent, author = objModel.UpdatedUserID, show_cover_pic = "1", thumb_media_id = GetMediaId(objModel.ImageCoverUrl, strToken), // PicUrl = aiv.ThumbImageId == null ? wechatBaseUrl+"/Content/img/LogoRed.png" : string.Format("{0}/Common/PushFile?id={1}&FileName={2}", wechatBaseUrl, aiv.ThumbImageId, aiv.ThumbImageUrl), //PicUrl = objModel.ImageCoverUrl == null ? wechatBaseUrl + "Content/img/LogoRed.png" : string.Format("{0}{1}", wechatBaseUrl, objModel.ImageCoverUrl), // = string.Format("{0}{1}", wechatBaseUrl, objModel.ImageCoverUrl), content_source_url = string.Format("{0}/News/ArticleInfo/WxDetail/{1}?wechatid={2}&isPreview={3}", wechatBaseUrl1, objModel.Id, objModel.AppId, isPreview) }); } objResult = MassApi.SendMpNews(strToken, strUser, strDept, strTags, objConfig.WeixinAppId.ToString(), lstMpArticle, IsSec); #endregion break; case "video": //WechatCommon.GetMediaInfo(AutoReplyContentEnum.VIDEO, news, news.AppId); news.MediaId = DoNewsInfo(news, objConfig, AutoReplyContentEnum.VIDEO, news.VideoContent); objResult = MassApi.SendVideo(strToken, strUser, strDept, strTags, objConfig.WeixinAppId.ToString(), news.MediaId, news.NewsTitle, news.NewsComment, IsSec); //NewsToAttachments(news, "video", news.VideoContent); break; case "file": // WechatCommon.GetMediaInfo(AutoReplyContentEnum.FILE, news, news.AppId); news.MediaId = DoNewsInfo(news, objConfig, AutoReplyContentEnum.FILE, news.FileSrc); objResult = MassApi.SendFile(strToken, strUser, strDept, strTags, objConfig.WeixinAppId.ToString(), news.MediaId, IsSec); // NewsToAttachments(news, "file", news.FileSrc); break; case "voice": // WechatCommon.GetMediaInfo(AutoReplyContentEnum.VOICE, news, news.AppId); news.MediaId = DoNewsInfo(news, objConfig, AutoReplyContentEnum.VOICE, news.SoundSrc); objResult = MassApi.SendVoice(strToken, strUser, strDept, strTags, objConfig.WeixinAppId.ToString(), news.MediaId, IsSec); //NewsToAttachments(news, "voice", news.SoundSrc); break; } } catch (Exception e) { log.Error(e); throw; } //在更新MsgLog时保持原来的错误处理, 即不进行任何处理 finally { UpdateMsgLog(lstContent, objResult); } return(objResult); }
/// <summary> /// 发送文件; /// </summary> /// <param name="userOpenId"></param> /// <param name="filepath"></param> public MassResult SendFileToUser(string userOpenId, string filepath) { var uploadResult = UploadFile(filepath); return(MassApi.SendFile(AccessTokenOrAppKey, Config.AppId, uploadResult.media_id, CorrectUserOpenId(userOpenId))); }