public async Task <ApiResult <string> > PushMaterial([FromBody] ParmInt obj) { var res = new ApiResult <string>(); //根据公众号查询配置 var gzhModel = _settingService.GetModelAsync(m => m.Id == obj.id).Result.data; var token = WxTools.GetAccess(gzhModel.AppId, gzhModel.AppSecret); //提交素材的Url var url = string.Format("https://api.weixin.qq.com/cgi-bin/material/add_news?access_token={0}", token.access_token); //定义变量只接受同步成功的素材 var asynOkList = new List <WxMaterial>(); //定义标识,是否有素材没有上传成功 bool isUploadOk = true, asynOk = true; //根据公众号查询所有 var list = _meterialService.GetListAsync(m => m.WxId == obj.id && m.Position == 1, m => m.AddDate, DbOrderEnum.Desc).Result.data; if (list.Count > 0) { //到微信服务端获得thumb_media_id foreach (var item in list) { var articleList = new List <WxMeterArticle>(); item.Position = 2; if (!string.IsNullOrEmpty(item.TestJson)) { var resList = JsonConvert.DeserializeObject <List <WxMaterial> >(item.TestJson); foreach (var row in resList) { var fileExt = FileHelperCore.GetFileExtension(row.Img); var resultJson = WxTools.UploadFile(token.access_token, FileHelperCore.MapPath("/wwwroot" + row.Img), fileExt); if (resultJson.code == 200) { articleList.Add(new WxMeterArticle() { title = row.Title, thumb_media_id = resultJson.media_id, author = row.Author, digest = row.Summary, content = row.Content, content_source_url = row.Link }); } else { isUploadOk = false; } } } //开始发送到微信 var postStr = JsonConvert.SerializeObject(new { articles = articleList }); string resMewsJson = WxTools.PostResponse(url, postStr); if (resMewsJson.Contains("errcode")) { asynOk = false; } else { asynOkList.Add(item); } } } if (!isUploadOk) { res.statusCode = 500; res.message = "上传素材失败~"; return(res); } if (!asynOk) { res.statusCode = 500; res.message = "同步素材失败~"; return(res); } //只修改同步成功的素材 if (asynOkList.Count > 0) { await _meterialService.UpdateAsync(list); } return(res); }
public async Task <IActionResult> PushMaterial([FromBody] WxMaterial model) { var res = new ApiResult <string>(); var gzhModel = _settingService.GetModelAsync(m => m.Id == model.WxId).Result.data; var token = WxTools.GetAccess(gzhModel.AppId, gzhModel.AppSecret); await _meterialService.Add(model, null); var articleList = new List <WxMeterArticle>(); //定义标识,是否有素材没有上传成功 var isUploadOk = true; //根据公众号查询所有 var list = _meterialService.GetListAsync(m => m.WxId == model.WxId && m.Position == 1, m => m.AddDate, DbOrderEnum.Desc).Result.data; if (list.Count > 0) { //到微信服务端获得thumb_media_id foreach (var item in list) { item.Position = 2; if (!string.IsNullOrEmpty(item.TestJson)) { var resList = JsonConvert.DeserializeObject <List <WxMaterial> >(item.TestJson); foreach (var row in resList) { var fileExt = FileHelperCore.GetFileExtension(row.Img); var path = row.Img; if (!path.ToLower().StartsWith("http", StringComparison.Ordinal) && !path.ToLower().StartsWith("https", StringComparison.Ordinal)) { path = FileHelperCore.MapPath("/wwwroot" + row.Img); } var resultJson = WxTools.UploadFile(token.access_token, path, fileExt); if (resultJson.code == 200) { articleList.Add(new WxMeterArticle() { title = row.Title, thumb_media_id = resultJson.media_id, author = row.Author, digest = row.Summary, content = row.Content, content_source_url = row.Link }); } else { isUploadOk = false; } } } } } if (!isUploadOk) { res.statusCode = 500; res.message = "同步素材失败~"; return(Ok(res)); } var postStr = JsonConvert.SerializeObject(new { articles = articleList }); var url = string.Format("https://api.weixin.qq.com/cgi-bin/material/add_news?access_token={0}", token.access_token); string resMewsJson = WxTools.PostResponse(url, postStr); if (resMewsJson.Contains("errcode")) { res.statusCode = 500; res.message = "上传图文失败~"; return(Ok(res)); } //修改状态 await _meterialService.UpdateAsync(list); return(Ok(res)); }