星空网 > 软件开发 > 操作系统

微信小程序授权获取用户详细信息openid

小程序获取用户的头像昵称openid之类  

微信小程序授权获取用户详细信息openidimages/loading.gif' data-original="http://files.jb51.net/file_images/article/201709/2017920100902055.gif?201782010924" >

第一种使用wx.getUserInfo直接获取微信头像,昵称

?
12345678wx.getUserInfo({   success:function(res) {   that.setData({     nickName: res.userInfo.nickName,     avatarUrl: res.userInfo.avatarUrl,   })   },})

第二种 

我们在使用小程序wx.login API进行登录的时候,直接使用wx.getUserInfo是不能获取更多的信息的,如微信用户的openid。 
官方提示,需要发送获取到的code进行请求到微信的后端API,进行用户解密之类的操作才可以获取,

根据文档,只需要进行一个get请求到如下地址即可:

 

123https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code appid和secret在微信小程序后台可以看到,js_code为使用wx.login登录时获取到的code参数数据,grant_type这个不用改动。

js文件

 
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113varopenId = (wx.getStorageSync('openId'))    if(openId) {     wx.getUserInfo({      success:function(res) {       that.setData({        nickName: res.userInfo.nickName,        avatarUrl: res.userInfo.avatarUrl,       })      },      fail:function() {       // fail       console.log("获取失败!")      },      complete:function() {       // complete       console.log("获取用户信息完成!")      }     })    }else{     wx.login({      success:function(res) {       console.log(res.code)       if(res.code) {        wx.getUserInfo({         withCredentials:true,         success:function(res_user) {          wx.request({           //后台接口地址           url:'https://....com/wx/login',           data: {            code: res.code,            encryptedData: res_user.encryptedData,            iv: res_user.iv           },           method:'GET',           header: {            'content-type':'application/json'           },           success:function(res) {            // this.globalData.userInfo = JSON.parse(res.data);            that.setData({             nickName: res.data.nickName,             avatarUrl: res.data.avatarUrl,            })            wx.setStorageSync('openId', res.data.openId);            }          })         }, fail: function() {          wx.showModal({           title:'警告通知',           content:'您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',           success:function(res) {            if(res.confirm) {             wx.openSetting({              success: (res) => {               if(res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录                wx.login({                 success:function(res_login) {                  if(res_login.code) {                   wx.getUserInfo({                    withCredentials:true,                    success:function(res_user) {                     wx.request({                      url:'https://....com/wx/login',                      data: {                       code: res_login.code,                       encryptedData: res_user.encryptedData,                       iv: res_user.iv                      },                      method:'GET',                      header: {                       'content-type':'application/json'                      },                      success:function(res) {                       that.setData({                        nickName: res.data.nickName,                        avatarUrl: res.data.avatarUrl,                        })                       wx.setStorageSync('openId', res.data.openId);                      }                     })                    }                   })                  }                 }                });               }              }, fail: function(res) {               }             })             }           }          })         }, complete: function(res) {           }        })       }      }     })     }   }, globalData: {    userInfo:null }

后台是php 框架是laravel5.4版本

源码免费下载地址 border="0" cellspacing="0" cellpadding="0">12345678910111213141516171819202122232425262728293031323334353637383940414243444546<?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Request;use App\Http\Controllers\Controller;use App\Models\User;use App\Models\Wechatuser;include_once  app_path('/Http/Controllers/Admin/PHP/wxBizDataCrypt.php');   // 获取微信用户信息  publicfunctiongetWxLogin(Request $request)  {   // require_once ROOTPATH . "./PHP/wxBizDataCrypt.php";     $code  =  $request->get('code');    $encryptedData  =  $request->get('encryptedData');    $iv  =  $request->get('iv');    $appid = "***";    $secret =  "***";     $URL = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";     $apiData=file_get_contents($URL);    // var_dump($code,'wwwwwwww',$apiData['errscode']);    //   $ch = curl_init();    //   curl_setopt($ch, CURLOPT_URL, $URL);    //   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    //   curl_setopt($ch, CURLOPT_HEADER, 0);    //   $output = curl_exec($ch);    //   curl_close($ch)     if(!isset($apiData['errcode'])){      $sessionKey = json_decode($apiData)->session_key;      $userifo = new\WXBizDataCrypt($appid, $sessionKey);       $errCode = $userifo->decryptData($encryptedData, $iv, $data );       if($errCode == 0) {        return($data . "\n");      }else{        returnfalse;      }    }  }

官方文档的登录流程图,整个登录流程基本如下图所示:

微信小程序授权获取用户详细信息openid 

 

原标题:微信小程序授权获取用户详细信息openid

关键词:小程序

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

dsv国际货运:https://www.goluckyvip.com/tag/101632.html
zmc国际货运:https://www.goluckyvip.com/tag/101633.html
国际货运运输公司:https://www.goluckyvip.com/tag/101635.html
深圳市深国际西部物流:https://www.goluckyvip.com/tag/101636.html
刀具国际物流:https://www.goluckyvip.com/tag/101637.html
澳洲当地国际物流:https://www.goluckyvip.com/tag/101638.html
武陵山大裂谷周围景点 武陵山大裂谷周围景点图片:https://www.vstour.cn/a/411233.html
南美旅游报价(探索南美洲的旅行费用):https://www.vstour.cn/a/411234.html
我的浏览记录
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流