C # development of WeChat portals and applications (11)-Introduction to multiple forms of WeChat menus

Source: Internet
Author: User
Tags allkeys openid

In the previous series of articles, we can see the importance of custom menus. In the public account, menus are the first impression of users. We need to plan the content and layout of these menus. According to the menu definition, we can see that menus are generally divided into two types: Common Url menus (View menus ), one is the event menu (the menu type is Click). Generally, the Url menu cannot obtain any information about the user, but the user information is very important, therefore, another method (similar to redirection) is provided for us to use. This article mainly introduces the use of this method menu, so that we can interact with users as much as possible.

1. custom menu category

Requirements for custom menus:Currently, a custom menu can contain up to three level-1 menus, and each level-1 menu can contain up to five level-2 menus. A level-1 menu can contain up to four Chinese characters, and a level-2 menu can contain up to seven Chinese characters. The extra content will be replaced.

Based on the menu category, we can display it by image classification:

I learned about various public accounts and found that most accounts use the common View-type menu links through which they are linked to their own micro-network sites, but some of them are doing well, for example, the provincial Zhongshan Library provides an entry for binding library users and OpenID through redirection. After binding, users can view borrowed books, then, you can use the one-click renewal function to implement the quick renewal function of books.

The Url menu events of this redirection type are described as follows:

If a user accesses a third-party webpage with a public account in the middle (excluding the Web), the public account developer can use this interface to obtain the basic information of the current user (including nickname, gender, city, and country ). Using user information, you can achieve user experience optimization, user source statistics, account binding, user identity authentication, and other functions.Note: "The interface for obtaining basic user information is used to obtain basic user information based on the user's OpenID when a message is generated between the user and the public account, and the webpage authorization method is used to obtain basic user information, message interaction is not required, but the user can access the webpage of the public account. The page requesting user authorization is displayed. After user authorization, you can obtain the basic information (this process does not even require users to pay attention to the public account .)"

2. URL of the Redirect type menu

As mentioned above, there are two types of redirection menus. In fact, they are only different in the Scope type parameter, and the rest are the same.

For the purpose of display, we assume that when you click the menu, you can switch to the http://www.iqidi.com/testwx.ashxpage, and bring the openidwait information of the current user.

The link for scope = snsapi_base is as follows:

Https://open.weixin.qq.com/connect/oauth2/authorize? Appid = wx3d81fc2886d86526 & redirect_uri = http % 3A % 2F % 2Fwww.iqidi.com % 2Ftestwx. ashx & response_type = code & scope = snsapi_base & state = 123 # wechat_redirect

The link to scope = snsapi_userinfo is as follows:

Https://open.weixin.qq.com/connect/oauth2/authorize? Appid = wx3d81fc2886d86526 & redirect_uri = http % 3A % 2F % 2Fwww.iqidi.com % 2Ftestwx. ashx & response_type = code & scope = snsapi_userinfo & state = 123 # wechat_redirect

However, their experience for the mobile client is different. The first option can be smoothly switched, but the second option will pop up a dialog box for the user to confirm before continuing.

To demonstrate the differences between the above two types of data acquisition, I will parse the value of the code they passed in and the user information in exchange for OpenID. The results of both are the same. The specific test interface is as follows.

The background code of TestWX. ashx is as follows:

/// <Summary> /// summary of TestWX /// </summary> public class TestWX: IHttpHandler {string appId = ""; // replace it with your information string appSecret = ""; // replace it with your information public void ProcessRequest (HttpContext context) {context. response. contentType = "text/plain"; string content = ""; if (context. request! = Null & context. Request. Url! = Null) {NameValueCollection list = HttpUtility. parseQueryString (context. request. url. query); foreach (string key in list. allKeys) {content + = string. format ("{0 }:{ 1} \ r \ n", key, list [key]) ;}} string code = context. request. queryString ["code"]? ""; If (! String. isNullOrEmpty (code) {IBasicApi api = new BasicApi (); try {AppConfig config = new AppConfig (); appId = config. appConfigGet ("AppId"); // obtain the program ID from the configuration appSecret = config. appConfigGet ("AppSecret"); // obtain the AccessTokenResult from the configuration = api. getAccessToken (appId, appSecret, code); if (result! = Null) {content + = string. format ("openid: {0} \ r \ n", result. openid); string token = api. getAccessToken (appId, appSecret); IUserApi userApi = new UserApi (); UserJson userDetail = userApi. getUserDetail (token, result. openid); if (userDetail! = Null) {content + = string. format ("nickname: {0} sex: {1} \ r \ n", userDetail. nickname, userDetail. sex); content + = string. format ("Location: {0} {1} {2} {3} \ r \ n", userDetail. country, userDetail. province, userDetail. city, userDetail. language); content + = string. format ("HeadUrl: {0} \ r \ n", userDetail. headimgurl); content + = string. format ("subscribe: {0}, {1} \ r \ n", (userDetail. subscribe = 1 )? "Subscribed": "unsubscribed", userDetail. subscribe_time.GetDateTime () ;}} catch {}} context. Response. Write (content );}

In the above code, I mainly divided into several steps. One is to print the parameter information of the link redirected by the current user. The Code is as follows.

                NameValueCollection list = HttpUtility.ParseQueryString(context.Request.Url.Query);                foreach (string key in list.AllKeys)                {                    content += string.Format("{0}:{1} \r\n", key, list[key]);                }

After obtaining the Code parameter, you can use the API to obtain the data of AccessTokenResult, which includes the user's OpenID.

AccessTokenResult result = api.GetAccessToken(appId, appSecret, code);

After a normal call, we further parse the OpenID identified by the user and call the API to obtain the user's detailed information. The specific code is as follows.

UserJson userDetail = userApi.GetUserDetail(token, result.openid);

When we obtain user-related information, we can present various user information, as shown in the following code.

If (userDetail! = Null) {content + = string. format ("nickname: {0} sex: {1} \ r \ n", userDetail. nickname, userDetail. sex); content + = string. format ("Location: {0} {1} {2} {3} \ r \ n", userDetail. country, userDetail. province, userDetail. city, userDetail. language); content + = string. format ("HeadUrl: {0} \ r \ n", userDetail. headimgurl); content + = string. format ("subscribe: {0}, {1} \ r \ n", (userDetail. subscribe = 1 )? "Subscribed": "unsubscribed", userDetail. subscribe_time.GetDateTime ());}
3. Use of the redirection link menu

In this menu, you need to specify a Domain Name and set it in the background. The redirected link must belong to this domain name. Otherwise, it will not be transferred to the desired link.

In this way, the background of our application can obtain the user ID and detailed user information, so that we can bind user-related business information, as mentioned above, the library borrowing information, water delivery customer information, customer point information, or can be associated with the background account for more complex applications. The user's identity information is so important. If we integrate it with our CRM system and business management system, we can play the role of user information application.

The above is my understanding of the application of this type of menu link. We also need to further deepen its application and hope to discuss the application scenarios in this area with you.

 

If you are interested in this series of C # development portals and applications, you can follow my other articles as follows:

C # development portal and application (10) -- synchronize user group information in the management system

C # development portal and application (9)-portal menu management and submission to server

C # development portal and application (8)-portal application management system function Introduction

C # development portal and application (7)-Multi-customer service functions and development integration

C # development portal and application (6)-portal menu management operations

C # development portal and application (5) -- User Group Information Management

C # development portal and application (4) -- Focus on the user list and detailed information management

C # development portal and application (3) -- Response to text messages and text messages

C # development portal and application (2) -- Message Processing and response

C # development portal and application (1) -- getting started with Interfaces

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.