当前位置:Gxlcms > asp.net > .NET微信公众号 用户分组管理

.NET微信公众号 用户分组管理

时间:2021-07-01 10:21:17 帮助过:28人阅读

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

Model层实体类:

  1. public class UserList
  2. {
  3. public string total { get; set; }
  4. public string count { get; set; }
  5. public userlistopenid data { get; set; }
  6. public string next_openid { get; set; }
  7. }
  1. public class userlistopenid
  2. {
  3. public List<string> openid { get; set; }
  4. }
  1. public class WxGroupsInfo
  2. {
  3. public string Group_ID { get; set; }//分组编号
  4. public string Group_Name { get; set; }//分组名称
  5. public string Group_Count { get; set; }//分组人数
  6. }

WX.aspx内容:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WX.aspx.cs" Inherits="test.WX" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  6. <style type="text/css">
  7. .inputstyle {
  8. height: 35px;
  9. line-height: 35px;
  10. text-indent: 5px;
  11. width: 350px;
  12. background-image: url('images/inputbg.gif');
  13. background-repeat: repeat-x;
  14. border-top: solid 1px #a7b5bc;
  15. border-left: solid 1px #a7b5bc;
  16. border-right: solid 1px #ced9df;
  17. border-bottom: solid 1px #ced9df;
  18. margin: 15px auto 15px auto;
  19. }
  20. .g_title {
  21. width: 100%;
  22. border-bottom: 2px solid #ced9df;
  23. font-size: 20px;
  24. font-weight: bold;
  25. text-align: left;
  26. text-indent: 5px;
  27. height: 40px;
  28. line-height: 40px;
  29. }
  30. th {
  31. height: 35px;
  32. background-color: #31bb34;
  33. background-repeat: repeat-x;
  34. }
  35. tr { height: 30px; }
  36. #shownewgroup {
  37. width: 300px;
  38. height: 200px;
  39. background-color: white;
  40. z-index: 9999;
  41. border: 2px solid #DDD;
  42. top: 40%;
  43. left: 40%;
  44. background-color: #fff;
  45. position: fixed;
  46. margin: -100px auto auto -100px;
  47. display: none;
  48. }
  49. #shownewgroupzhezhaoceng {
  50. height: 200%;
  51. width: 200%;
  52. left: 0px;
  53. top: 0px;
  54. position: fixed;
  55. z-index: 9998;
  56. background: rgb(50, 50, 50);
  57. background: rgba(0, 0, 0, 0.5);
  58. display: none;
  59. }
  60. .closeLogin {
  61. height: 30px;
  62. border-bottom: 2px solid #31bb34;
  63. text-align: right;
  64. line-height: 30px;
  65. font-size: 14px;
  66. font-weight: bold;
  67. }
  68. a:hover { cursor: pointer; }
  69. .inputstyle22 {
  70. height: 35px;
  71. line-height: 35px;
  72. text-indent: 5px;
  73. width: 280px;
  74. background-image: url('images/inputbg.gif');
  75. background-repeat: repeat-x;
  76. border-top: solid 1px #a7b5bc;
  77. border-left: solid 1px #a7b5bc;
  78. border-right: solid 1px #ced9df;
  79. border-bottom: solid 1px #ced9df;
  80. float: left;
  81. margin: auto 5px auto 5px;
  82. }
  83. </style>
  84. <style type="text/css">
  85. .button {
  86. font: 15px Calibri, Arial, sans-serif;
  87. text-shadow: 1px 1px 0 rgba(255, 255, 255, 0.4);
  88. text-decoration: none !important;
  89. white-space: nowrap;
  90. display: inline-block;
  91. vertical-align: baseline;
  92. position: relative;
  93. cursor: pointer;
  94. padding: 4px 20px;
  95. background-repeat: no-repeat;
  96. background-position: bottom left;
  97. background-image: url('button_bg.png');
  98. background-position: bottom left, top right, 0 0, 0 0;
  99. background-clip: border-box;
  100. -moz-border-radius: 8px;
  101. -webkit-border-radius: 8px;
  102. border-radius: 8px;
  103. -moz-box-shadow: 0 0 1px #fff inset;
  104. -webkit-box-shadow: 0 0 1px #fff inset;
  105. box-shadow: 0 0 1px #fff inset;
  106. -webkit-transition: background-position 1s;
  107. -moz-transition: background-position 1s;
  108. transition: background-position 1s;
  109. }
  110. .blue.button {
  111. color: #0f4b6d !important;
  112. border: 1px solid #84acc3 !important;
  113. background-color: #48b5f2;
  114. background-image: url('button_bg.png'), url('button_bg.png'),
  115. -moz-radial-gradient(center bottom, circle,
  116. rgba(89, 208, 244, 1) 0, rgba(89, 208, 244, 0) 100px),
  117. -moz-linear-gradient(#4fbbf7, #3faeeb);
  118. background-image: url('button_bg.png'), url('button_bg.png'),
  119. -webkit-gradient(radial, 50% 100%, 0, 50% 100%, 100,
  120. from(rgba(89, 208, 244, 1)), to(rgba(89, 208, 244, 0))),
  121. -webkit-gradient(linear, 0% 0%, 0% 100%, from(#4fbbf7), to(#3faeeb));
  122. }
  123. .blue.button:hover {
  124. background-color: #63c7fe;
  125. background-image: url('button_bg.png'), url('button_bg.png'),
  126. -moz-radial-gradient(center bottom, circle,
  127. rgba(109, 217, 250, 1) 0, rgba(109, 217, 250, 0) 100px),
  128. -moz-linear-gradient(#63c7fe, #58bef7);
  129. background-image: url('button_bg.png'), url('button_bg.png'),
  130. -webkit-gradient(radial, 50% 100%, 0, 50% 100%, 100,
  131. from(rgba(109, 217, 250, 1)), to(rgba(109, 217, 250, 0))),
  132. -webkit-gradient(linear, 0% 0%, 0% 100%, from(#63c7fe), to(#58bef7));
  133. }
  134. </style>
  135. <script src="js/jquery-1.11.2.min.js"></script>
  136. <script type="text/javascript">
  137. //修改分组名称调用的函数
  138. function EditRoster(PayNo, name) {
  139. //alert(PayNo+":"+name);
  140. var url = 'Edit.aspx?id=' + PayNo + "&name=" + name; //转向网页的地址;
  141. var name = 'add'; //网页名称,可为空;
  142. var iWidth = 600; //弹出窗口的宽度;
  143. var iHeight = 300; //弹出窗口的高度;
  144. //获得窗口的垂直位置
  145. var iTop = (window.screen.availHeight - 30 - iHeight) / 2;
  146. //获得窗口的水平位置
  147. var iLeft = (window.screen.availWidth - 10 - iWidth) / 2;
  148. window.open(url, name, 'height=' + iHeight + ',innerHeight=' + iHeight + ',width=' + iWidth + ',innerWidth=' + iWidth + ',top=' + iTop + ',left=' + iLeft + ',status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=0,titlebar=no');
  149. }
  150. $(document).ready(function() {
  151. $(".newGroups").click(function() {
  152. $("#shownewgroupzhezhaoceng").show();
  153. $("#shownewgroup").show();
  154. }),
  155. $('.closeloginpage').click(function() {
  156. $("#shownewgroupzhezhaoceng").hide();
  157. $("#shownewgroup").hide();
  158. });
  159. });
  160. </script>
  161. </head>
  162. <body>
  163. <form id="form1" runat="server">
  164. <div class="g_title">分组管理</div>
  165. <div id="shownewgroup">
  166. <div class="closeLogin">
  167. <span style="float: left; color: #08a5e0; font-size: 18px; text-indent: 5px;">新建分组</span>
  168. <a class="closeloginpage button blue ">
  169. 关闭
  170. </a>
  171. </div>
  172. <div style="font-size: 12px; height: 40px; color: red; line-height: 40px;">
  173. 30字符以内
  174. </div>
  175. <input type="text" id="txtgroupsName" name="txtgroupsName" class="inputstyle22" maxlength="30" runat="server" value="分组名称" onfocus="if(value==defaultValue){value='';this.style.color='#000'}" onblur="if(!value){value=defaultValue;this.style.color='#999'}" style="color: #999"/>
  176. <asp:LinkButton ID="LinkBtnCreateGroup" runat="server" OnClick="LinkBtnCreateGroup_Click">
  177. <div style="background-image: url('images/buttonbg.png'); width: 111px; height: 35px; line-height: 35px; font-weight: bold; float: left; margin-top: 20px; margin-left: 5px; text-align: center; color: #fff;">
  178. 确定创建
  179. </div>
  180. </asp:LinkButton>
  181. </div>
  182. <div id="shownewgroupzhezhaoceng"></div>
  183. <table style="width: 1124px; margin: 10px auto 10px auto; border: 1px solid #ecd9df; text-align: center;">
  184. <asp:Repeater ID="RepeaterGroupList" runat="server" OnItemCommand="RepeaterGroupList_ItemCommand" OnItemDataBound="RepeaterGroupList_ItemDataBound">
  185. <HeaderTemplate>
  186. <tr>
  187. <th>序号</th>
  188. <th>ID编号</th>
  189. <th>分组名称</th>
  190. <th>分组人数</th>
  191. <th>操作</th>
  192. </tr>
  193. </HeaderTemplate>
  194. <ItemTemplate>
  195. <tr style='background-color: <%#(Container.ItemIndex%2 == 0) ? "#fff" : "#ced9ff" %>'>
  196. <td><asp:Label ID="lbXuHao" runat="server" Text=""></asp:Label></td>
  197. <td><%# Eval("Group_ID") %></td>
  198. <td><%# Eval("Group_Name") %></td>
  199. <td><%# Eval("Group_Count") %></td>
  200. <td>
  201. <a class="button blue" onclick=" EditRoster('<%# Eval("Group_ID") %>', '<%# Eval("Group_Name") %>'); ">修改分组名称</a>
  202. <asp:LinkButton ID="LinkBtnDeleteGroup" runat="server" CommandName="DeleteGroups" CommandArgument='<%# Eval("Group_ID") %>' CssClass="button blue">删除分组</asp:LinkButton>
  203. <asp:LinkButton ID="LinkBtnSendByGroup" runat="server" CommandName="SendByGroups" CommandArgument='<%# Eval("Group_ID") %>' CssClass="button blue">此分组消息群发</asp:LinkButton>
  204. <asp:LinkButton ID="LinkBtnMoveUserToGroup" runat="server" CommandName="MoveUserToGroup" CommandArgument='<%# Eval("Group_ID") %>' CssClass="button blue">移动分组</asp:LinkButton>
  205. </td>
  206. </tr>
  207. </ItemTemplate>
  208. </asp:Repeater>
  209. </table>
  210. <a class="newGroups"><div style="background-image: url('images/buttonbg.png'); width: 111px; height: 35px; line-height: 35px; margin: 10px auto 10px 28px; font-weight: bold; float: left; text-align: center; color: #fff;"> ┼ 新建分组</div></a>
  211. </form>
  212. </body>
  213. </html>

WX.aspx.cs代码:

  1. public partial class WX : System.Web.UI.Page
  2. {
  3. protected void Page_Load(object sender, EventArgs e)
  4. {
  5. BindGroupList();
  6. this.DataBind();
  7. }
  8. private void BindGroupList()
  9. {
  10. WeiXinServer wxs = new WeiXinServer();
  11. //从缓存读取accesstoken
  12. string Access_token = Cache["Access_token"] as string;
  13. if (Access_token == null)
  14. {
  15. //如果为空,重新获取
  16. Access_token = wxs.GetAccessToken();
  17. //设置缓存的数据7000秒后过期
  18. Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000),
  19. System.Web.Caching.Cache.NoSlidingExpiration);
  20. }
  21. string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
  22. string jsonres = "";
  23. string content = Cache["AllGroups_content"] as string;
  24. if (content == null)
  25. {
  26. jsonres = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=" + Access_tokento;
  27. HttpWebRequest myRequest = (HttpWebRequest) WebRequest.Create(jsonres);
  28. myRequest.Method = "GET";
  29. HttpWebResponse myResponse = (HttpWebResponse) myRequest.GetResponse();
  30. StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  31. content = reader.ReadToEnd();
  32. reader.Close();
  33. //设置缓存的数据7000秒后过期
  34. Cache.Insert("AllGroups_content", content, null, DateTime.Now.AddSeconds(7000),
  35. System.Web.Caching.Cache.NoSlidingExpiration);
  36. }
  37. //使用前需要引用Newtonsoft.json文件
  38. JObject jsonObj = JObject.Parse(content);
  39. int groupsnum = jsonObj["groups"].Count();
  40. List<WxGroupsInfo> wxgrouplist = new List<WxGroupsInfo>();
  41. for (int i = 0; i < groupsnum; i++)
  42. {
  43. WxGroupsInfo wginfo = new WxGroupsInfo();
  44. wginfo.Group_ID = jsonObj["groups"][i]["id"].ToString();
  45. wginfo.Group_Name = jsonObj["groups"][i]["name"].ToString();
  46. wginfo.Group_Count = jsonObj["groups"][i]["count"].ToString();
  47. wxgrouplist.Add(wginfo);
  48. }
  49. this.RepeaterGroupList.DataSource = wxgrouplist;
  50. this.RepeaterGroupList.DataBind();
  51. }
  52. /// <summary>
  53. /// 绑定事件
  54. /// </summary>
  55. /// <param name="sender"></param>
  56. /// <param name="e"></param>
  57. protected void RepeaterGroupList_ItemDataBound(object sender, RepeaterItemEventArgs e)
  58. {
  59. if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
  60. {
  61. Label lbXuHao = e.Item.FindControl("lbXuHao") as Label;
  62. int num = 1;
  63. lbXuHao.Text = num.ToString();
  64. for (int i = 0; i < this.RepeaterGroupList.Items.Count; i++)
  65. {
  66. num += 1;
  67. lbXuHao.Text = num.ToString();
  68. }
  69. LinkButton LinkBtnDeleteGroup = e.Item.FindControl("LinkBtnDeleteGroup") as LinkButton;
  70. LinkButton LinkBtnSendByGroup = e.Item.FindControl("LinkBtnSendByGroup") as LinkButton;
  71. LinkBtnDeleteGroup.Attributes.Add("OnClick", "return confirm('您确定要删除该分组?删除后该分组内的人员即将恢复到默认分组!')");
  72. LinkBtnDeleteGroup.Attributes.Add("OnClick", "return confirm('您确定要群发消息到该分组?')");
  73. }
  74. }
  75. /// <summary>
  76. /// 执行事件
  77. /// </summary>
  78. /// <param name="source"></param>
  79. /// <param name="e"></param>
  80. protected void RepeaterGroupList_ItemCommand(object source, RepeaterCommandEventArgs e)
  81. {
  82. if (e.CommandName == "DeleteGroups")
  83. {
  84. WeiXinServer wxs = new WeiXinServer();
  85. string res = "";
  86. //从缓存读取accesstoken
  87. string Access_token = Cache["Access_token"] as string;
  88. if (Access_token == null)
  89. {
  90. //如果为空,重新获取
  91. Access_token = wxs.GetAccessToken();
  92. //设置缓存的数据7000秒后过期
  93. Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000),
  94. System.Web.Caching.Cache.NoSlidingExpiration);
  95. }
  96. string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
  97. string posturl = "https://api.weixin.qq.com/cgi-bin/groups/delete?access_token=" + Access_tokento;
  98. //POST数据例子: POST数据例子:{"group":{"id":108}}
  99. string groupid = e.CommandArgument.ToString();
  100. string postData = "{\"group\":{\"id\":\"" + groupid + "\"}}";
  101. res = wxs.GetPage(posturl, postData);
  102. ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "",
  103. "alert('删除成功!由于缓存问题,您可能需要重新登录才能看到效果!');location='WxGroupManageList.aspx';", true);
  104. }
  105. if (e.CommandName == "SendByGroups")
  106. {
  107. WeiXinServer wxs = new WeiXinServer();
  108. string res = "";
  109. //从缓存读取accesstoken
  110. string Access_token = Cache["Access_token"] as string;
  111. if (Access_token == null)
  112. {
  113. //如果为空,重新获取
  114. Access_token = wxs.GetAccessToken();
  115. //设置缓存的数据7000秒后过期
  116. Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000),
  117. System.Web.Caching.Cache.NoSlidingExpiration);
  118. }
  119. string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
  120. string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=" + Access_tokento;
  121. string groupid = e.CommandArgument.ToString();
  122. //string postData = "{\"group\":{\"id\":\"" + groupid.ToString() + "\"}}";
  123. JObject postData = new JObject();
  124. JObject filter = new JObject();
  125. filter.Add("is_to_all", false);
  126. filter.Add("group_id", groupid);
  127. JObject text = new JObject();
  128. text.Add("content", "测试内容!");
  129. postData.Add("filter", filter);
  130. postData.Add("text", text);
  131. postData.Add("msgtype", "text");
  132. res = wxs.GetPage(posturl, postData.ToString());
  133. ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "",
  134. "alert('群发成功!由于缓存问题,您可能需要重新登录才能看到效果!');location='WxGroupManageList.aspx';", true);
  135. }
  136. if (e.CommandName == "MoveUserToGroup")
  137. {
  138. WeiXinServer wxs = new WeiXinServer();
  139. string res = "";
  140. string Access_token = Cache["Access_token"] as string;
  141. if (Access_token == null)
  142. {
  143. Access_token = wxs.GetAccessToken();
  144. Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
  145. }
  146. string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
  147. string posturl = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=" + Access_tokento;
  148. string UserListurl = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + Access_tokento + "&next_openid=";
  149. //{"total":2,"count":2,"data":{"openid":["","OPENID1","OPENID2"]},"next_openid":"NEXT_OPENID"}
  150. res = wxs.GetPage(UserListurl,"");
  151. UserList userList= JsonConvert.DeserializeObject<UserList>(res);
  152. String openid=userList.data.openid.FirstOrDefault();
  153. int togroup_id = 101;//输入分组ID
  154. string postData = "{\"openid\":\"" + openid + "\",\"to_groupid\":" + togroup_id + "}";
  155. res = wxs.GetPage(posturl, postData);
  156. ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "",
  157. "alert('移动分组成功!由于缓存问题,您可能需要重新登录才能看到效果!');location='WxGroupManageList.aspx';", true);
  158. }
  159. }
  160. /// <summary>
  161. /// 创建分组
  162. /// </summary>
  163. /// <param name="sender"></param>
  164. /// <param name="e"></param>
  165. protected void LinkBtnCreateGroup_Click(object sender, EventArgs e)
  166. {
  167. if (this.txtgroupsName.Value.Equals("分组名称"))
  168. {
  169. ////
  170. ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('不能为空!')", true);
  171. this.txtgroupsName.Focus();
  172. return;
  173. }
  174. WeiXinServer wxs = new WeiXinServer();
  175. string res = "";
  176. ///从缓存读取accesstoken
  177. string Access_token = Cache["Access_token"] as string;
  178. if (Access_token == null)
  179. {
  180. //如果为空,重新获取
  181. Access_token = wxs.GetAccessToken();
  182. //设置缓存的数据7000秒后过期
  183. Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000),
  184. System.Web.Caching.Cache.NoSlidingExpiration);
  185. }
  186. string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
  187. string posturl = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token=" + Access_tokento;
  188. string postData = "{\"group\":{\"name\":\"" + this.txtgroupsName.Value.ToString().Trim() + "\"}}";
  189. res = wxs.GetPage(posturl, postData);
  190. ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "",
  191. "alert('创建成功!如未显示,请退出重新登录即可!');location='WxGroupManageList.aspx';", true);
  192. }
  193. }

WeiXinServer.cs代码:

  1. /// <summary>
  2. /// 微信服务类
  3. /// </summary>
  4. public class WeiXinServer
  5. {
  6. /// <summary>
  7. /// 获取通行证
  8. /// </summary>
  9. /// <returns></returns>
  10. public string GetAccessToken()
  11. {
  12. string url_token =
  13. "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3eb5bf1290db2ca0&secret=e6013be0a7338c7d3e02877db116e231";
  14. HttpWebRequest myRequest = (HttpWebRequest) WebRequest.Create(url_token);
  15. myRequest.Method = "GET";
  16. HttpWebResponse myResponse = (HttpWebResponse) myRequest.GetResponse();
  17. StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  18. string content = reader.ReadToEnd();
  19. reader.Close();
  20. return content;
  21. }
  22. public string GetPage(string p, string postData)
  23. {
  24. Stream outstream = null;
  25. Stream instream = null;
  26. StreamReader sr = null;
  27. HttpWebResponse response = null;
  28. HttpWebRequest request = null;
  29. Encoding encoding = Encoding.UTF8;
  30. byte[] data = encoding.GetBytes(postData);
  31. // 准备请求...
  32. try
  33. {
  34. // 设置参数
  35. request = WebRequest.Create(p) as HttpWebRequest;
  36. CookieContainer cookieContainer = new CookieContainer();
  37. request.CookieContainer = cookieContainer;
  38. request.AllowAutoRedirect = true;
  39. request.Method = "POST";
  40. request.ContentType = "application/x-www-form-urlencoded";
  41. request.ContentLength = data.Length;
  42. outstream = request.GetRequestStream();
  43. outstream.Write(data, 0, data.Length);
  44. outstream.Close();
  45. //发送请求并获取相应回应数据
  46. response = request.GetResponse() as HttpWebResponse;
  47. //直到request.GetResponse()程序才开始向目标网页发送Post请求
  48. instream = response.GetResponseStream();
  49. sr = new StreamReader(instream, encoding);
  50. //返回结果网页(html)代码
  51. string content = sr.ReadToEnd();
  52. string err = string.Empty;
  53. return content;
  54. }
  55. catch (Exception ex)
  56. {
  57. string err = ex.Message;
  58. return string.Empty;
  59. }
  60. }
  61. }

Edit.aspx内容:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Edit.aspx.cs" Inherits="test.Edit" %>
  2. <html >
  3. <head runat="server">
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5. <title></title>
  6. <style type="text/css">
  7. .inputstyle {
  8. height: 35px;
  9. line-height: 35px;
  10. text-indent: 5px;
  11. width: 280px;
  12. background-image: url('images/inputbg.gif');
  13. background-repeat: repeat-x;
  14. border-top: solid 1px #a7b5bc;
  15. border-left: solid 1px #a7b5bc;
  16. border-right: solid 1px #ced9df;
  17. border-bottom: solid 1px #ced9df;
  18. float: left;
  19. margin: auto 5px auto 5px;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <form id="form1" runat="server">
  25. <table border="1" style="width: 500px; border-collapse: collapse; margin: 20px auto 20px auto; line-height: 40px;">
  26. <tr>
  27. <td style="text-align: right;">分组编号:</td>
  28. <td> <asp:TextBox ID="txtGroupId" CssClass="inputstyle" Enabled="false" runat="server"></asp:TextBox></td>
  29. </tr>
  30. <tr>
  31. <td style="text-align: right;">分组名称:</td>
  32. <td>
  33. <asp:TextBox ID="txtGroupName" CssClass="inputstyle" runat="server"></asp:TextBox>
  34. </td>
  35. </tr>
  36. <tr>
  37. <td></td>
  38. <td>
  39. <asp:LinkButton ID="LinkBtnSet" runat="server" OnClick="LinkBtnSet_Click" ><div style="background-image: url('images/buttonbg.png'); width: 111px; height: 35px; line-height: 35px; float: left; font-weight: bold; text-align: center; color: #fff;"> 设 置</div></asp:LinkButton></td>
  40. </tr>
  41. </table>
  42. </form>
  43. </body>
  44. </html>

Edit.aspx.cs代码:

  1. public partial class Edit : System.Web.UI.Page
  2. {
  3. protected void Page_Load(object sender, EventArgs e)
  4. {
  5. if (!Page.IsPostBack)
  6. {
  7. if (Request.QueryString["id"] != null)
  8. {
  9. string group_id = Request.QueryString["id"].ToString();
  10. string group_name = Request.QueryString["name"].ToString();
  11. this.txtGroupId.Text = group_id.ToString();
  12. this.txtGroupName.Text = group_name.ToString();
  13. this.txtGroupName.Focus();
  14. }
  15. }
  16. }
  17. /// <summary>
  18. /// 设置
  19. /// </summary>
  20. /// <param name="sender"></param>
  21. /// <param name="e"></param>
  22. protected void LinkBtnSet_Click(object sender, EventArgs e)
  23. {
  24. if (String.IsNullOrWhiteSpace(this.txtGroupName.Text.ToString().Trim()))
  25. {
  26. ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('不能为空!');", true);
  27. this.txtGroupName.Focus();
  28. return;
  29. }
  30. if (this.txtGroupName.Text.Trim().Length > 30)
  31. {
  32. ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('组名称应在30个字符之内!');", true);
  33. this.txtGroupName.Focus();
  34. return;
  35. }
  36. WeiXinServer wxs = new WeiXinServer();
  37. string res = "";
  38. //从缓存读取accesstoken
  39. string Access_token = Cache["Access_token"] as string;
  40. if (Access_token == null)
  41. {
  42. //如果为空,重新获取
  43. Access_token = wxs.GetAccessToken();
  44. //设置缓存的数据7000秒后过期
  45. Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000),
  46. System.Web.Caching.Cache.NoSlidingExpiration);
  47. }
  48. string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
  49. string posturl = "https://api.weixin.qq.com/cgi-bin/groups/update?access_token=" + Access_tokento;
  50. //POST数据例子:POST数据例子:{"group":{"id":108,"name":"test2_modify2"}}
  51. string postData = "{\"group\":{\"id\":\"" + txtGroupId.Text + "\",\"name\":\"" + this.txtGroupName.Text +
  52. "\"}}";
  53. res = wxs.GetPage(posturl, postData);
  54. //使用Newtonsoft.json
  55. JObject jsonObj = JObject.Parse(res);
  56. //获取返回结果的正确|true|false,
  57. string isright = jsonObj["errcode"].ToString(); //0
  58. string istrueorfalse = jsonObj["errmsg"].ToString(); //ok
  59. if (isright.Equals("0") && istrueorfalse.Equals("ok"))
  60. {
  61. //修改成功之后,刷新父窗体,关闭本页
  62. ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "",
  63. "alert('修改成功!如未正常显示,属缓存问题,请重新登录即可!');window.open![这里写图片描述](http://img.blog.csdn.net/20161008103126694)er.location.reload();this.close();", true);
  64. }
  65. else
  66. {
  67. ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('修改失败!');this.close();",
  68. true);
  69. }
  70. }
  71. }

buttonbg.png

结果如图:

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

人气教程排行