Home >WeChat Applet >Mini Program Development >How to obtain user UnionID, nickname, and avatar information in mini program development
The content of this article is about the method of obtaining user UnionID, nickname, and avatar information in the development of small programs. It has certain reference value and friends in need can refer to it. I hope it helps you.
I have been developing a small program recently and recorded some of it.
In the past, you could use wx.getUserInfo to obtain user information, but then the official made adjustments, so you have to change your mind.
Obtaining user nicknames and avatars
This step is very convenient. It can be achieved using the built-in components of the mini program. You can obtain the following data
As you can see, there is still a lot of relevant information that can be obtained. The following is an example of avatar and nickname
<!-- 头像 --> <open-data></open-data> <!-- 昵称 --> <open-data></open-data>
Get the user’s UnionID
Get the user’s nickname , The avatar is very simple, but in actual development, we often need the user's UnionID, which can be achieved using wx.login and wx.request. Let’s take a look at the official process first
You can see that the process is not complicated. The following is the JS example of the mini program
onLoad: function (options) { var that = this; wx.login({ success: function (res) { if (res.code) { // 发起网络请求,获取用户UnionID wx.request({ url: 'https://xxxx', data: { code: res.code }, success: function (res) { if (res.data.message == 'success') { // 获取数据成功 console.log(res.data.data) } } }) } else { // 获取code失败 console.log('登录失败!' + res.errMsg) } } }); }
The following is the back-end PHP code
<?php $code = $_GET['code']; $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $this->appid . '&secret=' . $this->secret . '&js_code=' . $code . '&grant_type=authorization_code'; $userInfo = file_get_contents($url); $userInfo = json_decode($userInfo, true); if (!$userInfo['unionid']) { echo json_encode(array('data'=>'','message'=>'error')); } else { echo json_encode(array('data'=> $userInfo['unionid'],'message'=>'success')); }
After obtaining the UnionID, you can continue the business process.
The above is the detailed content of How to obtain user UnionID, nickname, and avatar information in mini program development. For more information, please follow other related articles on the PHP Chinese website!