/// <summary> /// 读取永久素材列表 /// </summary> /// <param name="type">类型 不能为news</param> /// <param name="offset">从全部素材的该偏移位置开始返回,0表示从第一个素材 默认0</param> /// <param name="count">返回素材的数量,取值在1到20之间,默认20</param> /// <returns></returns> public static PubMeterialResult GetMaterialList(PubMediaType type, int offset, int count = 20) { if (type == PubMediaType.news) { throw new Exception("news请使用GetMaterialNewsList"); } return(GetMaterialList <PubMeterialResult>(new { type = type.ToString(), offset = offset, count = count })); }
/// <summary> /// 新增临时素材 /// </summary> /// <param name="file">文件流</param> /// <param name="fileName">文件名称</param> /// <param name="type">媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)</param> /// <returns></returns> public static PubMediaResult AddMedia(string fileName, Stream file, PubMediaType type) { var url = string.Format(ADD_MEDIA_URL, PubInterface.AccessToken, type.ToString()); var content = PubInterface.BuildFromContent(fileName, file); var res = WebApiHelper.PostHttpContent <PubMediaResult>(url, content); return(res); }
/// <summary> /// 根据素材类型和id获取素材下拉列表 /// </summary> /// <param name="type"></param> /// <param name="id"></param> /// <returns></returns> private async Task <dynamic> GetMediaInfoByTypeId(PubMediaType type, string id) { // todo orm如果直接用linq x.type == type.ToString() 无法查询数据 var mediaType = type.ToString(); var media = await repo.FirstOrDefaultAsync <WxMediaShow>(x => x.Type == mediaType && x.MediaId == id); if (media != null) { var optTemp = new { id = media.MediaId, text = media.Title }; return(new List <dynamic> { optTemp }); } return(null); }
/// <summary> /// 保存同步的素材和下载 /// </summary> /// <param name="mType"></param> /// <param name="syncRes"></param> private void SyncMedias(PubMediaType mType, PubMeterialResult syncRes) { try { // 保存永久素材记录 var dtNow = DateTime.Now; var lstWxMedia = new List <WxMediaShow>(); var userName = currentUser.UserName; foreach (var media in syncRes.item) { // 判断是否存在 var exist = repo.FirstOrDefault <WxMediaShow>(x => x.MediaId == media.media_id); if (exist == null) { exist = new WxMediaShow(); exist.Creater = userName; exist.CreateTime = dtNow; exist.Title = media.name; } exist.MediaId = media.media_id; exist.Type = mType.ToString(); exist.WxUrl = media.url; exist.Updater = userName; exist.UpdateTime = dtNow; exist.IsDel = 0; repo.Save(exist); lstWxMedia.Add(exist); } // 下载附件 var upVirPath = UpPathHelper.GetUploadVirPath("wxmedia", dtNow); var upPhyPath = UpPathHelper.GetUploadPhyPath("wxmedia", dtNow); DownWxMedia(lstWxMedia, mType, upVirPath, upPhyPath); } catch (Exception ex) { logHelper.Error("SyncMedias:保存永久素材失败:" + ex.Message + " " + ex.StackTrace); } }
/// <summary> /// 新增永久素材 /// </summary> /// <param name="type"></param> /// <param name="fileName"></param> /// <param name="file"></param> /// <param name="title">视频时有效</param> /// <param name="introduction">视频时有效</param> /// <returns></returns> public static PubMediaResult AddMaterial(PubMediaType type, Stream file, string fileName, string title, string introduction) { MultipartFormDataContent form = new MultipartFormDataContent(); form.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");//重要,一定要标明 if (type == PubMediaType.video) { var video = "{\"title\":\"" + title + "\", \"introduction\":\"" + introduction + "\"}"; var videoContent = new StringContent(video); videoContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "\"description\"", }; form.Add(videoContent); } form.Add(PubInterface.BuildStreamContent(fileName, file)); var url = string.Format(ADD_MATERIAL_URL, PubInterface.AccessToken, type.ToString()); var res = WebApiHelper.PostHttpContent <PubMediaResult>(url, form); return(res); }