/// <summary> /// 从JObject对象解析 /// </summary> /// <param name="jo"></param> public override void Parse(JObject jo) { base.Parse(jo); JToken jt; if (jo.TryGetValue("content", out jt) && jt.Type == JTokenType.Object && ((JObject)jt).TryGetValue("news_item", out jt) && jt.Type == JTokenType.Array && ((JArray)jt).Count > 0) { JArray ja = (JArray)jt; Content = new MultiMediaArticle[ja.Count]; for (int i = 0; i < ja.Count; i++) { Content[i] = new MultiMediaArticle((JObject)ja[i]); } } else { Content = null; } }
/// <summary> /// 获取永久图文素材 /// </summary> /// <param name="userName">公众号</param> /// <param name="mediaId">图文素材的id</param> /// <param name="errorMessage">返回获取是否成功</param> /// <returns>返回图文消息;如果获取失败,返回null。</returns> public static MultiMediaArticle[] GetNews(string userName, string mediaId, out ErrorMessage errorMessage) { byte[] responseData = Get(userName, mediaId); if (responseData == null || responseData.Length == 0) { errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "获取永久图文素材失败。"); return(null); } string responseContent = HttpHelper.ResponseEncoding.GetString(responseData); if (ErrorMessage.IsErrorMessage(responseContent)) { errorMessage = ErrorMessage.Parse(responseContent); return(null); } else { JObject jo = JObject.Parse(responseContent); JToken jt; if (jo.TryGetValue("news_item", out jt)) { errorMessage = new ErrorMessage(ErrorMessage.SuccessCode, "获取永久图文素材成功。"); JArray ja = (JArray)jt; MultiMediaArticle[] articles = new MultiMediaArticle[ja.Count]; for (int i = 0; i < articles.Length; i++) { articles[i] = new MultiMediaArticle((JObject)ja[i]); } return(articles); } else { errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "获取永久图文素材失败。"); return(null); } } }
/// <summary> /// 上传图文消息 /// </summary> /// <param name="userName">公众号</param> /// <param name="articles">图文消息</param> /// <param name="errorMessage">返回上传是否成功</param> /// <returns>返回上传图文消息的结果;如果上传失败,返回null。</returns> public static MultiMediaUploadResult Upload(string userName, IEnumerable <MultiMediaArticle> articles, out ErrorMessage errorMessage) { if (articles == null) { errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "图文消息不能为空。"); return(null); } AccessToken token = AccessToken.Get(userName); if (token == null) { errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "获取许可令牌失败。"); return(null); } string url = string.Format(urlForUploadingArticles, token.access_token); string responseContent; HttpHelper.Request(url, out responseContent, WebRequestMethods.Http.Post, MultiMediaArticle.ToJson(articles)); return(ParseResult(responseContent, out errorMessage)); }
/// <summary> /// 新增永久图文素材 /// </summary> /// <param name="userName">公众号</param> /// <param name="articles">图文消息</param> /// <param name="errorMessage">返回新增是否成功</param> /// <returns>返回图文消息的媒体id;如果失败,返回null。</returns> public static string Add(string userName, IEnumerable <MultiMediaArticle> articles, out ErrorMessage errorMessage) { string responseContent = HttpHelper.RequestResponseContent(urlForAddingNews, userName, null, httpMethodForEditing, MultiMediaArticle.ToJson(articles)); return(GetMediaId(responseContent, out errorMessage)); }