当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序授权获取用户详细信息openid的实例详解

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

小程序获取用户的头像昵称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版本

官方文档:

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

下载之后在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('/http/controllers/admin/php/wxbizdatacrypt.php');


 // 获取微信用户信息
  public function getwxlogin(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 {
        return false;
      }
    }
  }

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

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网