Detailed graphic and text explanation of examples of developing custom menu jump pages and obtaining user information for WeChat public accounts

高洛峰
Release: 2017-03-16 15:09:50
Original
4945 people have browsed it

This article mainly introduces the development of WeChat public accountsCustom menu Jump to the page and obtain relevant information about user information examples. Friends in need can refer to the following

WeChat public account development custom menu

Please read this article before proceeding with configuration development

Please go to the WeChat platform developer documentation to read "Web page authorization to obtain basic user information" "InterfaceExplanation

In the development of WeChat public accounts, a menu is often defined, and then the user clicks the menu to enter the user's personal center function, which is usually applied to each public account membership service.

How to navigate a user to the personal center page in the WeChat custom menu?

The first choice is to obtain the user's openid through the user's click. To obtain the user's openid through the user's click jump, you must dynamically bind the user's openid in the menu, or fill in WeChat in the jump URL of the menu. The link provided is officially given two link types

One is the link with Scope as snsapi_base

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx520c15f417810387&redirect_uri=https%3A%2F%2Fchong.qq.com%2Fphp%2Findex.php%3Fd%3D%26c%3DwxAdapter%26m%3DmobileDeal%26showwxpaytitle%3D1%26vb2ctag%3D4_2030_5_1194_60&response_type=code&scope=snsapi_base&state=123#wechat_redirect
Copy after login

The other is the link whose Scope is snsapi_userinfo

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http%3A%2F%2Fnba.bluewebgame.com%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
Copy after login


The difference between these two links is as follows

Application authorization scope, snsapi_base (does not pop up the authorization page, jumps directly, and can only obtain the user's openid), snsapi_userinfo (pops up the authorization page, and can get the nickname, gender, and location through openid. And, even if it is not in the future In the case of attention, as long as the user authorizes it, its information can also be obtained)

Many opinions on the Internet are to directly use the URL of the link as the URL in the view type in the WeChat custom menu (when filling in the URL It is necessary to configure the web page authorization callback domain name and appid). I tried this method but failed.

{ "type":"view", "name":"会员中心", "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=你的appid&redirect_uri=你配置接收微信认证的地址?response_type=code&scope=snsapi_base&state=1#wechat_redirect" },
Copy after login

The return result is that the menu creation failed

Create menu failed errcode:{40033} errmsg:{invalid char<a href="http://www.php.cn/code/8209.html" target="_blank">set</a>. please check your request, <a href="http://www.php.cn/wiki/109.html" target="_blank">if</a> <a href="http://www.php.cn/wiki/137.html" target="_blank">include</a> \uxxxx will create fail! hint: [91..gA0792vr23]}

I tried urlEncoding the following address, but still got the same error.

Later I thought of a way

Fill in your own URL in the custom menu, redirect the user to the URL of snsapi_base in the filled-in URL, and then configure the acquisition in snsapi_base The user's openid and other user information finally jump to a page, which is the usual member center page.

The process is as follows

Detailed graphic and text explanation of examples of developing custom menu jump pages and obtaining user information for WeChat public accounts

Please see the code

{ "type":"view",

"name":"Member Center",

"url":"http://configured URL/redirect"}

Use the url to jump the user to

##http://configured URL/redirect

and then call it once in the processing method Just redirect

//类上的配置
@Controller
@RequestMapping("/wechat")
public class WeChatController{
  @RequestMapping(value = "/redirect", method = RequestMethod.GET)
  public String weixinRedirect(HttpServletRequest request, HttpServletResponse response) {
    return "redirect:https://open.weixin.qq.com/connect/oauth2/authorize?appid=你的appid&redirect_uri=你的服务器处理地址?response_type=code&scope=snsapi_base&state=1&connect_redirect=1#wechat_redirect";
    }
}
Copy after login

The server will jump to the WeChat authentication to your server processing address, which is the above

redirect_uri =The address in your server processing address

The configuration here is

Your server address/oauth

The code is as follows

@RequestMapping(value = "/oauth", method = RequestMethod.GET)
  public String weixinOAuth(HttpServletRequest request, HttpServletResponse response, Model model) {
    //得到code
    String CODE = request.getParameter("code");
    String APPID = "你的APPID";
    String SECRET = "你的SECRET";
    //换取access_token 其中包含了openid
    String URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code".replace("APPID", APPID).replace("SECRET", SECRET).replace("CODE", CODE);
    //URLConnectionHelper是一个模拟发送http请求的类
    String jsonStr = URLConnectionHelper.sendGet(URL);
    //System.out.println(jsonStr);
    //out.print(jsonStr);
    JSONObject jsonObj = new JSONObject(jsonStr);
    String openid = jsonObj.get("openid").toString();
    //有了用户的opendi就可以的到用户的信息了
    //地址为https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
    //得到用户信息之后返回到一个页面
    model.addAttribute("user", wechatUser);
    return "vip/userInfo";
  }
Copy after login

The effect is as follows

Detailed graphic and text explanation of examples of developing custom menu jump pages and obtaining user information for WeChat public accounts

Detailed graphic and text explanation of examples of developing custom menu jump pages and obtaining user information for WeChat public accounts

And in this way, when the user uses other When the browser is opened, an error will occur, ensuring that it can only be used in WeChat, ensuring

security. And the address bar will not expose other users’ personal information.

Detailed graphic and text explanation of examples of developing custom menu jump pages and obtaining user information for WeChat public accounts





The above is the detailed content of Detailed graphic and text explanation of examples of developing custom menu jump pages and obtaining user information for WeChat public accounts. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template