内容详情 您现在的位置是: 首页> PHP
微信小程序 登录 获取unionID
发布时间:2020-11-25 19:20 已围观:1665
摘要微信小程序 登录 获取unionID
其次来说一下如何获取unionID,获取unionID需要解密encryptedData。需要调用wx.login来获取code,用code去换取session_key,以及调用wx.getUserInfo获取encryptedData和iv。
最后来贴一下代码:将code 、encryptedData和iv一起发送到后端
.js
wx.login({
success: res => {
console.log(res)
wx.getUserInfo({
success: function(result) {
console.log(result) if (res.code) {
this.login(result.encryptedData, result.iv,res.code)
}
}
})
}
})//登录
login: function (encryptedData, iv, code) {
var that = this;
var url = that.globalData.url; //解密用户信息
wx.request({
url: url + '/api.php',
method: 'GET', data: {
exec: 'get_unionid',
chid: that.globalData.chid,
skey: that.globalData.skey,
sign: that.globalData.sign,
encryptedData: encryptedData,
iv: iv,
code: code
},header: {
'Accept': 'application/json',
"Content-Type": "application/x-www-form-urlencoded"
},success: function (res) { //从数据库获取用户信息
console.log(res);
if (typeof res.data != 'string') {
return false
}
var msg = JSON.parse(res.data);
that.globalData.openId = msg.openId, //从数据库获取用户信息
wx.request({
url: url + '/api2user.php',
method: 'POST',
data: {
exec: 'login',
chid: that.globalData.chid,
skey: that.globalData.skey,
sign: that.globalData.sign,
ukey: msg.openId,
unionId: msg.unionId,
},header: {
'Accept': 'application/json',
"Content-Type": "application/x-www-form-urlencoded"
},
success: function (res) {
console.log(res.data);
if (!res.data.status) {
var list = res.data.list;
if (list) {
that.globalData.uniq = res.data.uniq,
that.globalData.userInfo = res.data.list[0]
console.log(that.globalData.userInfo)
console.log("小程序登录用户信息成功!");
}
}
}
})
}
})
},
class get_unionid {
private $action;
private $result;
private $request;
private $sessionKey;
private $encryptedData ;
private $iv ;
private $appid = '你自己的APPID';
private $url = 'https://api.weixin.qq.com/sns/jscode2session';
private $secret = '你自己的secret';
private $js_code ;
public function __construct($basic = '') {
}
/**
* 接收参数
*/
public function put($input = '') {
$this->request = $input;
$this->encryptedData = $input['encryptedData'];
$this->iv = $input['iv'];
$this->js_code = $input['code'];
}
//主方法
public function logic($logic = '') {
//获取openID、session_key
$info = $this->get_info();
$info = json_decode($info,true);
if ($info) {
$this->sessionKey = $info['session_key'];
$errCode = $this->decryptData($this->encryptedData, $this->iv);
$this->result = $errCode;
}
}
/**
* 检验数据的真实性,并且获取解密后的明文.
* @param $encryptedData string 加密的用户数据
* @param $iv string 与用户数据一同返回的初始向量
* @param $data string 解密后的原文
*
* @return int 成功0,失败返回对应的错误码
*/
private function decryptData( $encryptedData, $iv )
{
if (strlen($this->sessionKey) != 24) {
return $this->ErrorCode('IllegalAesKey');
}
$aesKey=base64_decode($this->sessionKey);
if (strlen($iv) != 24) {
return $this->ErrorCode('IllegalIv');
}
$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 $this->ErrorCode('IllegalBuffer');
}
if( $dataObj->watermark->appid != $this->appid ){
return $this->ErrorCode('IllegalBuffer');
}
return $result;
}
/**
* error code 说明.
* -41001: encodingAesKey 非法
* -41003: aes 解密失败
* -41004: 解密后得到的buffer非法
* -41005: base64加密失败
* -41016: base64解密失败
*/
private function ErrorCode($msg)
{
switch($msg) {
case 'OK':
return 0;
break;
case 'IllegalAesKey':
return -41001;
break;
case 'IllegalIv':
return -41002;
break;
case 'IllegalBuffer':
return -41003;
break;
case 'DecodeBase64Error':
return -41004;
break;
}
}
//获取openID、session_key
private function get_info(){
$url = $this->url . '?appid='. $this->appid .'&secret='.$this->secret . '&js_code=' . $this->js_code .'&grant_type=authorization_code';
$ch = curl_init(); //设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,
赞一个 (5)
上一篇: php 抽奖概率 随机数
下一篇: phpstorm 断点调试 清楚简单教程