Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

C # Development of Wechat how to use redirection to obtain user data in Wechat menu

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the C# development of Wechat how to use redirection in the Wechat menu to obtain user data, the article is very detailed, has a certain reference value, interested friends must finish!

We know that Wechat's custom menus fall into two categories, corresponding to the Click type and the View type, while redirection belongs to one of the View types, as shown below.

1. Configuration of Wechat redirect menu

The menu of Wechat redirection allows the Wechat server to redirect by passing in an address parameter. Its main rules are as follows.

The link for scope=snsapi_base mode is as follows:

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

The links for scope=snsapi_userinfo are as follows:

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

These two menu links are mainly implemented by UrlEncode the link address we have given, and then assigning it to the parameter redirect_uri.

As the link address is relatively long, if you need to configure the menu every time, copy over to modify, very inconvenient, we can in the configuration interface of the custom menu, add a button function to deal with the content, in order to achieve the address translation we need, my portal application platform on the custom menu operation is based on this idea.

By default, we only need to fill in a url address that needs to be redirected, as shown below.

If you need to configure the menu link address to be redirected, call the "convert Redirect menu" button and use the script function to convert it. The result of the conversion is as follows.

The original is to use the background javascript to achieve parameter URL transcoding, but also need to obtain the background AppId, so that a complete address connection can be constructed.

2. The implementation code of script conversion operation

As mentioned earlier, the first is to implement URL transcoding, the second is to get the background AppId, and then generate a complete URL. In order to avoid repetitive research, I will post this part of the code and study it together.

Before using it, we need to pay attention to another problem, that is, after redirecting to the specified page, the page will have a parameter of code, which is very important, and we need to get it, of course, through javascript to obtain the corresponding code parameter.

This logic can be implemented with a script function, as follows

Function getUrlVars () {var vars = [], hash; var hashes = _ window.location.href.slice_ (window.location.href.indexOf ('?) + 1). Split ('&'); for (var I = 0; I < hashes.length; iTunes +) {hash = hashes.split ('='); vars.push (hash [0]) Vars [hash [0]] = hash [1];} return vars;}

After defining this function, we can get the code parameter in the redirected page as follows.

Var code = getUrlVars () ["code"]

Putting these aside, let's first discuss how to convert the link address to the desired link address operation.

In order to convert link addresses to each other (for convenience), we can determine whether the link address contains the domain name of qq.

If (url.indexOf ("https://open.weixin.qq.com/connect/oauth3/authorize?") = = 0) {var redirect_uri = getUrlVars (url) [" redirect_uri "]; if (redirect_uri! =") {var newUrl = decodeURIComponent (redirect_uri); $(" # "+ ctrlName) .val (newUrl);}}

If it is a normal link we entered, then it should be converted to a redirected link address, as shown below.

Else {var newUrl = encodeURIComponent (url); var reNewUrl = "https://open.weixin.qq.com/connect/oauth3/authorize?appid=@ViewBag.appid&redirect_uri=" + newUrl +" & response_type=code&scope=snsapi_base&state=123#wechat_redirect "; $(" # "+ ctrlName) .val (reNewUrl);}

The redirect link needs to have an appId of the current Wechat development user, which is not fixed, but different developers are different. Here, the dynamic object of MVC is used for binding: @ ViewBag.appid.

In the corresponding MenuController controller, just assign a value to it.

/ / default view control method / public override ActionResult Index () {ViewBag.appid = GetAppId (); return View ();}

The address list of the redirect menu after configuration is shown below. We open the corresponding record details page, and you can convert the address of the redirection menu at any time through the function button in the page, so as to understand the detailed link content.

3. Design and processing of the redirected page.

After configuring the link address above, we need to add such a page to the website to deal with users' information. In general, we may make it convenient for users to view their basic information on Wechat, and also to bind users for the purpose of their personal data. For example, users can bind to mobile phones, Email mailboxes and other operations, as well as user names related to the business system. In this way, users can quickly register as members or associate with the backstage system.

The two user information display interfaces I designed are shown below.

These two interfaces mainly use the relevant content of Jquery Mobile, deal with the interface, the whole module combines the way of SMS verification code to verify the user's mobile phone, so that the accurate binding operation of information can be realized more efficiently. Of course, it can also be combined with the external system to bind the user's account password. In this way, users can enter the Wechat platform for shopping, data maintenance, business management and other operations. In fact, once the ID of the external system is bound, it provides an entrance to the external system quickly.

The above is all the contents of the article "how to use redirection to obtain user data in the Wechat menu developed by C#". Thank you for reading! Hope to share the content to help you, more related 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report