您的位置:首页 > 编程语言 > ASP

asp.net微信开发第六篇----高级群发(文本)

2015-11-11 16:22 399 查看
说到高级群发,微信的参考资料http://mp.weixin.qq.com/wiki/14/0c53fac3bdec3906aaa36987b91d64ea.html

首先我们先来讲解一下群发文本信息的过程,我个人开发程序是首先要有UI才能下手去写代码,界面如下,

/// <summary>
/// 发送前预览
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkBtnSendPreview_Click(object sender, EventArgs e)
{
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/preview?access_token=" + Access_tokento;

///如果选择的是文本消息
if (this.RadioBtnList.SelectedValue.ToString().Equals("0"))
{
if (String.IsNullOrWhiteSpace(this.txtwenben.InnerText.ToString().Trim()))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入您要发送预览的文本内容!');", true);
return;
}
if (this.txttoUserName.Value.ToString().Trim().Equals("请输入用户微信号"))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入接收消息的用户微信号!');", true);
return;
}
//文本消息的json数据{
//   "touser":"OPENID", 可改为对微信号预览,例如towxname:zhangsan
//    "text":{
//           "content":"CONTENT"
//           },
//    "msgtype":"text"
//}
string postData = "{\"towxname\":\"" + this.txttoUserName.Value.ToString() +
"\",\"text\":{\"content\":\"" + this.txtwenben.InnerText.ToString() +
"\"},\"msgtype\":\"text\"}";

string tuwenres = wxs.GetPage(posturl, postData);

//使用前需药引用Newtonsoft.json.dll文件
JObject jsonObj = JObject.Parse(tuwenres);

if (jsonObj["errcode"].ToString().Equals("0"))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览成功!!');", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览失败!!');", true);
return;
}
}
//如果选择的是图文消息
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;
}
if (this.txttoUserName.Value.ToString().Trim().Equals("请输入用户微信号"))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入接收消息的用户微信号!');", true);
return;
}
//图文消息的json数据{
//   "touser":"OPENID", 可改为对微信号预览,例如towxname:zhangsan
//   "mpnews":{
//            "media_id":"123dsdajkasd231jhksad"
//             },
//   "msgtype":"mpnews"
//}
string postData = "{\"towxname\":\"" + this.txttoUserName.Value.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;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览成功!!');", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览失败!!');", true);
return;
}

}

}


View Code
关键部分,获取全部用户的openID并串联成字符串:

/// <summary>
/// 获取所有微信用户的OpenID
/// </summary>
/// <returns></returns>
protected string GetAllUserOpenIDList()
{
StringBuilder sb = new StringBuilder();

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 jsonres = "";

string content = Cache["AllUserOpenList_content"] as string;

if (content == null)
{
jsonres = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + Access_tokento;

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(jsonres);
myRequest.Method = "GET";
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
content = reader.ReadToEnd();
reader.Close();

//设置缓存的数据7000秒后过期
Cache.Insert("AllUserOpenList_content", content, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}

//使用前需要引用Newtonsoft.json.dll文件
JObject jsonObj = JObject.Parse(content);

if (jsonObj.ToString().Contains("count"))
{
int totalnum = int.Parse(jsonObj["count"].ToString());

for (int i = 0; i < totalnum; i++)
{
sb.Append('"');
sb.Append(jsonObj["data"]["openid"][i].ToString());
sb.Append('"');
sb.Append(",");
}
}

return sb.Remove(sb.ToString().LastIndexOf(","),1).ToString();
}


至此结束,下一章将继续讲解群发图文信息,因群发图文信息之前,需要先上传图文信息所需的素材,获取media_id,所以本章不做介绍,下一章将介绍新建单图文信息并群发。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: