public JsonResult GetServerMaterial([FromBody] ParmInt obj) { var gzhModel = _settingService.GetModelAsync(m => m.Id == obj.id).Result.data; var token = WxTools.GetAccess(gzhModel.AppId, gzhModel.AppSecret); var list = WxTools.GetMediaList(token.access_token); return(Json(list)); }
public async Task <IActionResult> GetServerMaterial([FromBody] ParmInt obj) { var gzhModel = await _settingService.GetModelAsync(m => m.Id == obj.id); var token = WxTools.GetAccess(gzhModel.data.AppId, gzhModel.data.AppSecret); var list = WxTools.GetMediaList(token.access_token); return(Ok(list)); }
public async Task <IActionResult> PushMenu([FromBody] ParmInt obj) { //MemoryCacheService.Default.RemoveCache("WinXinAccessToken"); var res = new ApiResult <string>(); //获得公众号配置 var model = await _settingService.GetModelAsync(m => m.Id == obj.id); //获得access_taken var token = WxTools.GetAccess(model.data.AppId, model.data.AppSecret); var dbMenu = JsonConvert.DeserializeObject <List <WxButton> >(model.data.MenuJson); foreach (var item in dbMenu) { item.sub_button = item.sub_button.Count > 0 ? item.sub_button : null; if (item.type == "0" && !string.IsNullOrEmpty(item.url)) { item.type = "view"; } else if (item.type == "1" && !string.IsNullOrEmpty(item.media_id)) { item.type = "media_id"; item.url = null; } else { item.type = null; item.url = null; } if (item.sub_button != null) { foreach (var row in item.sub_button) { if (row.type == "0" && !string.IsNullOrEmpty(row.url)) { row.type = "view"; } else if (row.type == "1" && !string.IsNullOrEmpty(row.media_id)) { row.type = "media_id"; row.url = null; } else { row.type = null; row.url = null; } } } } JsonSerializerSettings jsetting = new JsonSerializerSettings(); jsetting.NullValueHandling = NullValueHandling.Ignore; var body = JsonConvert.SerializeObject(new WxPushButton() { button = dbMenu }, jsetting); var wxres = WxTools.PushMenu(token.access_token, body); res.message = wxres.errmsg; res.statusCode = wxres.errcode == 0 ? 200 : wxres.errcode; ////构建菜单 //var wxbutton = new List<WxButton>(); //var AsubButton = new List<WxSubButton>(); //AsubButton.Add(new WxSubButton() //{ // name = "返回文本", // type= "click", // key = "V1001_TODAY_MUSIC" //}); //var BsubButton = new List<WxSubButton>(); //BsubButton.Add(new WxSubButton() //{ // name = "Url跳转", // type= "view", // url = "http://h5.feiyit.com/" //}); //wxbutton.Add(new WxButton() { // name = "菜单A", // sub_button = AsubButton //}); //wxbutton.Add(new WxButton() //{ // name = "菜单B", // sub_button = BsubButton //}); //wxbutton.Add(new WxButton() //{ // name = "菜单C", // type = "click", // key = "V1001_GOOD" //}); ////过滤空值不序列化 //JsonSerializerSettings jsetting = new JsonSerializerSettings(); //jsetting.NullValueHandling = NullValueHandling.Ignore; //var body = JsonConvert.SerializeObject(new WxPushButton() {button=wxbutton }, jsetting); //var wxres = WxTools.PushMenu(token.access_token,body); //res.message = wxres.errmsg; //res.statusCode = wxres.errcode == 0 ? 200 : wxres.errcode; return(Ok(res)); }
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)); }
public void OnGet() { token = WxTools.GetAccess("wx7797e2efb5dc9502", "9bfed87a0157250c26479a084c207b12"); var tests = WxTools.GetMediaList(token.access_token); }