Get the App
SLTechnology News&Howtos  ›  Development  › 

How to obtain user Information for Mini Program developers

Shulou Source: shulou.com Published: 2022-06-01 06:03:08 09月20日 Update

Most people do not understand the knowledge points of this article "how to obtain user information in Mini Program development", so the editor summarizes the following contents for you. The content is detailed, the steps are clear, and it has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Mini Program development how to obtain user information" article.

There are two ways to get user information.

1. Json objects that do not contain sensitive information openId (including: nickname, avatarUrl and other basic information)

2. Basic information containing sensitive information openId.

The first acquisition scheme

1. First call the wx.login () API to allow users to authorize authentication, that is, we observe with our naked eyes whether you authorize this kind of information to xxxxx.

2. After the user is successfully authorized, call the wx.getUserInfo () API to obtain the user information.

The complete code is as follows:

Wx.login ({success:function () {wx.getUserInfo ({success:function (res) {var simpleUser = res.userInfo; console.log (simpleUser.nickName);}});}}); the second is more complex and requires interaction with the background to obtain userInfo, but the data obtained by this scheme is complete (including openId).

1. Call the wx.login () API to authorize the inclusion of code in the parameters of the success success function.

2. Calling the success function of wx.getUserInfo () interface contains encryptedData and iv.

3. Pass the above parameters to the background for analysis, and generate userInfo

The code is as follows

Js

Var request = require (".. /.. / utils/request.js"); wx.login ({success:function (res_login) {if (res_login.code) {wx.getUserInfo ({withCredentials:true, success:function (res_user) {var requestUrl = "/ getUserApi/xxx.php") Var jsonData = {code:res_login.code, encryptedData:res_user.encryptedData, iv:res_user.iv} Request.httpsPostRequest (requestUrl,jsonData,function (res) {console.log (res.openId);});}}) background parsing

/ * * get fan information * the parameter is the * / public function wxUserInfo ($code,$encryptedData,$iv) {$apiUrl = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->wxConfig['appid']}&secret={$this->wxConfig['appsecret']}&js_code={$code}&grant_type=authorization_code"; $apiData = json_decode (curlHttp ($apiUrl,true), true) passed from the front end. If (! isset ($apiData ['session_key'])) {echoJson (array ("code" = > 102, "msg" = > "curl error"), true);} $userInfo = getUserInfo ($this- > wxConfig [' appid'], $apiData ['session_key'], $encryptedData,$iv) If (! $userInfo) {echoJson (array ("code" = > 105, "msg" = > "userInfo not"));} / / $userInfo = json_decode ($userInfo,true) / / loading user services the above is the content of this article on "how to obtain user information in Mini Program development". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Information users content interfaces routines development parameters background solutions success code functions that is articles knowledge articles subcontracting complexity that is value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Information Linux Microsoft vpn