if (Session["imgmedia_id"] != null) { WxSuCaiService wscs = new WxSuCaiService(); WxSuCaiInfo wscinfo = wscs.GetWxSuCaiInfo(Session["imgmedia_id"].ToString()); if(wscinfo!=null) { this.ImgTuWen.ImageUrl = "~/WeiXinImg/" + wscinfo.SuCaiUrl.ToString(); this.ImgTuWen2.ImageUrl = "~/WeiXinImg/" + wscinfo.SuCaiUrl.ToString(); this.ImgTuWen2.Visible = true; Session["imgmedia_id"] = wscinfo.media_ID.ToString();//图片的media_id Session["fileNameimg"] = wscinfo.SuCaiUrl.ToString();//图片的文件名称 } }
最后新建图文信息的效果图如下:
官方后台如下:
关于编辑图文信息的关键代码如下:
/// <summary> /// 绑定事件 /// </summary> /// <param></param> /// <param></param> protected void DLMpNewsList_ItemDataBound(object sender, DataListItemEventArgs e) { if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem) { LinkButton LinkBtnDeleteSucai = e.Item.FindControl("LinkBtnDeleteSucai") as LinkButton; LinkBtnDeleteSucai.Attributes.Add("OnClick","return confirm('您确定删除该图文素材???删除后将和微信官网同步删除!!')"); HyperLink HyperLinkEdit = e.Item.FindControl("HyperLinkEdit") as HyperLink; HyperLinkEdit.Attributes.Add("OnClick", "return confirm('即将进入编辑模式!!是否执行下一步操作??')"); Label lbmedia_ID = e.Item.FindControl("lbmedia_ID") as Label; HyperLinkEdit.NavigateUrl = "WxNewTuWen.aspx?media_id=" + lbmedia_ID.Text.ToString();//把图文消息的media_id传参到新建图文界面 } }
新建图文页面关键代码如下:
if(!Page.IsPostBack) { ///编辑模式 if (Request.QueryString["media_id"] != null) { string media_id = Request.QueryString["media_id"].ToString(); Session["sucaimedia_id"] = media_id; WxSuCaiService wscs = new WxSuCaiService(); WxSuCaiInfo wscinfo = wscs.GetWxSuCaiInfo(media_id); if (wscinfo != null) { this.txttuwen_title.Value = wscinfo.SuCaiTitle.ToString(); if (wscinfo.SuCaiTitle.ToString().Length > 15) { this.biaoti_yulan.InnerText = wscinfo.SuCaiTitle.ToString().Substring(0, 15) + "..."; } else { this.biaoti_yulan.InnerText = wscinfo.SuCaiTitle.ToString(); } this.txttuwen_author.Value = wscinfo.SuCaiauthor.ToString(); this.txtzhaiyao.InnerText = wscinfo.SuCaiDigest.ToString(); this.ImgTuWen.ImageUrl = "~/WeiXinImg/" + wscinfo.SuCaiUrl.ToString(); this.ImgTuWen2.ImageUrl = "~/WeiXinImg/" + wscinfo.SuCaiUrl.ToString(); this.ImgTuWen2.Visible = true; Session["imgmedia_id"] = wscinfo.SuCaithumb_media_id.ToString(); this.LinkBtnDeleteImg.Visible = true; if (!String.IsNullOrWhiteSpace(wscinfo.SuCaicontent_source_url.ToString())) { this.txtYuanWenUrl.Text = wscinfo.SuCaicontent_source_url.ToString(); this.txtYuanWenUrl.Visible = true; this.CheckYuanWen.Checked = true; } this.txtYuanWenUrl.Text = wscinfo.SuCaicontent_source_url.ToString(); this.tbContent.InnerText = wscinfo.SuCaicontent.ToString(); if (wscinfo.SuCaishow_cover_pic.ToString().Equals("1")) { this.CheckFengMianShow.Checked = true; } else { this.CheckFengMianShow.Checked = false; } } } }
编辑提交关键代码如下:
/// <summary> /// 保存图文素材和修改按钮公用 /// </summary> /// <param></param> /// <param></param> protected void LinkBtnSaveYongjiu_Click(object sender, EventArgs e) { //非空验证 if (String.IsNullOrWhiteSpace(this.txttuwen_title.Value.ToString())) { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入图文标题!');", true); return; } if (this.ImgTuWen2.ImageUrl.ToString().Equals("")) { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('必须上传一张图片!');", true); return; } if (String.IsNullOrWhiteSpace(this.tbContent.InnerText.ToString())) { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入正文内容!');", true); return; } //对各项进行赋值 WeiXinServer wxs = new WeiXinServer(); ///从缓存读取accesstoken string Access_token = Cache["Access_token"] as string; if (Access_token == null) { //如果为空,重新获取 Access_token = wxs.GetAccessToken(); //设置缓存的数据7000秒后过期 Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration); } string Access_tokento = Access_token.Substring(17, Access_token.Length - 37); //根据session判断media_id是否为空,也可根据request.queryString["media_id"]进行判断是否为空 if (Session["sucaimedia_id"] != null) { //执行更新操作 //{ // "media_id":MEDIA_ID, // "index":INDEX, // "articles": { // "title": TITLE, // "thumb_media_id": THUMB_MEDIA_ID, // "author": AUTHOR, // "digest": DIGEST, // "show_cover_pic": SHOW_COVER_PIC(0 / 1), // "content": CONTENT, // "content_source_url": CONTENT_SOURCE_URL // } //} string isshow_cover_pic = ""; if (this.CheckFengMianShow.Checked) { isshow_cover_pic = "1"; } else { isshow_cover_pic = "0"; } string description = NoHTML(this.tbContent.InnerText.ToString()); string postData = "{\"media_id\":\"" + Session["sucaimedia_id"].ToString() + "\",\"index\":\"0\" ,\"articles\":{\"title\":\"" + this.txttuwen_title.Value.ToString() + "\",\"thumb_media_id\":\"" + Session["imgmedia_id"].ToString() + "\",\"author\":\"" + this.txttuwen_author.Value.ToString() + "\",\"digest\":\"" + this.txtzhaiyao.InnerText.ToString() + "\",\"show_cover_pic\":\"" + isshow_cover_pic + "\",\"content\":\"" + description + "\",\"content_source_url\":\"" + this.txtYuanWenUrl.Text.ToString() + "\"}}"; ///修改永久图文素材 string url = string.Format("https://api.weixin.qq.com/cgi-bin/material/update_news?access_token={0}", Access_tokento); string jsonres = PostUrl(url, postData); if (jsonres.Contains("errcode")) { //使用前需要引用Newtonsoft.json.dll文件 JObject jsonObj = JObject.Parse(jsonres); if (jsonObj["errcode"].ToString().Equals("0")) { //修改本地数据 //保存数据,方便获取列表的时候直接从本地服务器读取 WxSuCaiInfo wsc = new WxSuCaiInfo(); wsc.SuCaiUrl = Session["fileNameimg"].ToString();//注意,这里保存的图片名称 wsc.SuCaiTitle = this.txttuwen_title.Value.ToString();//图文消息的标题 wsc.SuCaiDigest = this.txtzhaiyao.InnerText.ToString();//图文消息的摘要 wsc.SuCaithumb_media_id = Session["imgmedia_id"].ToString();//图文的消息封面media_id wsc.SuCaiauthor = this.txttuwen_author.Value.ToString(); wsc.SuCaishow_cover_pic = isshow_cover_pic; wsc.SuCaicontent = description; wsc.SuCaicontent_source_url = this.txtYuanWenUrl.Text.ToString(); wsc.uploadDate = System.DateTime.Now.ToString();//记录当前文件保存图文素材日期时间 //修改数据库信息 WxSuCaiService wscs = new WxSuCaiService(); int num = wscs.UpdateWxSuCaiInfo(Session["sucaimedia_id"].ToString(), wsc); if (num > 0) { Session["sucaimedia_id"] = null; Response.Write("<script>alert('图文素材修改成功!');location='https://www.jb51.net/WxSuCaiManageList.aspx';</script>"); } else { Response.Write("<script>alert('图文素材修改失败!');</script>"); } } } } else { //新增图文素材 } }
需注意:新建图文页面和修改图文页面是公用的一个页面.......