首頁  >  文章  >  web前端  >  微信小程式授權取得使用者詳細資料實例

微信小程式授權取得使用者詳細資料實例

小云云
小云云原創
2018-05-15 11:09:1117418瀏覽

本文主要和大家介紹微信小程式授權取得使用者詳細資料openid的實例詳解的相關資料,希望透過本文能幫助到大家,需要的朋友可以參考下,希望能幫助到大家。

小程式取得使用者的頭像暱稱openid之類

第一種使用wx.getUserInfo直接取得微信頭像,暱稱

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

第二

我們在使用小程式wx.login API登入的時候,直接使用wx.getUserInfo是不能取得更多的資訊的,如微信使用者的openid。
官方提示,需要發送獲取到的code進行請求到微信的後端API,進行用戶解密之類的操作才可以獲取,

根據文檔,只需要進行一個get請求到以下網址即可:

https://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檔案

var openId = (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版本

官方文件:

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

微信官方提供了多種程式語言的範例程式碼(點擊下載)。每種語言類型的介面名字均一致。調用方式可以參考範例。

下載後在php檔案中引入:

<?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(&#39;/Http/Controllers/Admin/PHP/wxBizDataCrypt.php&#39;);


 // 获取微信用户信息
  public function getWxLogin(Request $request)
  {
   // require_once ROOTPATH . "./PHP/wxBizDataCrypt.php";

    $code  =  $request->get(&#39;code&#39;);
    $encryptedData  =  $request->get(&#39;encryptedData&#39;);
    $iv  =  $request->get(&#39;iv&#39;);
    $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,&#39;wwwwwwww&#39;,$apiData[&#39;errscode&#39;]);
    //   $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[&#39;errcode&#39;])){
      $sessionKey = json_decode($apiData)->session_key;
      $userifo = new \WXBizDataCrypt($appid, $sessionKey);

      $errCode = $userifo->decryptData($encryptedData, $iv, $data );

      if ($errCode == 0) {
        return ($data . "\n");
      } else {
        return false;
      }
    }
  }

官方文件的登入流程圖,整個登入流程基本上如下圖所示:

相關推薦:

Thinkphp5微信小程式如何取得使用者資訊介面

微信小程式取得使用者資訊實例方法

微信小程式取得使用者資訊如何實作

#

以上是微信小程式授權取得使用者詳細資料實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn