微信登录
wx.login({
success (res) {
if (res.code) {
//发起网络请求
wx.request({
url: that.globalData.IP+'login/login',
data: {
code: res.code
},
success(res){
var info=JSON.parse(res.data)
console.log(info)
var openid=info.openid
that.globalData.key=info.session_key
//session_key用来获取手机号码
})
} else {
console.log('登录失败!' + res.errMsg)
}
}
})
},
获取用户信息
wx.getUserProfile({
desc: '正在获取', //不写不弹提示框
success: function (res) {
var userInfo=res.userInfo
console.log(res.userInfo)
wx.setStorageSync("userInfo",userInfo)//将信息存入缓存
},
fail: function (err) {
console.log("获取失败: ", err)
}
})
获取手机号
getPhoneNumber: function(e) {
var that=this
if (e.detail.errMsg == 'getPhoneNumber:fail user deny'){
wx.showModal({
title: '提示',
showCancel: false,
content: '未授权',
success: function (res) { }
})
} else {
wx.request({
url: app.globalData.IP+'login/getphone',
data:{
session_key:app.globalData.key, //微信登录获取的seesion_key
iv:e.detail.iv,
encryptedData:e.detail.encryptedData,
uid:app.globalData.userid //之前数据库中得到的uid
},
header: {
'content-type': 'application/json'
},
success(res){
console.log(res.data)
app.globalData.phone=res.data.phoneNumber
}
})
}
},
后台代码如下:
login.class
引用不能丢
include_once "wxBizDataCrypt.php";
public function login(Request $request){
$appid="****************";
$secret="***************";//小程序appid与secret配置,自己上网站查看
$code=input('get.code');
if(isset($code)&&$code<>""){
$js_code=$code;
}else{
die("ERROR");
}//获得js_code
$url="https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$secret."&js_code=".$js_code."&grant_type=authorization_code";
$info=$this->geturl($url);
return json($info);
}
public function getphone(){
$appid="******************";
$sessionKey =input('get.session_key');
$encryptedData=input('get.encryptedData');
$iv=input('get.iv');
$data='';
$pc = new WXBizDataCrypt($appid, $sessionKey);
$errCode = $pc->decryptData($encryptedData, $iv, $data );
if ($errCode == 0) {
$data=json_decode($data,true);
$user=User::where('uid',input('get.uid'))->find();
$user->uphone=$data['phoneNumber'];
$user->save();
return json($data);
} else {
return json('f');
}
}
function geturl($url){
$info=curl_init();
curl_setopt($info,CURLOPT_RETURNTRANSFER,true);
curl_setopt($info,CURLOPT_HEADER,0);
curl_setopt($info,CURLOPT_NOBODY,0);
curl_setopt($info,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($info,CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($info,CURLOPT_URL,$url);
$output= curl_exec($info);
curl_close($info);
return $output;
}
WXBizDataCrypt.class
<?php
/**
* 对微信小程序用户加密数据的解密示例代码.
*
* @copyright Copyright (c) 1998-2014 Tencent Inc.
*/
include_once "errorCode.php";
class WXBizDataCrypt
{
private $appid;
private $sessionKey;
/**
* 构造函数
* @param $sessionKey string 用户在小程序登录后获取的会话密钥
* @param $appid string 小程序的appid
*/
public function __construct( $appid, $sessionKey)
{
$this->sessionKey = $sessionKey;
$this->appid = $appid;
}
/**
* 检验数据的真实性,并且获取解密后的明文.
* @param $encryptedData string 加密的用户数据
* @param $iv string 与用户数据一同返回的初始向量
* @param $data string 解密后的原文
*
* @return int 成功0,失败返回对应的错误码
*/
public function decryptData( $encryptedData, $iv, &$data )
{
if (strlen($this->sessionKey) != 24) {
return ErrorCode::$IllegalAesKey;
}
$aesKey=base64_decode($this->sessionKey);
if (strlen($iv) != 24) {
return 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 ErrorCode::$IllegalBuffer;
}
if( $dataObj->watermark->appid != $this->appid )
{
return ErrorCode::$IllegalBuffer;
}
$data = $result;
return ErrorCode::$OK;
}
}
errorCode.class
<?php
/**
* error code 说明.
* <ul>
* <li>-41001: encodingAesKey 非法</li>
* <li>-41003: aes 解密失败</li>
* <li>-41004: 解密后得到的buffer非法</li>
* <li>-41005: base64加密失败</li>
* <li>-41016: base64解密失败</li>
* </ul>
*/
class ErrorCode
{
public static $OK = 0;
public static $IllegalAesKey = -41001;
public static $IllegalIv = -41002;
public static $IllegalBuffer = -41003;
public static $DecodeBase64Error = -41004;
}
?>