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

Table of Contents
1. Following user list and user group information
2. Obtain the Token of the AIP caller
3. Get the list of following users
4、獲取用戶詳細信息
Home WeChat Applet WeChat Development C# Development of WeChat Portal and Application (4)--Follow user list and detailed information management

C# Development of WeChat Portal and Application (4)--Follow user list and detailed information management

Feb 16, 2017 pm 04:34 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.

A very important feature of WeChat is that it can take advantage of the huge user base of its platform, so it is easy to integrate into the CRM (Customer Relationship Management) system. Both service accounts and subscriptions can push relevant information to followers. Product news, and interactive conversations with active users who responded to messages and events within 48 hours. Therefore, user information is a very important part of the WeChat API. This essay mainly introduces how to obtain following users, view user information, group management, etc. Develop applications.

1. Following user list and user group information

On the WeChat management platform, we can see the follower users of our account and user group information, as shown below.

C#開發(fā)微信門戶及應用(4)--關注用戶列表及詳細信息管理

In the above management interface, you can see the basic information of follower users, but what you get using the WeChat API is a list called OpenID. Let’s understand this first. What is the thing? The description of the WeChat API gives the following analysis:

The follower list consists of a string of OpenIDs (encrypted WeChat IDs). Each user’s OpenID for each public account is unique. For different public accounts, the same The user's openid is different). Official accounts can use this interface to obtain basic user information based on OpenID, including nickname, avatar, gender, city, language and follow time.

The meaning of the above analysis is very clear, that is, if a user follows our official account, no matter how many times he follows it, it is a certain value for our official account; however, a user For other public accounts, they have different OpenIDs.

WeChat provides a few keyword information to record user-related content. Based on the user's relevant definitions, we define an entity class to place the retrieved user information.

????///?<summary>
????///?高級接口獲取的用戶信息。????///?在關注者與公眾號產生消息交互后,公眾號可獲得關注者的OpenID????///?(加密后的微信號,每個用戶對每個公眾號的OpenID是唯一的。對于不同公眾號,同一用戶的openid不同)。????///?公眾號可通過本接口來根據(jù)OpenID獲取用戶基本信息,包括昵稱、頭像、性別、所在城市、語言和關注時間。????///?</summary>
????public?class?UserJson?:?BaseJsonResult
????{????????///?<summary>
????????///?用戶是否訂閱該公眾號標識,值為0時,代表此用戶沒有關注該公眾號,拉取不到其余信息。????????///?</summary>
????????public?int?subscribe?{?get;?set;?}????????///?<summary>
????????///?用戶的標識,對當前公眾號唯一????????///?</summary>
????????public?string?openid?{?get;?set;?}????????///?<summary>
????????///?用戶的昵稱????????///?</summary>
????????public?string?nickname?{?get;?set;?}????????///?<summary>
????????///?用戶的性別,值為1時是男性,值為2時是女性,值為0時是未知????????///?</summary>
????????public?int?sex?{?get;?set;?}????????///?<summary>
????????///?用戶的語言,簡體中文為zh_CN????????///?</summary>
????????public?string?language?{?get;?set;?}????????///?<summary>
????????///?用戶所在城市????????///?</summary>
????????public?string?city?{?get;?set;?}????????///?<summary>
????????///?用戶所在省份????????///?</summary>
????????public?string?province?{?get;?set;?}????????///?<summary>
????????///?用戶所在國家????????///?</summary>
????????public?string?country?{?get;?set;?}????????///?<summary>
????????///?用戶頭像,最后一個數(shù)值代表正方形頭像大?。ㄓ?、46、64、96、132數(shù)值可選,0代表640*640正方形頭像),用戶沒有頭像時該項為空????????///?</summary>
????????public?string?headimgurl?{?get;?set;?}????????///?<summary>
????????///?用戶關注時間,為時間戳。如果用戶曾多次關注,則取最后關注時間????????///?</summary>
????????public?long?subscribe_time?{?get;?set;?}
????}

According to the grouping information definition, we define a grouped entity class information.

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

2. Obtain the Token of the AIP caller

When developing WeChat API, many times we need to pass in a AccessToken, this is a string that distinguishes the caller and records session information. Therefore, before learning all API development, we need to have a good understanding of this access control parameter.

C#開發(fā)微信門戶及應用(4)--關注用戶列表及詳細信息管理

#We can understand the definition of this object from WeChat’s API description.

access_token is the globally unique ticket of the public account. The public account needs to use access_token when calling each interface. Under normal circumstances, access_token is valid for 7200 seconds. Repeated acquisition will cause the last access_token to become invalid. Since the number of api calls to obtain access_token is very limited, it is recommended that developers store and update access_token globally. Frequent refresh of access_token will limit api calls and affect their own business.

According to the above definition, we can see that it is a parameter related to identity and session time, and there is a limit to the number of times it can be generated, so we need to It is cached and reused. Before the session expires, we should reuse this parameter as much as possible to avoid repeated requests, increase server pressure, and call time.

I have defined a method to construct and generate related Access Token, and it has the function of caching, but how to cache and use it is transparent to the call to my API. When we use it, Just call it.

????????///?獲取憑證接口????????///?</summary>
????????///?<param name="appid">第三方用戶唯一憑證</param>
????????///?<param name="secret">第三方用戶唯一憑證密鑰,既appsecret</param>
????????string?GetAccessToken(string?appid,?string?secret);

The cache is mainly based on the MemoryCache class library added in .NET4. This is a very good cache class.

My operation implementation code for obtaining AccessToken is as follows.

????????///?<summary>
????????///?獲取每次操作微信API的Token訪問令牌????????///?</summary>
????????///?<param name="appid">應用ID</param>
????????///?<param name="secret">開發(fā)者憑據(jù)</param>
????????///?<returns></returns>
????????public?string?GetAccessToken(string?appid,?string?secret)
????????{????????????//正常情況下access_token有效期為7200秒,這里使用緩存設置短于這個時間即可
????????????string?access_token?=?MemoryCacheHelper.GetCacheItem<string>("access_token",?delegate()
????????????????{????????????????????string?grant_type?=?"client_credential";????????????????????var?url?=?string.Format("http://www.miracleart.cn/{0}&appid={1}&secret={2}",
????????????????????????????????????????????grant_type,?appid,?secret);

????????????????????HttpHelper?helper?=?new?HttpHelper();????????????????????string?result?=?helper.GetHtml(url);????????????????????string?regex?=?"\"access_token\":\"(?<token>.*?)\"";
????????????????????string?token?=?CRegex.GetText(result,?regex,?"token");????????????????????return?token;
????????????????},????????????????new?TimeSpan(0,?0,?7000)//7000秒過期????????????);????????????return?access_token;????????????
????????}

Since we know that AccessToken expires in 7200 seconds by default, so during this time period, we try to use the cache to record its value. If it exceeds After this time, when we call this method, it will automatically obtain a new value for us.

3. Get the list of following users

Get the list of following users. One pull API call can pull up to 10,000 followers' OpenIDs. You can pull them multiple times. ways to meet needs. The interface definition of WeChat is as follows.

http request method: GET (please use https protocol)
http://www.miracleart.cn/

這個接口返回的數(shù)據(jù)是

{"total":2,"count":2,"data":{"openid":["","OPENID1","OPENID2"]},"next_openid":"NEXT_OPENID"}

根據(jù)返回的Json數(shù)據(jù)定義,我們還需要定義兩個實體類,用來存放返回的結果。

????///?<summary>
????///?獲取關注用戶列表的Json結果????///?</summary>
????public?class?UserListJsonResult?:?BaseJsonResult
????{????????///?<summary>
????????///?關注該公眾賬號的總用戶數(shù)????????///?</summary>
????????public?int?total?{?get;?set;?}????????///?<summary>
????????///?拉取的OPENID個數(shù),最大值為10000????????///?</summary>
????????public?int?count?{?get;?set;?}????????///?<summary>
????????///?列表數(shù)據(jù),OPENID的列表????????///?</summary>
????????public?OpenIdListData?data?{?get;?set;?}????????///?<summary>
????????///?拉取列表的后一個用戶的OPENID????????///?</summary>
????????public?string?next_openid?{?get;?set;?}
????}????///?<summary>
????///?列表數(shù)據(jù),OPENID的列表????///?</summary>
????public?class?OpenIdListData
????{????????///?<summary>
????????///?OPENID的列表????????///?</summary>
????????public?List<string>?openid?{?get;?set;?}
????}

為了獲取相關的用戶信息,我定義了一個接口,用來獲取用戶的信息,接口定義如下所示。

????///?<summary>
????///?微信用戶管理的API接口????///?</summary>
????public?interface?IUserApi
????{????????///?<summary>
????????///?獲取關注用戶列表????????///?</summary>
????????///?<param name="accessToken">調用接口憑證</param>
????????///?<param name="nextOpenId">第一個拉取的OPENID,不填默認從頭開始拉取</param>
????????///?<returns></returns>
????????List<string>?GetUserList(string?accessToken,?string?nextOpenId?=?null);????????///?<summary>
????????///?獲取用戶基本信息????????///?</summary>
????????///?<param name="accessToken">調用接口憑證</param>
????????///?<param name="openId">普通用戶的標識,對當前公眾號唯一</param>
????????///?<param name="lang">返回國家地區(qū)語言版本,zh_CN?簡體,zh_TW?繁體,en?英語</param>
????????UserJson?GetUserDetail(string?accessToken,?string?openId,?Language?lang?=?Language.zh_CN);

然后在實現(xiàn)類里面,我們分別對上面兩個接口進行實現(xiàn),獲取用戶列表信息如下所示。

????????///?<summary>
????????///?獲取關注用戶列表????????///?</summary>
????????///?<param name="accessToken">調用接口憑證</param>
????????///?<param name="nextOpenId">第一個拉取的OPENID,不填默認從頭開始拉取</param>
????????///?<returns></returns>
????????public?List<string>?GetUserList(string?accessToken,?string?nextOpenId?=?null)
????????{
????????????List<string>?list?=?new?List<string>();????????????string?url?=?string.Format("http://www.miracleart.cn/{0}",?accessToken);????????????if?(!string.IsNullOrEmpty(nextOpenId))
????????????{
????????????????url?+=?"&next_openid="?+?nextOpenId;
????????????}

????????????UserListJsonResult?result?=?JsonHelper<UserListJsonResult>.ConvertJson(url);????????????if?(result?!=?null?&&?result.data?!=?null)
????????????{
????????????????list.AddRange(result.data.openid);
????????????}????????????return?list;
????????}

我們看到,轉換的邏輯已經放到了JsonHelper里面去了,這個輔助類里面分別對數(shù)值進行了獲取內容,驗證返回值,然后轉換正確實體類幾個部分的操作。

獲取內容,通過輔助類HttpHelper進行,這個在我的公用類庫里面,里面的邏輯主要就是通過HttpRequest進行數(shù)據(jù)的獲取操作,不在贅述。

HttpHelper?helper?=?new?HttpHelper();string?content?=?helper.GetHtml(url);

由于返回的內容,我們需要判斷它是否正確返回所需的結果,如果沒有,拋出自定義的相關異常,方便處理,具體如下所示。

????????///?<summary>
????????///?檢查返回的記錄,如果返回沒有錯誤,或者結果提示成功,則不拋出異常????????///?</summary>
????????///?<param name="content">返回的結果</param>
????????///?<returns></returns>
????????private?static?bool?VerifyErrorCode(string?content)
????????{????????????if?(content.Contains("errcode"))
????????????{
????????????????ErrorJsonResult?errorResult?=?JsonConvert.DeserializeObject<ErrorJsonResult>(content);????????????????//非成功操作才記錄異常,因為有些操作是返回正常的結果({"errcode":?0,?"errmsg":?"ok"})
????????????????if?(errorResult?!=?null?&&?errorResult.errcode?!=?ReturnCode.請求成功)
????????????????{????????????????????string?error?=?string.Format("微信請求發(fā)生錯誤!錯誤代碼:{0},說明:{1}",?(int)errorResult.errcode,?errorResult.errmsg);
????????????????????LogTextHelper.Error(errorResult);????????????????????throw?new?WeixinException(error);//拋出錯誤????????????????}
????????????}????????????return?true;
????????}

然后轉換為相應的格式,就是通過Json.NET的類庫進行轉換。

????????????T?result?=?JsonConvert.DeserializeObject<T>(content);????????????return?result;

這樣我們就可以在ConvertJson函數(shù)實體里面,完整的進行處理和轉換了,轉換完整的函數(shù)代碼如下所示。

????///?
????///?Json字符串操作輔助類????///?
????public?class?JsonHelper?where?T?:?class,?new()
????{????????///?<summary>
????????///?檢查返回的記錄,如果返回沒有錯誤,或者結果提示成功,則不拋出異常????????///?</summary>
????????///?<param name="content">返回的結果</param>
????????///?<returns></returns>
????????private?static?bool?VerifyErrorCode(string?content)
????????{????????????if?(content.Contains("errcode"))
????????????{
????????????????ErrorJsonResult?errorResult?=?JsonConvert.DeserializeObject<ErrorJsonResult>(content);????????????????//非成功操作才記錄異常,因為有些操作是返回正常的結果({"errcode":?0,?"errmsg":?"ok"})
????????????????if?(errorResult?!=?null?&&?errorResult.errcode?!=?ReturnCode.請求成功)
????????????????{????????????????????string?error?=?string.Format("微信請求發(fā)生錯誤!錯誤代碼:{0},說明:{1}",?(int)errorResult.errcode,?errorResult.errmsg);
????????????????????LogTextHelper.Error(errorResult);????????????????????throw?new?WeixinException(error);//拋出錯誤????????????????}
????????????}????????????return?true;
????????}????????///?
????????///?轉換Json字符串到具體的對象????????///?
????????///?返回Json數(shù)據(jù)的鏈接地址
????????///?
????????public?static?T?ConvertJson(string?url)
????????{
????????????HttpHelper?helper?=?new?HttpHelper();????????????string?content?=?helper.GetHtml(url);
????????????VerifyErrorCode(content);

????????????T?result?=?JsonConvert.DeserializeObject<T>(content);????????????return?result;
????????}
}

調用這個API的界面層代碼如下所示(測試代碼)

????????????IUserApi?userBLL?=?new?UserApi();
????????????List<string>?userList?=?userBLL.GetUserList(token)

?

4、獲取用戶詳細信息

上面的獲取列表操作,相對比較簡單,而且不用POST任何數(shù)據(jù),因此通過Get協(xié)議就能獲取到所需的數(shù)據(jù)。

本小節(jié)繼續(xù)介紹獲取用戶詳細信息的操作,這個操作也是通過GET協(xié)議就可以完成的。

這個API的調用定義如下所示:

http請求方式:?GET
https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN

通過傳入一個OpenId,我們就能很好獲取到用戶的相關信息了。

前面小節(jié)我們已經定義了它的接口,說明了傳入及返回值,根據(jù)定義,它的實現(xiàn)函數(shù)如下所示。

????????///?<summary>
????????///?獲取用戶基本信息????????///?</summary>
????????///?<param name="accessToken">調用接口憑證</param>
????????///?<param name="openId">普通用戶的標識,對當前公眾號唯一</param>
????????///?<param name="lang">返回國家地區(qū)語言版本,zh_CN?簡體,zh_TW?繁體,en?英語</param>
????????public?UserJson?GetUserDetail(string?accessToken,?string?openId,?Language?lang?=?Language.zh_CN)
????????{????????????string?url?=?string.Format("http://www.miracleart.cn/{0}&openid={1}&lang={2}",
???????????????????accessToken,?openId,?lang.ToString());

????????????UserJson?result?=?JsonHelper<UserJson>.ConvertJson(url);????????????return?result;
????????}

最后,我們結合獲取用戶列表和獲取用戶詳細信息的兩個API,我們看看調用的代碼(測試代碼)。

????????private?void?btnGetUsers_Click(object?sender,?EventArgs?e)
????????{
????????????IUserApi?userBLL?=?new?UserApi();
????????????List<string>?userList?=?userBLL.GetUserList(token);????????????foreach?(string?openId?in?userList)
????????????{
????????????????UserJson?userInfo?=?userBLL.GetUserDetail(token,?openId);????????????????if?(userInfo?!=?null)
????????????????{????????????????????
????????????????????string?tips?=?string.Format("{0}:{1}",?userInfo.nickname,?userInfo.openid);
????????????????????Console.WriteLine(tips);
????????????????}
????????????}
????????}

?

更多C#開發(fā)微信門戶及應用(4)--關注用戶列表及詳細信息管理?相關文章請關注PHP中文網!

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)