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

Response.Redirect("WxMassManage.aspx?media_id=" + jsonObj["media_id"].ToString());
这句代码就是将上传图文后得到的media_Id参数传送到群发界面,群发界面接收代码如下:

protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { BindNewsSucaiList();//绑定素材列表 BindGroupList();//绑定分组列表 BindMassCount();//绑定本月已群发条数 this.DataBind(); if (Request.QueryString["media_id"] != null) { this.RadioBtnList.SelectedValue = "1"; this.showExpress.Visible = false; this.txtwenben.Visible = false; this.tuwen.Visible = true; this.tuwenxuan.Visible = false; this.tuwenjian.Visible = false; this.lbtuwenmedai_id.Visible = true; this.lbtuwenmedai_id.Text = Request.QueryString["media_id"].ToString(); this.LinkBtndeletetuwen.Visible = true; this.Imageyixuan.Visible = true; } } }

最终界面如下:

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

我这里只接收了一个media_id值,相对于做的简单,直接将值赋值给了一个label用于显示,也可以做成像官网那样,确定选择后,按照图文样式显示.

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

最后一步:群发按钮代码:其实上一章已经将代码贴出去了,这一章,我就单独贴一遍吧。
 

/// <summary> /// 群发 /// </summary> /// <param></param> /// <param></param> 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<WxMassInfo> 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; } } } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjwjxw.html