var inurl = app.globalData.url + "m/wechat_api.php?rec=index&page=1&num=8&username=" + app.globalData.userid;
this.http(inurl, 10, this.callback);
http:function(category,count,callback){
wx.request({
url: inurl,
data: {},
header: {
'content-type': 'application/json' // 默认值
},
success: function (res) {
callback(res.data)
}
})
},
callback:function(res){
//数据处理
}
bindGetUserInfo: function (e) {
var that = this
wx.login({
success: function (res) {
var code = res.code
wx.getSetting({
success: function (res) {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称
wx.getUserInfo({
success: function (res) {
//微信用户头像
that.setData({
logo: res.userInfo.avatarUrl,
name: res.userInfo.nickName,
})
// that.globalData.userInfo = res.userInfo
var rawData = res.rawData;
var signature = res.signature;
var encryptedData = res.encryptedData;
var iv = res.iv;
//登录动画
wx.showLoading({
title: '登录中...',
})
wx.request({
url: app.globalData.url + "m/wechat_api.php?rec=wechat_code",
data: {
"code": code,
"rawData": rawData,
"signature": signature,
"iv": iv,
"encryptedData": encryptedData
},
method: 'GET',
success: function (res) {
// success
that.setData({
openid: res.data.openid
})
console.log(res.data);
//返回openid session_key
//获取到的3rd_session保存在本地
wx.setStorageSync('3rd_session', res.data);
//发送查询
wx.request({
url: app.globalData.url + "m/wechat_api.php?rec=getsession",
data: {
"rd3_session": res.data
},
method: 'GET',
success: function (res) {
if (!res.data) {
console.log('登陆后失效了');
} else {
console.log('登录后没有失效');
app.globalData.userid = res.data.user_name;
app.globalData.logininfo = res.data;
app.globalData.canIUse = 0;
//刷新当前登录状态
that.onShow();
//动画结束
setTimeout(function () {
wx.hideLoading()
}, 100)
}
}
})
//查询结束
}
})
}
})
} else {
console.log('用户不给授权');
wx.showLoading({
title: '授权失败,返回首页',
})
setTimeout(function () {
wx.hideLoading()
wx.switchTab({
url: '/pages/index/index'
})
}, 1000)
}
}
})
}
})
}
elseif($rec == "wechat_code"){
$APPID = ''; //小程序APPID
$AppSecret = ''; //小程序secret
$code = $_GET['code'];
$url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$APPID.'&secret='.$AppSecret.'&js_code='.$code.'&grant_type=authorization_code';
$arr = vegt($url);
$arr = json_decode($arr,true);
// $openid = $arr['openid'];
$session_key = $arr['session_key'];
// 数字签名校验
$signature = $_GET['signature'];
$aaa = stripslashes($_GET['rawData']);
$signature2 = sha1($aaa.$session_key);
if($signature != $signature2){
echo "数字签名失败";
die;
}
// 获取信息,对接口进行解密
// Vendor("PHP.wxBizDataCrypt");
$encryptedData = $_GET['encryptedData'];
$iv = $_GET['iv'];
if(empty($signature) || empty($encryptedData) || empty($iv)){
echo "传递信息不全";
}
include_once "php/wxBizDataCrypt.php";
$pc = new \WXBizDataCrypt($APPID,$session_key);
$errCode = $pc->decryptData($encryptedData,$iv,$data);
if($errCode != 0){
echo "解密数据失败";
die;
}else {
$data = json_decode($data,true);
$_SESSION['myinfo'] = $data;
$save['openid'] = $data['openId'];
$save['uname'] = $data['nickName'];
$save['unex'] = $data['gender'];
$save['address'] = $data['city'];
$images = $data['avatarUrl'];
$time = time();
$map['openid'] = $data['openId'];
!empty($data['unionId']) && $save['unionId'] = $data['unionId'];
//save user
$query = $dou->select($dou->table('user'), '*', '`user_name` = \'' . $map['openid']. '\'');
$user = $dou->fetch_array($query);
if(!$user['user_name']){
$openid = $map['openid'];
$name = $save['uname'];
$sql = "INSERT INTO " . $dou->table('user') . " (user_name,name,images,time,jifen) VALUES ('$openid','$name','$images','$time','0')";
$dou->query($sql);
}
}
//生成第三方3rd_session
$session3rd = null;
$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($strPol)-1;
for($i=0;$i<16;$i++){
$session3rd .=$strPol[rand(0,$max)];
}
//判断有无此openid
$query = $dou->select($dou->table('wechat_log'), '*', '`open_id` = \'' . $map['openid']. '\'');
$user = $dou->fetch_array($query);
if($user['open_id']){
$openid = $map['openid'];
$sql = "UPDATE " . $dou->table('wechat_log') . " SET session_key = '$session_key',3rd_session='$session3rd' WHERE open_id = '$openid'";
$dou->query($sql);
echo $session3rd;
exit;
}
$sql = "INSERT INTO " . $dou->table('wechat_log') . " (3rd_session,session_key,open_id) VALUES ('".$session3rd."','".$session_key."','".$map['openid']."')";
$dou->query($sql);
echo $session3rd;
//鉴权end
}
小程序执行顺序是双线程执行 业务逻辑自己把握
// app.js执行
var that = this;
wx.checkSession({
success: function (res) {
console.log("登录态没失效");
//获取用户3rd_session
var rd3_session = wx.getStorageSync('3rd_session');
wx.request({
url: "https://xxxxx.com/m/wechat_api.php?rec=getsession",
data: {
"rd3_session": rd3_session
},
method: 'GET',
success: function (res) {
if (!res.data) {
console.log('本地缓存已经失效');
app.globalData.canIUse = 1;
that.jz_index();
//console.log(that.globalData.canIUse);
} else {
console.log('本地缓存没有失效,不需要重新登录');
app.globalData.logininfo = res.data;
app.globalData.userid = res.data.user_name;
console.log(app.globalData.userid);
that.jz_index();
}
}
})
},
fail: function (res) {
console.log("需要重新登录");
app.globalData.canIUse = 1;
that.jz_index();
}
})
// app.js执行end
原文地址:https://150643.com/510.html