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

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
java入门学习合集
java入门学习合集

本专题整合了java入门学习指南、初学者项目实战、入门到精通等等内容,阅读专题下面的文章了解更多详细学习方法。

2

2026.01.29

java配置环境变量教程合集
java配置环境变量教程合集

本专题整合了java配置环境变量设置、步骤、安装jdk、避免冲突等等相关内容,阅读专题下面的文章了解更多详细操作。

2

2026.01.29

java成品学习网站推荐大全
java成品学习网站推荐大全

本专题整合了java成品网站、在线成品网站源码、源码入口等等相关内容,阅读专题下面的文章了解更多详细推荐内容。

0

2026.01.29

Java字符串处理使用教程合集
Java字符串处理使用教程合集

本专题整合了Java字符串截取、处理、使用、实战等等教程内容,阅读专题下面的文章了解详细操作教程。

0

2026.01.29

Java空对象相关教程合集
Java空对象相关教程合集

本专题整合了Java空对象相关教程,阅读专题下面的文章了解更多详细内容。

3

2026.01.29

clawdbot ai使用教程 保姆级clawdbot部署安装手册
clawdbot ai使用教程 保姆级clawdbot部署安装手册

Clawdbot是一个“有灵魂”的AI助手,可以帮用户清空收件箱、发送电子邮件、管理日历、办理航班值机等等,并且可以接入用户常用的任何聊天APP,所有的操作均可通过WhatsApp、Telegram等平台完成,用户只需通过对话,就能操控设备自动执行各类任务。

25

2026.01.29

clawdbot龙虾机器人官网入口 clawdbot ai官方网站地址
clawdbot龙虾机器人官网入口 clawdbot ai官方网站地址

clawdbot龙虾机器人官网入口:https://clawd.bot/,clawdbot ai是一个“有灵魂”的AI助手,可以帮用户清空收件箱、发送电子邮件、管理日历、办理航班值机等等,并且可以接入用户常用的任何聊天APP,所有的操作均可通过WhatsApp、Telegram等平台完成,用户只需通过对话,就能操控设备自动执行各类任务。

16

2026.01.29

Golang 网络安全与加密实战
Golang 网络安全与加密实战

本专题系统讲解 Golang 在网络安全与加密技术中的应用,包括对称加密与非对称加密(AES、RSA)、哈希与数字签名、JWT身份认证、SSL/TLS 安全通信、常见网络攻击防范(如SQL注入、XSS、CSRF)及其防护措施。通过实战案例,帮助学习者掌握 如何使用 Go 语言保障网络通信的安全性,保护用户数据与隐私。

8

2026.01.29

俄罗斯Yandex引擎入口
俄罗斯Yandex引擎入口

2026年俄罗斯Yandex搜索引擎最新入口汇总,涵盖免登录、多语言支持、无广告视频播放及本地化服务等核心功能。阅读专题下面的文章了解更多详细内容。

622

2026.01.28

热门下载

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

精品课程

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

共25课时 | 6万人学习

破釜沉舟,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号