This article mainly for you to introduce the C # development portal and application of the fifth, the user group information management, with a certain reference value, interested in small partners can refer to
In the last month of the C # development portal and application of the introduction, wrote a few essays to share, because of time, interval for a period of time did not continue to write this series of blogs, not to this aspect of the study, but continue to explore this aspect of technology, in order to better apply, Concentrate on the technical development of the bottom. This article continues on an introduction, mainly introduces the development and application of group management, this article and the previous one, as a complete combination of user information and group information management.
1, user group management content
The introduction of user groups is mainly to facilitate the management of the list of followers, as well as easy to send messages to different groups of operations, a public account, up to support the creation of 500 groups.
User group management, which contains the following aspects:
1 Creating groupings
2 Querying all groupings
3 Querying groups of users
4 Modifying Group names
5 Mobile User Grouping
The definition for creating a grouping is as follows.
HTTP request mode: POST (please use HTTPS protocol)
Https://api.weixin.qq.com/cgi-bin/groups/create?access_token=ACCESS_TOKEN
Post data format: JSON
Post Data example: {"group": {"name": "Test"}}
The results of the normal return are as follows.
{" Group": { "id": 107, "name": "Test" }}
Other interfaces, too, are in a similar way, by post some parameters into the URL inside to get the JSON data returned.
The previous essay defines the entity class information for Groupjson as follows.
<summary>///Group information/// </summary> public class Groupjson:basejsonresult { // <summary>//// group ID, assigned by// </summary> public int ID {get; set;} <summary>//// group name, UTF8 Code/// </summary> public string name {get; set;} }
Based on the definitions of the above interfaces, I have defined several interfaces and summed them up in user-managed API interfaces.
<summary>///Check all groups///</summary>/<param name= "Accesstoken" > Invoke interface Credentials </param> <returns></returns> list<groupjson> getgrouplist (string accesstoken); <summary>//Create Groups///</summary>/<param name= "Accesstoken" > Invoke interface Credentials </param>// <param name= "name" > Group name </param>//<returns></returns> Groupjson CreateGroup (String access Token, string name); <summary>///Query the user's group///</summary>/<param name= "Accesstoken" > Invoke interface Credentials </param> <param name= "OpenID" > User's openid</param>//<returns></returns> int getusergroupid (strin G Accesstoken, String OpenID); <summary>//Modify Group name///</summary>//<param name= "Accesstoken" > Invoke interface Credentials </param>// /<param name= "id" > Group ID, by assignment </param>//<param name= "name" > minutesGroup name (within 30 characters) </param>//<returns></returns> commonresult updategroupname (string accesstoken, int ID, string name); <summary>///Mobile user groups///</summary>/<param name= "Accesstoken" > Invoke interface Credentials </param>/ <param name= "OpenID" > User openid</param>//<param name= "To_groupid" > Group id</param>//< Returns></returns> commonresult Moveusertogroup (String Accesstoken, string OpenID, int to_groupid);
2. Implementation of user group management interface
2.1 Creating User Groups
To resolve how to implement post data operations that create user groupings, let's step through the process of creating a user.
You first 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 ("https://api.weixin.qq.com/cgi-bin/groups/create?access_token={0}", Accesstoken); var data = new { Group = New { name = name }};string postdata = data. ToJson ();
In which we convert the object into the appropriate JSON data operation, put in the extension method Tojson inside, this is to facilitate the dynamic definition of the entity class to convert JSON content, mainly called Json.NET serial number operation.
<summary>///object as JSON string///</summary>///<param name= "obj" > Pending serial Number Object </param>///< Returns></returns>public static string ToJson (This object obj) { return jsonconvert.serializeobject (obj, formatting.indented); }
Once the post data is ready, we'll take a closer look at the operation code that gets the data and transforms it into the appropriate format.
Groupjson group = null; Creategroupresult result = Jsonhelper<creategroupresult>. Convertjson (URL, postdata); if (result! = null) { group = Result.group;}
Where the post data and conversion to the appropriate format entity class operation, placed in the Convertjson method, the definition of this method is as follows, the inside of the httphelper is my common class library auxiliary class, mainly is to call the underlying HttpWebRequest object method, Commits the data and obtains the returned results.
<summary>///convert the JSON string to a specific object///</summary>///<param name= "url" > return the link address of the JSON data </param>// /<param name= "PostData" >post submitted data </param>///<returns></returns> 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<t> (content); return result; }
In this way, the complete set of operations functions for creating user groups is shown below.
<summary>//Create Group/// </summary>/ <param name= "Accesstoken" > Invoke interface Voucher </param >// <param name= "name" > Group name </param>// <returns></returns> public Groupjson CreateGroup (String accesstoken, string name) { string url = string. Format ("https://api.weixin.qq.com/cgi-bin/groups/create?access_token={0}", Accesstoken); var data = new { group = new { name = name } }; string postdata = data. ToJson (); Groupjson group = null; Creategroupresult result = Jsonhelper<creategroupresult>. Convertjson (URL, postdata); if (result! = null) { group = Result.group; } return group; }
2.2 Querying all groupings
Querying all the groupings, you can get all the groupings on the server, that is, the ID and name of each group.
<summary>/// Query all Groups/// </summary>/ <param name= "Accesstoken" > Invoke interface Voucher </ param> //<returns></returns> public list<groupjson> getgrouplist (string Accesstoken ) { string url = string. Format ("https://api.weixin.qq.com/cgi-bin/groups/get?access_token={0}", Accesstoken); list<groupjson> list = new list<groupjson> (); Grouplistjsonresult result = Jsonhelper<grouplistjsonresult>. Convertjson (URL); if (result! = NULL && result.groups! = null) { list. AddRange (result.groups); } return list; }
2.3 Querying groups of users
Each user belongs to a group, by default in the group is not grouped, we can get the user's group information through the API, that is, to get the ID of the user group.
<summary>/// Query user group/// </summary>/ <param name= "Accesstoken" > Invoke interface Voucher </ param>// <param name= "OpenID" > User's openid</param>// <returns></returns> public int Getusergroupid (string accesstoken, String OpenID) { string url = string. Format ("https://api.weixin.qq.com/cgi-bin/groups/getid?access_token={0}", Accesstoken); var data = new { OpenID = OpenID }; string postdata = data. ToJson (); int groupId =-1; Groupidjsonresult result = Jsonhelper<groupidjsonresult>. Convertjson (URL, postdata); if (result! = null) { groupId = result.groupid; } return groupId; }
2.4 Modifying group names
You can also adjust the user's group in the actual, the operation code is as follows.
<summary>//Modify Group name///</summary>// <param name= "Accesstoken" > Invoke interface Voucher </param > //<param name= "id" > Group ID, by assignment </param> //<param name= "name" > Group name (within 30 characters) </ param> //<returns></returns> public Commonresult updategroupname (string accesstoken, int ID , string name) { string url = string. Format ("https://api.weixin.qq.com/cgi-bin/groups/update?access_token={0}", Accesstoken); var data = new { group = new { id = id, name = Name } }; string postdata = data. ToJson (); Return Helper.getexecuteresult (URL, postdata); }
The return value here, Commonresult, is an entity class that contains a symbol of the success or failure of bool, and a string type of error message, if any.
For this getexecuteresult function body, it is the function that submits the data, then obtains the result, and processes it according to the result.
///<summary>//General operating results///</summary>//<param name= "url" > net Page address </param>//<param name= "PostData" > Submitted data Content </param>//<returns></returns> Publ IC Static Commonresult getexecuteresult (string url, string postdata = null) {Commonresult success = new Commonres Ult (); try {errorjsonresult result; if (postdata! = null) {result = Jsonhelper<errorjsonresult>. Convertjson (URL, postdata); } else {result = Jsonhelper<errorjsonresult>. 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 of the above means that when converting to an entity class, if the error is defined inside, then the error message is logged, other exceptions I do not handle (that is, throw away).
2.5 Moving users to a new grouping
Move the user to the new grouping of the operation and the above section of the similar, specific look at the code.
<summary>/// Mobile User Group/// </summary>/ <param name= "Accesstoken" > Invoke interface Voucher </ param>// <param name= "OpenID" > User openid</param>// <param name= "To_groupid" > Group ID </param> //<returns></returns> public Commonresult moveusertogroup (String Accesstoken, string OpenID, int to_groupid) { string url = string. Format ("https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token={0}", Accesstoken); var data = new { OpenID = OpenID, to_groupid = To_groupid }; string postdata = data. ToJson (); Return Helper.getexecuteresult (URL, postdata); }
3, the user group interface call
The above section, defines and implements the user group the various kinds of interfaces, all the user related all has no reservation to post the code, it calls the operation as follows the code to be shown (test code).
private void Btngetgrouplist_click (object sender, EventArgs e) {Iuserapi USERBLL = new Userapi (); list<groupjson> 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 Group"); if (info! = null) {string tips = string. Format ("groupid:{0} Groupname:{1}", Info.id, Info.name); Console.WriteLine (tips); String newName = "Create Test modification"; Commonresult result = Userbll.updategroupname (token, Info.id, NewName); Console.WriteLine ("Modify Group Name:" + (Result). Success? "Success": "Failed:" + result. errormessage)); }} private void Btnupdategroup_click (object sender, EventArgs e) {int groupId = 111; String newName = "Create Test modification"; Iuserapi USERBLL = new Userapi (); Commonresult result = Userbll.updategroupname (token, groupId, newName); Console.WriteLine ("Modify Group Name:" + (Result). Success? "Success": "Failed:" + result. errormessage)); } private void Btnmovetogroup_click (object sender, EventArgs e) {int togroup_id = 111;//input Group ID if (Togrou p_id > 0) {iuserapi USERBLL = new Userapi (); Commonresult result = Userbll.moveusertogroup (token, openId, togroup_id); Console.WriteLine ("Mobile User Group Name:" + (Result). Success? "Success": "Failed:" + result. errormessage)); } }
Understand the above code and call rules, we can use the API to manage the user group information. By integrating the relevant interface code into the application, we are able to control our user list and user group information with great care. So as to our next user's information push to lay a good foundation.