微信小程序给出的demo并不是很详细,让人看起来很困惑,小编在此贴出代码供各位朋友参考。
小程序端代码
my.js
Page({
data: {
},
onLoad: function (options) {
var that = this
wx.checkSession({
success: function () {
console.log("登陆未过期")
wx.getStorage({
key: 'thirdsession',
success: function (res) {
console.log("客户端有缓存")
var sessionid = res.data
//console.log(sessionid)
wx.getUserInfo({
success: function (res) {
var encryptedData = res.encryptedData
var iv = res.iv
wx.request({
url: '你的域名/api/getuserinfo.php',
data: {
sessionid: sessionid,
encryptedData: encryptedData,
iv: iv
},
method: 'GET',
success: function (res) {
//console.log(res.data)
that.setData({
nickName: res.data.nickName,
avatarUrl: res.data.avatarUrl
})
//此处判断解密数据返回的状态码,如果为负数说明服务器端redis缓存失效,就需要重新走一遍登陆流程
if (res.data
//console.log("0")
wx.login({
success: function (res) {
var jcode = res.code
wx.request({
url: '你的域名/api/getsession.php',
data: {
jcode: jcode
},
method: 'GET',
success: function (res) {
//console.log(res)
var thirdsession = res.data.sessionid
wx.setStorage({
key: 'thirdsession',
data: thirdsession,
success: function () {
var sessionid = thirdsession
//console.log(sessionid)
wx.getUserInfo({
success: function (res) {
var encryptedData = res.encryptedData
var iv = res.iv
wx.request({
url: '你的域名/api/getuserinfo.php',
data: {
sessionid: sessionid,
encryptedData: encryptedData,
iv: iv
},
method: 'GET',
success: function (res) {
that.setData({
nickName: res.data.nickName,
avatarUrl: res.data.avatarUrl
})
}
})
}
})
}
})
}
})
}
})
} else {
//console.log("解密成功")
}
}
})
}
})
},
fail: function (res) {
console.log("客户端没有缓存")
wx.login({
success: function (res) {
//console.log(res.code)
var jcode = res.code
wx.request({
url: '你的域名/api/getsession.php',
data: {
jcode: jcode
},
method: 'GET',
success: function (res) {
//console.log(res.data)
var thirdsession = res.data.sessionid
wx.setStorage({
key: 'thirdsession',
data: thirdsession,
success: function () {
var sessionid = thirdsession
//console.log(sessionid)
wx.getUserInfo({
success: function (res) {
//console.log(res.iv)
var encryptedData = res.encryptedData
var iv = res.iv
wx.request({
url: '你的域名/api/getuserinfo.php',
data: {
sessionid: sessionid,
encryptedData: encryptedData,
iv: iv
},
method: 'GET',
success: function (res) {
that.setData({
nickName: res.data.nickName,
avatarUrl: res.data.avatarUrl
})
}
})
}
})
}
})
}
})
}
})
}
})
},
fail: function () {
console.log("登陆已过期")
}
})
},
})
my.wxml
{{nickName}}
getsession.php
$appid='你的appid';
$secret='你的secret';
$grant_type='authorization_code';
$jscode=$_GET['jcode'];
$url="https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$jscode&grant_type=$grant_type";
$fh= file_get_contents($url);
$array=json_decode($fh,true);
$openid=isset($array['openid'])?$array['openid']:$array['errcode'];
$session_key=$array['session_key'];
$expires_in=$array['expires_in'];
$sessionid= md5(uniqid(mt_rand()));
$response["result"] = 1;
$response["msg"] = "返回sessionid";
$response["sessionid"] = $sessionid;
echo json_encode($response);
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->set($sessionid,$openid.';'.$session_key, 7200);
?>
getuserinfo.php
include_once "wxBizDataCrypt.php"; //(wxBizDataCrypt.php下载微信小程序解密demo php版本即可获得)
$appid='你的appid';
$secret='你的secret';
$sessionid=$_GET['sessionid'];
$encryptedData=$_GET['encryptedData'];
$iv=$_GET['iv'];
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$value=$redis->get($sessionid);
$arraylist=split(";",$value);
$openid=$arraylist[0];
$session_key=$arraylist[1];
$pc = new WXBizDataCrypt($appid, $session_key);
$errCode = $pc->decryptData($encryptedData, $iv, $data );
if ($errCode == 0) {
print($data . "\n");
} else {
print($errCode . "\n"); //解密失败的话返回码均为负数,在微信小程序客户端my.js要用到
}
?>
以上内容如有不明白的,可以添加1688源码 微信公众号,给我们留言,还可获取源码哦。