0

0

ASP.NET微信公众号之用户分组管理web页面

高洛峰

高洛峰

发布时间:2016-12-20 13:34:47

|

2455人浏览过

|

来源于php中文网

原创

本文实例为大家分享了asp.net微信用户分组管理的具体代码,供大家参考,具体内容如下

Model层实体类:

public class UserList
{
public string total { get; set; }
public string count { get; set; }
public userlistopenid data { get; set; }
public string next_openid { get; set; }
}
 
public class userlistopenid
{
public List openid { get; set; }
}
public class WxGroupsInfo
{
public string Group_ID { get; set; }//分组编号
public string Group_Name { get; set; }//分组名称
public string Group_Count { get; set; }//分组人数
}

WX.aspx内容:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WX.aspx.cs" Inherits="test.WX" %>
 

 

 
 
 
 
 
 
 
 
 
 
分组管理
新建分组 关闭
30字符以内
确定创建
序号 ID编号 分组名称 分组人数 操作
<%# Eval("Group_ID") %> <%# Eval("Group_Name") %> <%# Eval("Group_Count") %> ', '<%# Eval("Group_Name") %>'); ">修改分组名称 删除分组 此分组消息群发 移动分组
┼ 新建分组

WX.aspx.cs代码:

public partial class WX : System.Web.UI.Page
 {
 protected void Page_Load(object sender, EventArgs e)
 {
 BindGroupList();
 this.DataBind();
 }
 
 
 private void BindGroupList()
 {
 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["AllGroups_content"] as string;
 
 if (content == null)
 {
 jsonres = "https://api.weixin.qq.com/cgi-bin/groups/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("AllGroups_content", content, null, DateTime.Now.AddSeconds(7000),
  System.Web.Caching.Cache.NoSlidingExpiration);
 }
 
 //使用前需要引用Newtonsoft.json文件
 JObject jsonObj = JObject.Parse(content);
 
 
 int groupsnum = jsonObj["groups"].Count();
 
 
 List wxgrouplist = new List();
 
 for (int i = 0; i < groupsnum; i++)
 {
 WxGroupsInfo wginfo = new WxGroupsInfo();
 
 wginfo.Group_ID = jsonObj["groups"][i]["id"].ToString();
 
 wginfo.Group_Name = jsonObj["groups"][i]["name"].ToString();
 
 wginfo.Group_Count = jsonObj["groups"][i]["count"].ToString();
 
 wxgrouplist.Add(wginfo);
 }
 
 this.RepeaterGroupList.DataSource = wxgrouplist;
 this.RepeaterGroupList.DataBind();
 }
 
 
 /// 
 /// 绑定事件
 /// 
 /// 
 /// 
 protected void RepeaterGroupList_ItemDataBound(object sender, RepeaterItemEventArgs e)
 {
 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
 {
 Label lbXuHao = e.Item.FindControl("lbXuHao") as Label;
 
 int num = 1;
 
 lbXuHao.Text = num.ToString();
 
 for (int i = 0; i < this.RepeaterGroupList.Items.Count; i++)
 {
  num += 1;
  lbXuHao.Text = num.ToString();
 }
 
 
 LinkButton LinkBtnDeleteGroup = e.Item.FindControl("LinkBtnDeleteGroup") as LinkButton;
 LinkButton LinkBtnSendByGroup = e.Item.FindControl("LinkBtnSendByGroup") as LinkButton;
 
 LinkBtnDeleteGroup.Attributes.Add("OnClick", "return confirm('您确定要删除该分组?删除后该分组内的人员即将恢复到默认分组!')");
 LinkBtnDeleteGroup.Attributes.Add("OnClick", "return confirm('您确定要群发消息到该分组?')");
 }
 }
 
 /// 
 /// 执行事件
 /// 
 /// 
 /// 
 protected void RepeaterGroupList_ItemCommand(object source, RepeaterCommandEventArgs e)
 { 
 if (e.CommandName == "DeleteGroups")
 {
 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/groups/delete?access_token=" + Access_tokento;
 
 
 //POST数据例子: POST数据例子:{"group":{"id":108}}
 
 string groupid = e.CommandArgument.ToString();
 
 string postData = "{\"group\":{\"id\":\"" + groupid + "\"}}";
 
 res = wxs.GetPage(posturl, postData);
 
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "",
  "alert('删除成功!由于缓存问题,您可能需要重新登录才能看到效果!');location='WxGroupManageList.aspx';", true);
 }
 
 if (e.CommandName == "SendByGroups")
 {
 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/message/mass/sendall?access_token=" + Access_tokento;
 
 string groupid = e.CommandArgument.ToString();
 
 //string postData = "{\"group\":{\"id\":\"" + groupid.ToString() + "\"}}";
 
 JObject postData = new JObject();
  JObject filter = new JObject();
  filter.Add("is_to_all", false);
  filter.Add("group_id", groupid);
 
  JObject text = new JObject();
  text.Add("content", "测试内容!");
 
  postData.Add("filter", filter);
  postData.Add("text", text);
  postData.Add("msgtype", "text");
 
 res = wxs.GetPage(posturl, postData.ToString());
 
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "",
  "alert('群发成功!由于缓存问题,您可能需要重新登录才能看到效果!');location='WxGroupManageList.aspx';", true);
 }
 
 if (e.CommandName == "MoveUserToGroup")
 {
 WeiXinServer wxs = new WeiXinServer();
 string res = "";
 string Access_token = Cache["Access_token"] as string;
 
 if (Access_token == null)
 {
  Access_token = wxs.GetAccessToken();
  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/groups/members/update?access_token=" + Access_tokento;
 
 string UserListurl = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + Access_tokento + "&next_openid=";
 
 
 //{"total":2,"count":2,"data":{"openid":["","OPENID1","OPENID2"]},"next_openid":"NEXT_OPENID"}
 res = wxs.GetPage(UserListurl,"");
 UserList userList= JsonConvert.DeserializeObject(res);
 String openid=userList.data.openid.FirstOrDefault();
 int togroup_id = 101;//输入分组ID
 string postData = "{\"openid\":\"" + openid + "\",\"to_groupid\":" + togroup_id + "}";
 res = wxs.GetPage(posturl, postData);
 
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "",
  "alert('移动分组成功!由于缓存问题,您可能需要重新登录才能看到效果!');location='WxGroupManageList.aspx';", true);
 }
 }
 
 /// 
 /// 创建分组
 /// 
 /// 
 /// 
 protected void LinkBtnCreateGroup_Click(object sender, EventArgs e)
 {
 if (this.txtgroupsName.Value.Equals("分组名称"))
 {
 ////
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('不能为空!')", true);
 this.txtgroupsName.Focus();
 return;
 }
 
 
 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/groups/create?access_token=" + Access_tokento;
 
 string postData = "{\"group\":{\"name\":\"" + this.txtgroupsName.Value.ToString().Trim() + "\"}}"; 
 res = wxs.GetPage(posturl, postData); 
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "",
 "alert('创建成功!如未显示,请退出重新登录即可!');location='WxGroupManageList.aspx';", true);
 }
 }

WeiXinServer.cs代码:

/// 
 /// 微信服务类
 /// 
 public class WeiXinServer
 {
 /// 
 /// 获取通行证
 /// 
 /// 
 public string GetAccessToken()
 {
 string url_token =
 "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3eb5bf1290db2ca0&secret=e6013be0a7338c7d3e02877db116e231";
 HttpWebRequest myRequest = (HttpWebRequest) WebRequest.Create(url_token);
 myRequest.Method = "GET";
 HttpWebResponse myResponse = (HttpWebResponse) myRequest.GetResponse();
 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
 string content = reader.ReadToEnd();
 reader.Close();
 return content;
 }
 
 public string GetPage(string p, string postData)
 {
 Stream outstream = null;
 Stream instream = null;
 StreamReader sr = null;
 HttpWebResponse response = null;
 HttpWebRequest request = null;
 Encoding encoding = Encoding.UTF8;
 byte[] data = encoding.GetBytes(postData);
 // 准备请求...
 try
 {
 // 设置参数
 request = WebRequest.Create(p) as HttpWebRequest;
 CookieContainer cookieContainer = new CookieContainer();
 request.CookieContainer = cookieContainer;
 request.AllowAutoRedirect = true;
 request.Method = "POST";
 request.ContentType = "application/x-www-form-urlencoded";
 request.ContentLength = data.Length;
 outstream = request.GetRequestStream();
 outstream.Write(data, 0, data.Length);
 outstream.Close();
 //发送请求并获取相应回应数据
 response = request.GetResponse() as HttpWebResponse;
 //直到request.GetResponse()程序才开始向目标网页发送Post请求
 instream = response.GetResponseStream();
 sr = new StreamReader(instream, encoding);
 //返回结果网页(html)代码
 string content = sr.ReadToEnd();
 string err = string.Empty;
 return content;
 }
 catch (Exception ex)
 {
 string err = ex.Message;
 return string.Empty;
 }
 }
 }

Edit.aspx内容:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Edit.aspx.cs" Inherits="test.Edit" %>
 

 
 
 
 
 
 
 
分组编号:
分组名称:
设 置

Edit.aspx.cs代码:

public partial class Edit : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Request.QueryString["id"] != null)
{
 string group_id = Request.QueryString["id"].ToString();
 string group_name = Request.QueryString["name"].ToString();
 this.txtGroupId.Text = group_id.ToString();
 this.txtGroupName.Text = group_name.ToString();
 this.txtGroupName.Focus();
}
}
}
 
/// 
/// 设置
/// 
/// 
/// 
protected void LinkBtnSet_Click(object sender, EventArgs e)
{
if (String.IsNullOrWhiteSpace(this.txtGroupName.Text.ToString().Trim()))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('不能为空!');", true);
this.txtGroupName.Focus();
return;
}
if (this.txtGroupName.Text.Trim().Length > 30)
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('组名称应在30个字符之内!');", true);
this.txtGroupName.Focus();
return;
}
 
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/groups/update?access_token=" + Access_tokento;
 
//POST数据例子:POST数据例子:{"group":{"id":108,"name":"test2_modify2"}}
 
string postData = "{\"group\":{\"id\":\"" + txtGroupId.Text + "\",\"name\":\"" + this.txtGroupName.Text +
 "\"}}";
res = wxs.GetPage(posturl, postData);
 
//使用Newtonsoft.json
JObject jsonObj = JObject.Parse(res);
 
//获取返回结果的正确|true|false,
string isright = jsonObj["errcode"].ToString(); //0
string istrueorfalse = jsonObj["errmsg"].ToString(); //ok
if (isright.Equals("0") && istrueorfalse.Equals("ok"))
{
//修改成功之后,刷新父窗体,关闭本页
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "",
 "alert('修改成功!如未正常显示,属缓存问题,请重新登录即可!');window.open![这里写图片描述](http://img.blog.csdn.net/20161008103126694)er.location.reload();this.close();", true);
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('修改失败!');this.close();",
 true);
}
}
}

buttonbg.png

Shop7z网上购物系统至尊版
Shop7z网上购物系统至尊版

Shop7z网上购物系统支持电脑版+手机版+支付宝及微信支付,支持QQ和微信一键登陆,系统集众家之所长,大气超美观页面+手机版+商品组合套餐+限时抢购秒杀+图片批量上传+淘宝数据包导入+弹出式分类菜单+不同规格不同价格+新订单邮件通知+销售报表打印与Excel输出+物流跟踪打印查询+会员积分及优惠券+邮件群发+图片在线管理+销售统计报表+五种价格体系+礼品礼券+微信公众号支付+扫码支付等等等。

下载

ASP.NET微信公众号之用户分组管理web页面

结果如图:

ASP.NET微信公众号之用户分组管理web页面

ASP.NET微信公众号之用户分组管理web页面

ASP.NET微信公众号之用户分组管理web页面

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHP中文网。

微信app下载
微信app下载

微信是一款手机通信软件,支持通过手机网络发送语音短信、视频、图片和文字。微信可以单聊及群聊,还能根据地理位置找到附近的人,带给大家全新的移动沟通体验,有需要的小伙伴快来保存下载体验吧!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
微信聊天记录删除恢复导出教程汇总
微信聊天记录删除恢复导出教程汇总

本专题整合了微信聊天记录相关教程大全,阅读专题下面的文章了解更多详细内容。

36

2026.01.18

高德地图升级方法汇总
高德地图升级方法汇总

本专题整合了高德地图升级相关教程,阅读专题下面的文章了解更多详细内容。

99

2026.01.16

全民K歌得高分教程大全
全民K歌得高分教程大全

本专题整合了全民K歌得高分技巧汇总,阅读专题下面的文章了解更多详细内容。

148

2026.01.16

C++ 单元测试与代码质量保障
C++ 单元测试与代码质量保障

本专题系统讲解 C++ 在单元测试与代码质量保障方面的实战方法,包括测试驱动开发理念、Google Test/Google Mock 的使用、测试用例设计、边界条件验证、持续集成中的自动化测试流程,以及常见代码质量问题的发现与修复。通过工程化示例,帮助开发者建立 可测试、可维护、高质量的 C++ 项目体系。

56

2026.01.16

java数据库连接教程大全
java数据库连接教程大全

本专题整合了java数据库连接相关教程,阅读专题下面的文章了解更多详细内容。

40

2026.01.15

Java音频处理教程汇总
Java音频处理教程汇总

本专题整合了java音频处理教程大全,阅读专题下面的文章了解更多详细内容。

19

2026.01.15

windows查看wifi密码教程大全
windows查看wifi密码教程大全

本专题整合了windows查看wifi密码教程大全,阅读专题下面的文章了解更多详细内容。

107

2026.01.15

浏览器缓存清理方法汇总
浏览器缓存清理方法汇总

本专题整合了浏览器缓存清理教程汇总,阅读专题下面的文章了解更多详细内容。

44

2026.01.15

ps图片相关教程汇总
ps图片相关教程汇总

本专题整合了ps图片设置相关教程合集,阅读专题下面的文章了解更多详细内容。

12

2026.01.15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP开发微信公众号平台从简到精
PHP开发微信公众号平台从简到精

共25课时 | 5.9万人学习

破釜沉舟,PHP 王者归来!
破釜沉舟,PHP 王者归来!

共5课时 | 1.9万人学习

微信开发视频教程
微信开发视频教程

共24课时 | 5.4万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号