小程序登录代码://app.js
var config = require('/pages/config/config.js');
App({
onLaunch: function() {
//调用API从本地缓存中获取数据
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs);
var openId=(wx.getStorageSync('openId'));
console.log("openId:"+openId);
if (openId)
{
;//已经有过授权的
}
else
{
//---------------begin login
wx.login({
success: function (res) {
console.log("code:"+res.code)
if (res.code)
{
wx.getUserInfo({
withCredentials: true,
success: function (res_user)
{
console.log("reqUserLogin:" + config.api.reqUserLogin);
console.log("nickName:" + res_user.userInfo.nickName);
console.log("avatarUrl:" + res_user.userInfo.avatarUrl);
console.log("avatarUrl:" + res_user.userInfo.avatarUrl);
wx.request({
//后台接口地址
url: config.api.reqUserLogin,
data:
{
"code": res.code,
"signature": res_user.signature,
"wxid": config.static.wxid,
"encryptedData":res_user.encryptedData,
"iv":res_user.iv,
"rawData":res_user.rawData,
},
method: 'GET',
header:
{
'content-type': 'application/json'
},
success: function (res) {
//
console.log(res);
var result=res.data||[];
if (result.result=="ok")
{
var data = result.data;
wx.setStorageSync('uid', data.id);
wx.setStorageSync('openId', data.openid);
console.log("openId:" + data.openid + "--uid:" + data.id);
}
else
{
console.log("result:" + result.msg);
}
}
})
},
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: config.api.reqUserLogin,
data:
{
code:res_login.code,
wxid: config.static.wxid,
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) {
}
})
}
}
})
//=-----------------end login
}
//------------end if openid
},
getUserInfo: function(cb) {
var that = this
if (this.globalData.userInfo) {
typeof cb == "function" && cb(this.globalData.userInfo)
} else {
//调用登录接口
wx.getUserInfo({
withCredentials: false,
success: function(res) {
that.globalData.userInfo = res.userInfo
typeof cb == "function" && cb(that.globalData.userInfo)
}
})
}
},
globalData: {
userInfo: null
}
})
thinkphp 后台代码:$code=I('get.code');
$wxid=I('get.wxid');
/*
$AppId=C("WXID")[$wxid]['appId'];
$AppSecret=C("WXID")[$wxid]['appSecret'];
*/
$AppId="wx85bfad0b61d6902a";
$AppSecret="efd5deedc28929a51372f896f47c550a";
$session_key="";
$url="https://api.weixin.qq.com/sns/jscode2session?appid=".$AppId."&secret=".$AppSecret."&js_code=".$code."&grant_type=authorization_code";
$result =$this->vget($url);
$result=json_decode($result,true);
if(empty($result['openid']))
{
$this->ajaxReturn(array("result"=>"err","msg"=>"openid err url:".$url."---".$result['errcode'].":".$result['errmsg']));
die;
}
else
{
$session_key=$result['session_key'];
$openid=$result['openid'];
}
// 数据签名校验
$signature = I('get.signature');
$signature2 = sha1($_GET['rawData'].$session_key); //记住不应该用TP中的I方法,会过滤掉必要的数据
if ($signature != $signature2)
{
$this->ajaxReturn(array("result"=>"err","msg"=> '数据签名验证失败!'));
die;
}
$iv=$_GET['iv'];
unset($_GET['_URL_']);
$_GET['encryptedData']=urlencode($_GET['encryptedData']);
$_GET['encryptedData'] = str_replace("%2B","+",$_GET['encryptedData']);
$_GET['encryptedData'] = str_replace("%3D","=",$_GET['encryptedData']);
$_GET['encryptedData'] = str_replace("%2F","/",$_GET['encryptedData']);
$encryptedData=$_GET['encryptedData'];
vendor("PHP-WX.wxBizDataCrypt");
$pc = new \WXBizDataCrypt($AppId, $session_key);
$errCode = $pc->decryptData($encryptedData, $iv, $data );
//$errCode=0;
if ($errCode == 0)
{
//----获得 openid
//------------------------------------------------------------------开始数据库操作
$userTab=M("Users")->field("id,openid,session_key,user_nicename,avatar")->where(array("openid"=>$openid))->find();
//echo M("Users")->getLastSql();
if($userTab!=false)
{
//已经存在 openid //----登录设置
$this->ajaxReturn(array("result"=>"ok","data"=>$userTab));
die;
}
else
{
$unionid=$data['unionid'];
$inData=array(
"openid"=>$openid,
"session_key"=>$session_key,
"user_login"=>"RZ".rand(100,999).$openid.rand(10,99),
"user_pass"=>sp_password($openid),
"user_nicename"=>$data['nickName'],
"avatar"=>$data['avatarUrl'],
"sex"=>$data['gender'],
"last_login_time"=>date("y-m-d H:i:s",NOW_TIME),
"create_time"=>date("y-m-d H:i:s",NOW_TIME)
);
if(M("User")->add($inData))
{
//----登录设置
$inData['id']=M("User")->getLastInsID();
$this->ajaxReturn(array("result"=>"ok","data"=>$inData));
die;
}
else
{
$this->ajaxReturn(array("result"=>"err","msg"=>"insert err"));
die;
}
}
//if($userTab!=false)
}
else
{
/**
* error code 说明.
*
*
-41001: encodingAesKey 非法*
-41003: aes 解密失败*
-41004: 解密后得到的buffer非法*
-41005: base64加密失败*
-41016: base64解密失败*
*/
$this->ajaxReturn(array("result"=>"err","msg"=>"解密数据失败!".$errCode."--encryptedData:".$encryptedData."----iv:".$iv));
die;
}
//vendor("php-AES.php-AES");
//$Curl = new \Curl($url,$data,'get','json');
//redirect($url);
//echo $code."
".$appId."
".$appId."
".$appSecret;