微信小程序授权获取用户详细信息openid的实例详解

这篇文章主要介绍了微信小程序授权获取用户详细信息openid的实例详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下

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

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

[js] view plain copy

 

  1. wx.getUserInfo({  
  2.    success: function (res) {  
  3.    that.setData({  
  4.      nickName: res.userInfo.nickName,  
  5.      avatarUrl: res.userInfo.avatarUrl,  
  6.    })  
  7.    },  
  8. })  

第二种 

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

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

[js] view plain copy

 

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

js文件

[js] view plain copy

 

  1. var openId = (wx.getStorageSync('openId'))  
  2.     if (openId) {  
  3.      wx.getUserInfo({  
  4.       success: function (res) {  
  5.        that.setData({  
  6.         nickName: res.userInfo.nickName,  
  7.         avatarUrl: res.userInfo.avatarUrl,  
  8.        })  
  9.       },  
  10.       fail: function () {  
  11.        // fail  
  12.        console.log("获取失败!")  
  13.       },  
  14.       complete: function () {  
  15.        // complete  
  16.        console.log("获取用户信息完成!")  
  17.       }  
  18.      })  
  19.     } else {  
  20.      wx.login({  
  21.       success: function (res) {  
  22.        console.log(res.code)  
  23.        if (res.code) {  
  24.         wx.getUserInfo({  
  25.          withCredentials: true,  
  26.          success: function (res_user) {  
  27.           wx.request({  
  28.            //后台接口地址  
  29.            url: 'https://....com/wx/login',  
  30.            data: {  
  31.             code: res.code,  
  32.             encryptedData: res_user.encryptedData,  
  33.             iv: res_user.iv  
  34.            },  
  35.            method: 'GET',  
  36.            header: {  
  37.             'content-type': 'application/json'  
  38.            },  
  39.            success: function (res) {  
  40.             // this.globalData.userInfo = JSON.parse(res.data);  
  41.             that.setData({  
  42.              nickName: res.data.nickName,  
  43.              avatarUrl: res.data.avatarUrl,  
  44.             })  
  45.             wx.setStorageSync('openId', res.data.openId);  
  46.   
  47.            }  
  48.           })  
  49.          }, fail: function () {  
  50.           wx.showModal({  
  51.            title: '警告通知',  
  52.            content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',  
  53.            success: function (res) {  
  54.             if (res.confirm) {  
  55.              wx.openSetting({  
  56.               success: (res) => {  
  57.                if (res.authSetting["scope.userInfo"]) {如果用户重新同意了授权登录  
  58.                 wx.login({  
  59.                  success: function (res_login) {  
  60.                   if (res_login.code) {  
  61.                    wx.getUserInfo({  
  62.                     withCredentials: true,  
  63.                     success: function (res_user) {  
  64.                      wx.request({  
  65.                       url: 'https://....com/wx/login',  
  66.                       data: {  
  67.                        code: res_login.code,  
  68.                        encryptedData: res_user.encryptedData,  
  69.                        iv: res_user.iv  
  70.                       },  
  71.                       method: 'GET',  
  72.                       header: {  
  73.                        'content-type': 'application/json'  
  74.                       },  
  75.                       success: function (res) {  
  76.                        that.setData({  
  77.                         nickName: res.data.nickName,  
  78.                         avatarUrl: res.data.avatarUrl,  
  79.   
  80.                        })  
  81.                        wx.setStorageSync('openId', res.data.openId);  
  82.                       }  
  83.                      })  
  84.                     }  
  85.                    })  
  86.                   }  
  87.                  }  
  88.                 });  
  89.                }  
  90.               }, fail: function (res) {  
  91.   
  92.               }  
  93.              })  
  94.   
  95.             }  
  96.            }  
  97.           })  
  98.          }, complete: function (res) {  
  99.   
  100.   
  101.          }  
  102.         })  
  103.        }  
  104.       }  
  105.      })  
  106.   
  107.     }  
  108.   
  109.   
  110.  },  
  111.  globalData: {    
  112.   userInfo: null  
  113.  }  

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

官方文档:

https://mp.weixin.qq.com/debug/wxadoc/dev/api/signature.html

微信官方提供了多种编程语言的示例代码(点击下载)。每种语言类型的接口名字均一致。调用方式可以参照示例。

下载之后在php文件中引入:

[js] view plain copy

 

  1. <?php  
  2.   
  3. namespace App\Http\Controllers\Admin;  
  4.   
  5. use Illuminate\Http\Request;  
  6. use App\Http\Controllers\Controller;  
  7. use App\Models\User;  
  8. use App\Models\Wechatuser;  
  9. include_once  app_path('/Http/Controllers/Admin/PHP/wxBizDataCrypt.php');  
  10.   
  11.   
  12.  // 获取微信用户信息  
  13.   public function getWxLogin(Request $request)  
  14.   {  
  15.    // require_once ROOTPATH . "./PHP/wxBizDataCrypt.php";  
  16.   
  17.     $code  =  $request->get('code');  
  18.     $encryptedData  =  $request->get('encryptedData');  
  19.     $iv  =  $request->get('iv');  
  20.     $appid = "***" ;  
  21.     $secret =  "***";  
  22.   
  23.     $URL = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";  
  24.   
  25.     $apiData=file_get_contents($URL);  
  26.     // var_dump($code,'wwwwwwww',$apiData['errscode']);  
  27.     //   $ch = curl_init();  
  28.     //   curl_setopt($ch, CURLOPT_URL, $URL);  
  29.     //   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  30.     //   curl_setopt($ch, CURLOPT_HEADER, 0);  
  31.     //   $output = curl_exec($ch);  
  32.     //   curl_close($ch)  
  33.   
  34.     if(!isset($apiData['errcode'])){  
  35.       $sessionKey = json_decode($apiData)->session_key;  
  36.       $userifo = new \WXBizDataCrypt($appid, $sessionKey);  
  37.   
  38.       $errCode = $userifo->decryptData($encryptedData, $iv, $data );  
  39.   
  40.       if ($errCode == 0) {  
  41.         return ($data . "\n");  
  42.       } else {  
  43.         return false;  
  44.       }  
  45.     }  
  46.   }  

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

 

转载于:https://my.oschina.net/u/1168113/blog/1634579

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值