In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to achieve user grouping information management in C#". The content is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn the article "how to achieve user grouping information management in C#".
1. User grouping management content
The introduction of user grouping is mainly to facilitate the management of follower lists and the operation of sending messages to different groups, a public account that supports the creation of up to 500 groups.
User grouping management, including the following aspects:
1 create a grouping
2 query all groups
3 query the user's grouping
4 modify the group name
5 Mobile user grouping
Wechat's definition of creating a group is as follows.
Http request method: POST (please use https protocol) https://api.weixin.qq.com/cgi-bin/groups/create?access_token=ACCESS_TOKENPOST data format: jsonPOST data example: {"group": {"name": "test"}}
The result returned normally is as follows.
{"group": {"id": 107, "name": "test"}}
Other interfaces, in a similar way, enter some parameters into the URL through POST to get the returned Json data.
The entity class information for GroupJson defined in the previous essay is shown below.
/ grouping information / public class GroupJson: BaseJsonResult {/ / BaseJsonResult / grouping id, assigned by Wechat / public int id {get; set;} / grouped name, UTF8 code / public string name {get; set;}}
According to the definition of the above interfaces, I defined several interfaces and summarized them into the user-managed API interface.
/ query all grouping / call API credentials / List GetGroupList (string accessToken) / create packet / call API credential / packet name / GroupJson CreateGroup (string accessToken, string name) / query the packet to which the user belongs / call the API credential / the user's OpenID / int GetUserGroupId (string accessToken, string openid) / / modify the grouping name / call API credential / grouping id. The grouping name (within 30 characters) / CommonResult UpdateGroupName (string accessToken, int id, string name) is assigned by Wechat. / Mobile user grouping / calling interface credential / user's OpenID / / packet id / CommonResult MoveUserToGroup (string accessToken, string openid, int to_groupid); 2. Implementation of user packet management interface
2.1 create user grouping
To understand how to implement the POST data operation of creating user groups, let's take a step-by-step look at the specific process of creating users.
First you need to create a dynamically defined entity class information that contains several attributes that need to be mentioned, as shown below.
String url = string.Format ("http://www.php.cn/{0}", accessToken); var data = new {group = new {name = name}}; string postData = data.ToJson ()
Among them, we convert the object into the appropriate Json data operation, and put it into the extension method ToJson, which is mainly to facilitate the dynamic definition of entity classes to convert Json content, mainly to call the sequence number operation of Json.NET.
/ make the object a json string / public static string ToJson (this object obj) {return JsonConvert.SerializeObject (obj, Formatting.Indented);}
Once the Post data is ready, let's take a closer look at the operation code that takes the data and converts it to the appropriate format.
GroupJson group = null; CreateGroupResult result = JsonHelper.ConvertJson (url, postData); if (result! = null) {group = result.group;}
The operation of converting the POST data into the entity class of the appropriate format is placed in the ConvertJson method. The definition of this method is as follows. The HttpHelper in this method is the auxiliary class of my common class library. The main purpose is to call the underlying httpWebRequest object method, submit the data, and get the returned result.
/ convert Json string to specific object / return link address of Json data / POST submitted data / public static T ConvertJson (string url, string postData) {HttpHelper helper = new HttpHelper (); string content = helper.GetHtml (url, postData, true) VerifyErrorCode (content); T result = JsonConvert.DeserializeObject (content); return result;}
In this way, the complete operation function for creating a user group is shown below.
/ create grouping / call API credential / grouping name / public GroupJson CreateGroup (string accessToken, string name) {string url = string.Format ("http://www.php.cn/{0}", accessToken) Var data = new {group = new {name = name}}; string postData = data.ToJson (); GroupJson group = null; CreateGroupResult result = JsonHelper.ConvertJson (url, postData) If (result! = null) {group = result.group;} return group;}
2.2 query all groups
By querying all the groups, you can get all the packets on the server, that is, the ID and name of each packet.
/ query all packets / public List GetGroupList (string accessToken) {string url = string.Format ("http://www.php.cn/{0}", accessToken); List list = new List (); GroupListJsonResult result = JsonHelper.ConvertJson (url) If (result! = null & & result.groups! = null) {list.AddRange (result.groups);} return list;}
2.3 query the user's grouping
Each user belongs to a grouping. By default, in the ungrouped group, we can obtain the user's grouping information through API, that is, the ID of the user's grouping.
/ query the packet to which the user belongs / call the API credential / the user's OpenID / public int GetUserGroupId (string accessToken, string openid) {string url = string.Format ("http://www.php.cn/{0}", accessToken)" Var data = new {openid = openid}; string postData = data.ToJson (); int groupId =-1; GroupIdJsonResult result = JsonHelper.ConvertJson (url, postData); if (result! = null) {groupId = result.groupid;} return groupId }
2.4 modify the group name
In practice, you can also adjust the grouping in which the user is located, as follows.
/ modify the grouping name / call the API credential / grouping id Assigned by Wechat / grouping name (within 30 characters) / public CommonResult UpdateGroupName (string accessToken, int id, string name) {string url = string.Format ("http://www.php.cn/{0}", accessToken) Var data = new {group = new {id = id, name = name}}; string postData = data.ToJson (); return Helper.GetExecuteResult (url, postData);}
The return value CommonResult here is an entity class that contains a flag for the success of bool, as well as an error message of type String, if any.
For this GetExecuteResult function body, it is mainly a function that submits the data, then gets the result, and processes it according to the result.
/ General operation result / Web address / submitted data content / public static CommonResult GetExecuteResult (string url, string postData = null) {CommonResult success = new CommonResult (); try {ErrorJsonResult result If (postData! = null) {result = JsonHelper.ConvertJson (url, postData);} else {result = JsonHelper.ConvertJson (url) } if (result! = null) {success.Success = (result.errcode = = ReturnCode. Request succeeded); success.ErrorMessage = result.errmsg;}} catch (WeixinException ex) {success.ErrorMessage = ex.Message;} return success;}}
The red part above means that when converting to an entity class, if the error is defined in Wechat, then record the error message, and I will not handle other exceptions (that is, throw).
2.5 move users to a new group
The operation of moving users to the new grouping is similar to that in the above section, depending on the code.
/ Mobile user grouping / call API credential / user's OpenID / / grouping id / public CommonResult MoveUserToGroup (string accessToken, string openid, int to_groupid) {string url = string.Format ("http://www.php.cn/{0}", accessToken) Var data = new {openid = openid, to_groupid = to_groupid}; string postData = data.ToJson (); return Helper.GetExecuteResult (url, postData);} 3. Call of user grouping API
In the above section, various interfaces for user grouping are defined and implemented, and all user-related codes have been posted without reservation, and its calling operation is shown in the following code (test code).
Private void btnGetGroupList_Click (object sender, EventArgs e) {IUserApi userBLL = new UserApi (); List list = userBLL.GetGroupList (token); foreach (GroupJson info in list) {string tips = string.Format ("{0}: {1}", info.name, info.id); Console.WriteLine (tips) }} private void btnFindUserGroup_Click (object sender, EventArgs e) {IUserApi userBLL = new UserApi (); int groupId = userBLL.GetUserGroupId (token, openId); string tips = string.Format ("GroupId: {0}", groupId); Console.WriteLine (tips) } private void btnCreateGroup_Click (object sender, EventArgs e) {IUserApi userBLL = new UserApi (); GroupJson info = userBLL.CreateGroup (token, "create test grouping"); if (info! = null) {string tips = string.Format ("GroupId: {0} GroupName: {1}", info.id, info.name) Console.WriteLine (tips); string newName = "create test modifications"; CommonResult result = userBLL.UpdateGroupName (token, info.id, newName); Console.WriteLine ("modify group name:" + (result.Success? "success": "failure:" + result.ErrorMessage);}} private void btnUpdateGroup_Click (object sender, EventArgs e) {int groupId = 111; string newName = "create test changes"; IUserApi userBLL = new UserApi (); CommonResult result = userBLL.UpdateGroupName (token, groupId, newName) Console.WriteLine ("modify grouping name:" + (result.Success? "success": "failure:" + result.ErrorMessage);} private void btnMoveToGroup_Click (object sender, EventArgs e) {int togroup_id = 111 new UserApi / input packet ID if (togroup_id > 0) {IUserApi userBLL = new UserApi (); CommonResult result = userBLL.MoveUserToGroup (token, openId, togroup_id) Console.WriteLine ("Mobile user Group name:" + (result.Success? "success": "failure:" + result.ErrorMessage);}}
Knowing the above code and calling rules, we can manage the user grouping information through API. By integrating the relevant interface code in the application, we can well control our follow user list and user grouping information. So as to lay a good foundation for our next step of user information push.
The above is all the contents of the article "how to achieve user grouping Information Management in C#". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.