///// <summary> ///// 发送微信消息 ///// </summary> ///// <param name="strMsgType">text,image,news</param> ///// <param name="strContent"></param> ///// <param name="lstContent"></param> ///// <returns></returns> //public static SendResult SendMsg(int iAppID, string strMsgType, string strContent, List<Article> lstContent) //{ // //MassResult objResult = null; // string strDept = "", strUser = "", strTags = ""; // // int IsSec = 0; // GetAppInfoResult Ret = GetUserOrDept(iAppID); // Ret.allow_userinfos.user.Where(a => a.status == "1").ToList().ForEach(a => { strUser += "|" + a.userid; }); // if (strUser.Length > 0) { strUser = strUser.Substring(1); } // strDept = string.Join(",", Ret.allow_partys.partyid); // strTags = string.Join(",", Ret.allow_tags.tagid); // return SendMsg(iAppID, strMsgType, strUser, strDept, strTags, strContent, lstContent); //} //public static GetAppInfoResult GetUserOrDept(int iAppID) //{ // var objConfig = WeChatCommonService.GetWeChatConfigByID(iAppID); // string strToken = AccessTokenContainer.TryGetToken(objConfig.WeixinCorpId, objConfig.WeixinCorpSecret); // return AppApi.GetAppInfo(strToken, int.Parse( objConfig.WeixinAppId)); // // Ret.allow_userinfos //} //public static List<Article> SendNews(List<NewsInfo> lst) //{ // var lstContent = new List<Article>(); // Article art = new Article(); // NewsInfo news = lst.Find(a => a.LanguageCode == ConstData.LAN_CN); // NewsInfo newsEN = lst.Find(a => a.LanguageCode == ConstData.LAN_EN); // art.Title = newsEN.NewsTitle + news.NewsTitle; // art.Description =newsEN.NewsContent+"\n"+news.NewsContent; // art.PicUrl = string.Format("{0}Common/file?Id={1}&filename={2}&ImgType=1", // WebConfigurationManager.AppSettings["WebUrl"], news.Id, news.ImageName); // art.Url = string.Format("{0}NewsInfo/Detail?NewsCode={1}", WebConfigurationManager.AppSettings["WebUrl"], news.NewsCode); // // art.Description = art.PicUrl; // lstContent.Add(art); // return lstContent; //} //public static List<Article> SendCourse(List<TrainingCourse> lst) //{ // var lstContent = new List<Article>(); // Article art = new Article(); // TrainingCourse news = lst.Find(a => a.LanguageCode == ConstData.LAN_CN); // TrainingCourse newsEN = lst.Find(a => a.LanguageCode == ConstData.LAN_EN); // art.Title = newsEN.CourseName + news.CourseName; // art.Description = newsEN.CourseComment + "\n" + news.CourseComment; // art.PicUrl = "Common/file?Id=" + news.Id + "&filename=/Content/img/Course1.png&ImgType=1"; // art.Url = string.Format("{0}/Course/Detail?CourseCode={1}", WebConfigurationManager.AppSettings["WebUrl"], news.CourseCode); // lstContent.Add(art); // return lstContent; //} /// <summary> /// 服务号消息内容中不能有外链的图片,需要转换 /// </summary> /// <param name="strContent"></param> /// <param name="iAPPID"></param> /// <returns></returns> public static string ImageConvert(string strContent, int iAPPID) { List <string> lst = new List <string>(); string regex = @"(<img.*?/>)"; Regex listRegex = new Regex(regex, RegexOptions.Multiline | RegexOptions.IgnoreCase); //得到匹配的数据集合 MatchCollection mc = listRegex.Matches(strContent); foreach (Match a in mc) { var href = Regex.Match(a.Value, "src=\"(.*?)\""); if (href == null || href.Groups.Count < 2) { continue; } if (lst.Contains(href.Groups[1].Value)) { continue; } lst.Add(href.Groups[1].Value); var file = href.Groups[1].Value.Replace(CommonService.GetSysConfig("Content Server", ""), ""); var config = WeChatCommonService.GetWeChatConfigByID(iAPPID); var ret = MediaApi.UploadImg(config.WeixinAppId, config.WeixinCorpSecret, (HttpContext.Current == null ? HttpRuntime.AppDomainAppPath : HttpContext.Current.Request.PhysicalApplicationPath) + file, 10000 * 60); strContent = strContent.Replace(href.Groups[1].Value, ret.url); } return(strContent); }
/// <summary> /// 上传永久素材 主要用到MediaApi这个接口 /// </summary> /// <returns></returns> public UploadImgResult UploadImg() { var accessToken = AccessTokenContainer.TryGetAccessToken(AppId, AppSecret); string imgFilePath = Server.MapPath("~/logo.png"); return(MediaApi.UploadImg(accessToken, imgFilePath)); }
public UploadImgResult UploadImg(string path) { UploadImgResult uploadImgResult = null; string appId = WxOperatorProvider.Provider.GetCurrent().AppId; uploadImgResult = MediaApi.UploadImg(appId, path); return(uploadImgResult); }
public static UploadImgResult UploadImg(string accessTokenOrAppId, string src) { if (src.StartsWith("https://mmbiz.qpic.cn/") || src.StartsWith("http://mmbiz.qpic.cn/")) { return(null); } if (src.StartsWith("http")) { return(ApiHandlerWapper.TryCommonApi(accessToken => { string url = $"{"https"}://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={accessToken.AsUrlData()}"; var fileBytes = HttpHelper.HttpDownload(src); return Post.GetResult <UploadImgResult>(HttpHelper.HttpUpload(url, fileBytes, Path.GetFileName(src), "media")); }, accessTokenOrAppId)); } return(MediaApi.UploadImg(accessTokenOrAppId, App.GetLocalResource(src))); }