首页 > PHP资讯 > 工具库 > asp.net微信开发(高级群发图文)

asp.net微信开发(高级群发图文)

工具库
这篇文章主要介绍了asp.net微信开发中有关高级群发图文的相关内容,需要的朋友可以参考下

上一篇介绍了如何群发文本消息,本篇将介绍如何群发图文信息,上传图文信息所需的素材,界面如下:

 
  

选择素材

确认选择

刷新

删除素材

本类型素材总数量为: 本次获取的素材数量为:

后台代码如下:

 ///  /// 绑定图文素材列表 ///  private void BindNewsSucaiList() {  WeiXinServer wxs = new WeiXinServer();  string res = "";  ///从缓存读取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);  string posturl = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + Access_tokento;  //POST数据例子: POST数据例子:{"type":TYPE,"offset":OFFSET,"count":COUNT}  string postData = "{"type":"news","offset":"0","count":"20"}";  res = wxs.GetPage(posturl, postData);  //使用前需要引用Newtonsoft.json.dll文件  JObject jsonObj = JObject.Parse(res);  int groupsnum = jsonObj["item"].Count();  List newssucaiitemlist = new List();  List WxNewsSuCaiItemlist = new List();  for (int i = 0; i < groupsnum; i++)  {  WxNewsSucaiIteminfo newssucaiitem = new WxNewsSucaiIteminfo();  newssucaiitem.media_id = jsonObj["item"][i]["media_id"].ToString();  newssucaiitem.update_time = jsonObj["item"][i]["update_time"].ToString();  newssucaiitem.total_count = jsonObj["total_count"].ToString();  newssucaiitem.item_count = jsonObj["item_count"].ToString();  newssucaiitemlist.Add(newssucaiitem);  int news_itemcount = jsonObj["item"][i]["content"]["news_item"].Count();  if (news_itemcount > 0)  {   for (int j = 0; j < news_itemcount; j++)   {   WxNewsSuCaiItemlistinfo wnscilinfo = new WxNewsSuCaiItemlistinfo();   wnscilinfo.title = jsonObj["item"][i]["content"]["news_item"][j]["title"].ToString();   wnscilinfo.thumb_media_id = jsonObj["item"][i]["content"]["news_item"][j]["thumb_media_id"].ToString();   wnscilinfo.show_cover_pic = int.Parse(jsonObj["item"][i]["content"]["news_item"][j]["show_cover_pic"].ToString());   wnscilinfo.author = jsonObj["item"][i]["content"]["news_item"][j]["author"].ToString();   wnscilinfo.digest = jsonObj["item"][i]["content"]["news_item"][j]["digest"].ToString();   wnscilinfo.content = jsonObj["item"][i]["content"]["news_item"][j]["content"].ToString();   wnscilinfo.url = jsonObj["item"][i]["content"]["news_item"][j]["url"].ToString();   wnscilinfo.content_source_url = jsonObj["item"][i]["content"]["news_item"][j]["content_source_url"].ToString();   wnscilinfo.media_id = newssucaiitem.media_id.ToString();   WxNewsSuCaiItemlist.Add(wnscilinfo);   }  }  }  Session["WxNewsSuCaiItemlist"] = WxNewsSuCaiItemlist;  this.Repeatersucailist.DataSource = newssucaiitemlist;  this.Repeatersucailist.DataBind(); }

再来看看,新建单图文信息界面:

新建单图文上传封面,删除封面的代码如下:

 ///  ///  /// 上传图片文件 /// 
  protected void LinkBtnSubSend_Click(object sender, EventArgs e)  {   //根据单选按钮判断类型,//如果选择的是图文消息   if (this.RadioBtnList.SelectedValue.ToString().Equals("1"))   {    if (String.IsNullOrWhiteSpace(this.lbtuwenmedai_id.Text.ToString().Trim()))    {     ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请选择或新建图文素材再进行群发!');", true);     return;    }    WxMassService wms = new WxMassService();    List wxmaslist = wms.GetMonthMassCount();    if (wxmaslist.Count >= 4)    {     ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('本月可群发消息数量已达上限!');", true);     return;    }    else    {          //如何群发类型为全部用户,根据openID列表群发给全部用户,订阅号不可用,服务号认证后可用     if (this.DDLMassType.SelectedValue.ToString().Equals("0"))     {      StringBuilder sbs = new StringBuilder();      sbs.Append(GetAllUserOpenIDList());      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);      string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + Access_tokento;      ///群发POST数据示例如下:       // {      // "touser":[      // "OPENID1",      // "OPENID2"      // ],      // "mpnews":{      //  "media_id":"123dsdajkasd231jhksad"      // },      // "msgtype":"mpnews"      //}      string postData = "{"touser":[" + sbs.ToString() +       "],"mpnews":{"media_id":"" + this.lbtuwenmedai_id.Text.ToString() +       ""},"msgtype":"mpnews"}";      string tuwenres = wxs.GetPage(posturl, postData);      //使用前需药引用Newtonsoft.json.dll文件      JObject jsonObj = JObject.Parse(tuwenres);      if (jsonObj["errcode"].ToString().Equals("0"))      {       Session["media_id"] = null;       WxMassInfo wmi = new WxMassInfo();       if (Session["wmninfo"] != null)       {        WxMpNewsInfo wmninfo = Session["wmninfo"] as WxMpNewsInfo;        wmi.title = wmninfo.title.ToString();        wmi.contents = wmninfo.contents.ToString();        wmi.ImageUrl = wmninfo.ImageUrl.ToString();        wmi.type = "图文";        if (this.DDLMassType.SelectedValue.ToString().Equals("0"))        {         wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();        }        else        {         wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();        }        wmi.massStatus = "成功";//群发成功之后返回的状态码        wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息ID        wmi.massDate = System.DateTime.Now.ToString();        int num = wms.AddWxMassInfo(wmi);        if (num > 0)        {         Session["wmninfo"] = null;         ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据已保存!');location='WxMassManage.aspx';", true);         return;        }        else        {         ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据保存失败!');", true);         return;        }       }       else       {        wmi.title = "";        wmi.contents = "";        wmi.ImageUrl = "";        wmi.type = "图文";        if (this.DDLMassType.SelectedValue.ToString().Equals("0"))        {         wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();        }        else        {         wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();        }        wmi.massStatus = "成功";//群发成功之后返回的状态码        wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息ID        wmi.massDate = System.DateTime.Now.ToString();        int num = wms.AddWxMassInfo(wmi);        if (num > 0)        {         Session["wmninfo"] = null;         ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!图文部分数据已保存!');location='WxMassManage.aspx';", true);         return;        }        else        {         ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据保存失败!');", true);         return;        }       }      }      else      {       ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务提交失败!!');", true);       return;      }     }     else     {      //根据分组进行群发,订阅号和服务号认证后均可用      string group_id = this.DDLGroupList.SelectedValue.ToString();      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);      string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + Access_tokento;      ///群发POST数据示例如下:       // {      // "filter":{      //  "is_to_all":false      //  "group_id":"2"      // },      // "mpnews":{      //  "media_id":"123dsdajkasd231jhksad"      // },      // "msgtype":"mpnews"      //}      string postData = "{"filter":{"is_to_all":"false""group_id":""+group_id+       ""},"mpnews":{"media_id":"" + this.lbtuwenmedai_id.Text.ToString() +       ""},"msgtype":"mpnews"}";      string tuwenres = wxs.GetPage(posturl, postData);      //使用前需药引用Newtonsoft.json.dll文件      JObject jsonObj = JObject.Parse(tuwenres);      if (jsonObj["errcode"].ToString().Equals("0"))      {       Session["media_id"] = null;       WxMassInfo wmi = new WxMassInfo();       if (Session["wmninfo"] != null)       {        WxMpNewsInfo wmninfo = Session["wmninfo"] as WxMpNewsInfo;        wmi.title = wmninfo.title.ToString();        wmi.contents = wmninfo.contents.ToString();        wmi.ImageUrl = wmninfo.ImageUrl.ToString();        wmi.type = "图文";        if (this.DDLMassType.SelectedValue.ToString().Equals("0"))        {         wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();        }        else        {         wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();        }        wmi.massStatus = "成功";//群发成功之后返回的状态码        wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息ID        wmi.massDate = System.DateTime.Now.ToString();        int num = wms.AddWxMassInfo(wmi);        if (num > 0)        {         Session["wmninfo"] = null;         ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据已保存!');location='WxMassManage.aspx';", true);         return;        }        else        {         ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据保存失败!');", true);         return;        }       }       else       {        wmi.title = "";        wmi.contents = "";        wmi.ImageUrl = "";        wmi.type = "图文";        if (this.DDLMassType.SelectedValue.ToString().Equals("0"))        {         wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();        }        else        {         wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();        }        wmi.massStatus = "成功";//群发成功之后返回的状态码        wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息ID        wmi.massDate = System.DateTime.Now.ToString();        int num = wms.AddWxMassInfo(wmi);        if (num > 0)        {         Session["wmninfo"] = null;         ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!图文部分数据已保存!');location='WxMassManage.aspx';", true);         return;        }        else        {         ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据保存失败!');", true);         return;        }       }      }      else      {       ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务提交失败!!');", true);       return;      }     }    }   }  }

为什么叫群发任务提交成功或失败,因为将信息提交给微信服务器,微信服务器还需审核,审核过程中也有可能审核不通过,不给于群发,所以我起名叫这个,嘿嘿,随便你们怎么起。。。。。

 ///  /// 微信已群发消息实体类,用于记录已群发消息的条数,信息实体 ///  public class WxMassInfo {  public int WxMassNo { get; set; }//群发消息编号,数据库自增列  public string title { get; set; }//图文消息的标题,若消息是文本类型,此项不显示  public string ImageUrl { get; set; }//图片地址,若消息是文本类型,此项不显示  public string type { get; set; }//消息的类型,文本,图文,图片,语音,视频  public string contents { get; set; }//文本消息的内容,图文消息的正文  public string massObject { get; set; }//群发对象  public string massStatus { get; set; }//群发状态  public string massMessageID{ get; set; }//群发成功后返回的消息ID  public string massDate { get; set; }//群发日期时间 }

以上就是本文的全部内容,希望对大家的学习有所帮助。

更多asp.net微信开发(高级群发图文)相关文章请关注PHP中文网!

工具库

本文由欣才IT学院整理发布,未经许可,禁止转载。
支持5不支持0