国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
1. User group management content
2. Implementation of user group management interface
3. Calling the user grouping interface
Home WeChat Applet WeChat Development C# Development of WeChat Portal and Application (5)--User Group Information Management

C# Development of WeChat Portal and Application (5)--User Group Information Management

Feb 16, 2017 pm 04:37 PM

Last month I introduced the development of WeChat portals and applications in C#, and wrote several essays to share. Due to time constraints, I have not continued writing this series of blogs for a while. It is not a review of this series. We stopped researching, but continued to explore the technology in this area in depth. In order to better apply it, we concentrated on the development of the underlying technology. This article continues the introduction of the previous article, mainly introducing the development and application of group management. The content of this article and the previous article serve 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 follower lists and the operation of sending messages to different groups. One public account supports up to Create 500 groups.

User group management includes the following aspects:

1 Create a group
2 Query all groups
3 Query the group the user belongs to
4 Modify the group name
5 Mobile User Groups

WeChat’s definition of creating groups is as follows.

http請求方式:?POST(請使用https協(xié)議)
https://api.weixin.qq.com/cgi-bin/groups/create?access_token=ACCESS_TOKENPOST數(shù)據(jù)格式:json
POST數(shù)據(jù)例子:{"group":{"name":"test"}}

The normally returned results are as follows.

{????"group":?{????????"id":?107,?
????????"name":?"test"
????}
}

Other interfaces work in a similar way, by POSTing some parameters into the URL to obtain the returned Json data.

The entity class information of GroupJson defined in the previous essay is as follows.

????///?<summary>
????///?分組信息
????///?</summary>????public?class?GroupJson?:?BaseJsonResult
????{????????///?<summary>
????????///?分組id,由微信分配
????????///?</summary>
????????public?int?id?{?get;?set;?}????????///?<summary>
????????///?分組名字,UTF8編碼
????????///?</summary>????????public?string?name?{?get;?set;?}
????}

Based on the definitions of the above interfaces, I defined several interfaces and summarized them into the user management API interface.

????????///?<summary>
????????///?查詢所有分組????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<returns></returns>
????????List<GroupJson>?GetGroupList(string?accessToken);???????????????????????
????????///?<summary>
????????///?創(chuàng)建分組????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="name">分組名稱</param>
????????///?<returns></returns>
????????GroupJson?CreateGroup(string?accessToken,?string?name);????????????????????????
????????///?<summary>
????????///?查詢用戶所在分組????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="openid">用戶的OpenID</param>
????????///?<returns></returns>
????????int?GetUserGroupId(string?accessToken,?string?openid);????????
????????///?<summary>
????????///?修改分組名????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="id">分組id,由微信分配</param>
????????///?<param name="name">分組名字(30個字符以內(nèi))</param>
????????///?<returns></returns>
????????CommonResult?UpdateGroupName(string?accessToken,?int?id,?string?name);???????????????????????
????????///?<summary>
????????///?移動用戶分組????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="openid">用戶的OpenID</param>
????????///?<param name="to_groupid">分組id</param>
????????///?<returns></returns>
????????CommonResult?MoveUserToGroup(string?accessToken,?string?openid,?int?to_groupid);

2. Implementation of user group management interface

2.1 Create user group

In order to analyze how to implement the POST data operation of creating user groups, let’s understand the specific process of creating users step by step.

First you need to create a dynamically defined entity class information, which contains several attributes that need to be mentioned, as shown below.

????????????string?url?=?string.Format("http://m.miracleart.cn/{0}",?accessToken);????????????var?data?=?new
????????????{
????????????????group?=?new
????????????????{
????????????????????name?=?name
????????????????}
????????????};????????????string?postData?=?data.ToJson();

We convert the object into a suitable Json data operation and put it in the extension method ToJson. This is mainly to facilitate the conversion of dynamically defined entity classes. Json content is mainly to call the serial number operation of Json.NET.

????????///?<summary>
????????///?把對象為json字符串????????///?</summary>
????????///?<param name="obj">待序列號對象</param>
????????///?<returns></returns>
????????public?static?string?ToJson(this?object?obj)
????????{????????????return?JsonConvert.SerializeObject(obj,?Formatting.Indented);
????????}

After preparing the Post data, we will further look at the operation code to obtain the data and convert it into a suitable format.

????????????GroupJson?group?=?null;
????????????CreateGroupResult?result?=?JsonHelper<CreateGroupResult>.ConvertJson(url,?postData);????????????if?(result?!=?null)
????????????{
????????????????group?=?result.group;
????????????}

The operation of POST data and converting it into a suitable format entity class is placed in the ConvertJson method. The definition of this method is as follows, and the HttpHelper inside It is an auxiliary class of my public class library. It mainly calls the underlying httpWebRequest object method to submit data and obtain the return result.

????????///?<summary>
????????///?轉(zhuǎn)換Json字符串到具體的對象????????///?</summary>
????????///?<param name="url">返回Json數(shù)據(jù)的鏈接地址</param>
????????///?<param name="postData">POST提交的數(shù)據(jù)</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 operation function for creating user groups is as follows.

????????///?
????????///?創(chuàng)建分組????????///?
????????///?調(diào)用接口憑證
????????///?分組名稱
????????///?
????????public?GroupJson?CreateGroup(string?accessToken,?string?name)
????????{????????????string?url?=?string.Format("http://m.miracleart.cn/{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 Query all groups

Query all groups, you can get all the groups on the server, also It is the ID and name of each group.

????????///?<summary>
????????///?查詢所有分組????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<returns></returns>
????????public?List<GroupJson>?GetGroupList(string?accessToken)
????????{????????????string?url?=?string.Format("http://m.miracleart.cn/{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 Query the group the user belongs to

Each user belongs to a group, the default is Group In this group, we can obtain the user's group information through the API, that is, obtain the ID of the user group.

????????///?<summary>
????????///?查詢用戶所在分組????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="openid">用戶的OpenID</param>
????????///?<returns></returns>
????????public?int?GetUserGroupId(string?accessToken,?string?openid)
????????{????????????string?url?=?string.Format("http://m.miracleart.cn/{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 Modify the group name

You can also adjust the group the user is in in practice. The operation code is as follows .

????????///?<summary>
????????///?修改分組名????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="id">分組id,由微信分配</param>
????????///?<param name="name">分組名字(30個字符以內(nèi))</param>
????????///?<returns></returns>
????????public?CommonResult?UpdateGroupName(string?accessToken,?int?id,?string?name)
????????{????????????string?url?=?string.Format("http://m.miracleart.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 bool flag of success or failure, and an error message of String type (if any talk).

For the GetExecuteResult function body, it is mainly a function that submits data, then obtains the results, and processes them based on the results.

????????///?<summary>
????????///?通用的操作結(jié)果????????///?</summary>
????????///?<param name="url">網(wǎng)頁地址</param>
????????///?<param name="postData">提交的數(shù)據(jù)內(nèi)容</param>
????????///?<returns></returns>
????????public?static?CommonResult?GetExecuteResult(string?url,?string?postData?=?null)
????????{
????????????CommonResult?success?=?new?CommonResult();????????????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.請求成功);
????????????????????success.ErrorMessage?=?result.errmsg;
????????????????}
????????????}????????????catch?(WeixinException?ex)
????????????{????????????????success.ErrorMessage?=?ex.Message;
????????????}????????????return?success;
????????}??
????}

The meaning of the red part above is that when converting to an entity class, if the error is defined in WeChat, then the error message will be recorded. I will not record other exceptions. Process (i.e. throw out).

2.5 Move the user to a new group

The operation of moving the user to a new group is similar to the above section, please see the code for details.

????????///?<summary>
????????///?移動用戶分組????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="openid">用戶的OpenID</param>
????????///?<param name="to_groupid">分組id</param>
????????///?<returns></returns>
????????public?CommonResult?MoveUserToGroup(string?accessToken,?string?openid,?int?to_groupid)
????????{????????????string?url?=?string.Format("http://m.miracleart.cn/{0}",?accessToken);????????????var?data?=?new
????????????{
????????????????openid?=?openid,
????????????????to_groupid?=?to_groupid
????????????};????????????string?postData?=?data.ToJson();????????????return?Helper.GetExecuteResult(url,?postData);
????????}

3. Calling the user grouping interface

The above section defines and implements various types of user grouping Interface, all user-related code has 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<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,?"創(chuàng)建測試分組");????????????if?(info?!=?null)
????????????{????????????????string?tips?=?string.Format("GroupId:{0}?GroupName:{1}",?info.id,?info.name);
????????????????Console.WriteLine(tips);????????????????string?newName?=?"創(chuàng)建測試修改";
????????????????CommonResult?result?=?userBLL.UpdateGroupName(token,?info.id,?newName);
????????????????Console.WriteLine("修改分組名稱:"?+?(result.Success???"成功"?:?"失敗:"?+?result.ErrorMessage));
????????????}
????????}????????private?void?btnUpdateGroup_Click(object?sender,?EventArgs?e)
????????{????????????int?groupId?=?111;????????????string?newName?=?"創(chuàng)建測試修改";

????????????IUserApi?userBLL?=?new?UserApi();
????????????CommonResult?result?=?userBLL.UpdateGroupName(token,?groupId,?newName);
????????????Console.WriteLine("修改分組名稱:"?+?(result.Success???"成功"?:?"失敗:"?+?result.ErrorMessage));
????????}????????private?void?btnMoveToGroup_Click(object?sender,?EventArgs?e)
????????{????????????int?togroup_id?=?111;//輸入分組ID

????????????if?(togroup_id?>?0)
????????????{
????????????????IUserApi?userBLL?=?new?UserApi();
????????????????CommonResult?result?=?userBLL.MoveUserToGroup(token,?openId,?togroup_id);

????????????????Console.WriteLine("移動用戶分組名稱:"?+?(result.Success???"成功"?:?"失敗:"?+?result.ErrorMessage));
????????????}
????????}

After understanding the above code and calling rules, we can manage user group information through the API. By integrating relevant interface code in the application, we can have good control over our following user list and user group information. This will lay a solid foundation for our next push of user information.

For more C# development of WeChat portals and applications (5)--User group information management, please pay attention to the PHP Chinese website for related articles!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)