Home > WeChat Applet > Mini Program Development > How to get the user's openid in the WeChat applet

How to get the user's openid in the WeChat applet

小云云
Release: 2018-05-15 11:19:00
Original
10516 people have browsed it

This article mainly shares with you how to obtain the user's openid in the WeChat applet. There are two ways to obtain the user's openid in the WeChat applet. I hope it can help everyone.

Method 1:
First obtain the user information (wx.getUserInfo), and then decrypt the sensitive information containing openid in the returned user information to obtain the user's openid. One is to obtain the user information first, and then return The sensitive information containing openid in the user information is decrypted to obtain the user's openid. This method is more troublesome to obtain the user's openid. It also involves data decryption, and if the user refuses authorization, it cannot be obtained. This method will not be described in detail here, please refer to the documentation for details.
Method 2:
First log in (wx.login) to obtain the user login credentials (code), then use this code as a parameter to call the interface, and obtain the user's openid through the backend.
The applet code is as follows:

//app.js
App({
  onLaunch: function() {
    wx.login({
      success: function(res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://test.com/onLogin',
            data: {
              code: res.code
            }
          })
        } else {
          console.log('获取用户登录态失败!' + res.errMsg)
        }
      }
    });
  }
})
Copy after login

The backend data acquisition method is as follows:

//获取用户openid
function getopenid(){
	$js_code = I('post.code');
	if(empty($js_code)) return array('status'=>0,'info'=>'缺少js_code');
	
	$appid = 'xxxxxxxxxxxxx';
	$appsecret = 'xxxxxxxxxxxxxxxxxxxxxx';
	$curl = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';
	$curl = sprintf($curl,$appid,$appsecret,$js_code);
	$result = request($curl);
	return array('status'=>1,'info'=>json_decode($result,true));
}
Copy after login

Data return instructions

//正常返回的JSON数据包
{
      "openid": "OPENID",
      "session_key": "SESSIONKEY",
      "unionid": "UNIONID"
}
//错误时返回JSON数据包(示例为Code无效)
{
    "errcode": 40029,
    "errmsg": "invalid code"
}
Copy after login

Related recommendations:

How to obtain openid in WeChat

How to obtain openid and user information in WeChat applet

WeChat Get the current user Openid and precautions in a timely manner from the official account development webpage

The above is the detailed content of How to get the user's openid in the WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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