微信小程序+php登录和获取用户信息

in 源码分享技术分享 / 0 评论 / 612阅读

在微信小程序中,有部分内容需要涉及用户认证,一般有以下方式:微信小程序登录、账号或密码登录、账号密码登录后绑定微信小程序。为了用户使用微信小程序更方便,一般会使用微信小程序登录或者登录后绑定微信小程序。微信小程序中,也提供了微信登录接口wx.login,通过服务端解密获取用户唯一openid。

登录

微信提供了解密接口,只需服务端使用appid、secret和小程序提交的code就可以解密获取openid和sessionkey(解密用户信息需要)。

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html

微信小程序部分,测试我是写在onload函数加载里面的,小程序登录获取openid是不需要用户确认登录,就可以直接获取openid。

  onLoad() {
    wx.login({
      success:function(result){
        wx.request({
          url: 'http://test.test/login.php',
          data:{code:result.code},
          success:function(result){
            console.log(result.data);
          }
        })
        console.log(result)
      }
    })
  }

php部分,经过测试,微信小程序测试号可能因为secret问题,无法解密获取openid,后台无法重置,所以建议直接使用微信小程序的appid和secret。

<?php
$appid = 'wx********';
$secret = '7f**********66';
$code = $_GET['code'];
$json = file_get_contents("https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code");
echo $json;
?>

获取用户信息

通过微信小程序的wx.getUserInfo获取用户信息,返回的encryptedData是通过AES-128-CBC加密,采用PKCS#7填充。于是将ivencryptedData和登录解密的session_key解密得到安全的用户信息。

解密流程

小程序js,通过提交获取用户信息按钮用户授权后获取信息,提交服务器

  getUserInfo(e) {
    console.log(e)
    wx.login({
      success: function (lres) {
        wx.getUserInfo({
          success: function (ures) {
            console.log(ures)
            wx.request({
              url: "http://test.test/getuserinfo.php",
              data: {
                code: lres.code,
                encryptedData: ures.encryptedData,
                iv: ures.iv,
              },
              success: function (result) {
                console.log(result)
              }
            })
          }
        })
      }
    });
  }

php,解密用户信息

<?php

function decryptData($appid,$sessionKey,$encryptedData, $iv)
{
    if (strlen($sessionKey) != 24) {
        return false;
    }
    $aesKey=base64_decode($sessionKey);

    
    if (strlen($iv) != 24) {
        return false;
    }
    $aesIV=base64_decode($iv);

    $aesCipher=base64_decode($encryptedData);

    $result=openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);

    $dataObj=json_decode($result);
    if($dataObj  == NULL)
    {
        return false;
    }
    if($dataObj->watermark->appid != $appid)
    {
        return false;
    }
    return$result;
}


$appid = 'wx********';
$secret = '7f**********66';

$code = $_GET['code'];
$encryptedData = $_GET['encryptedData'];
$iv = $_GET['iv'];

$loginJson = file_get_contents("https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code");
$login = json_decode($loginJson,true);

$data = decryptData($appid,$login['session_key'],$encryptedData,$iv);
print_r($data);

?>

用户信息

微信小程序官方文档信息:

//appid解密
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
//用户信息校验与解密
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html#method-decode
//用户信息解密demo
https://res.wx.qq.com/wxdoc/dist/assets/media/aes-sample.eae1f364.zip
回复