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

How to obtain user Information for Mini Program developers

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

Share

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

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.

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