关于thinkphp与⼩程序授权登录的⼼得,加上完整代码1.⼩程序端代
var app = getApp();
var url = 'shzujune/mianya/public/index/index/wxlogin';
var login = function (code, encryptedData, iv, signature, rawData) {
var that = this
//创建⼀个dialog提⽰
wx.showToast({
title: '正在登录...',
icon: 'loading',
duration: 5000
});
url: url,
method: 'get',
data: {
code: code,
encryptedData: encryptedData,
iv: iv,
signature:signature,
rawData:rawData
},
header: {
'Content-Type': 'application/json'
},
success: function (res) {
wx.hideToast()
//console.log('服务器返回' + res.data)
app.globalData.userInfo = res.data
},
fail: function () {
wx.showToast({
title: '⽹络错误!',
duration: 2000
})
},
complete: function () {
}
})
}
Page({
data: {
//判断⼩程序的API,回调,参数,组件等是否在当前版本可⽤。
canIUse: wx.canIUse('UserInfo')
},
php简单秒杀实例代码
onLoad: function () {
var that = this
wx.login({
success: function (res) { //登录成功
//console.log(res)
if (de) {
var code = de
success: function (data) { //getUserInfo获取⽤户信息成功
//console.log(data)
//encryptedData加密密⽂,iv偏移向量,encodeURIComponent把加密字符串解密成URI字符串
var encryptedData = ptedData);
var iv = encodeURIComponent(data.iv);
var signature = data.signature
var rawData = data.rawData
//请求⾃⼰的服务器
login(code, encryptedData, iv, signature, rawData);
//已经授权的⽤户
wx.switchTab({
url: '../rec/rec',
})
}
})
} else {
console.log('⽤户没有进⾏授权!' + Msg)
}
}
});
},
bindGetUserInfo: function (e) {
//console.log(e)
if (e.detail.userInfo) {
//⽤户按了允许授权的按钮
var that = this
wx.login({
success: function (res) {
if (de) {
var code = de
success: function (data) {
var encryptedData = ptedData); var iv = encodeURIComponent(data.iv);
var signature = data.signature;
var rawData = data.rawData;
//请求⾃⼰的服务器
login(code, encryptedData, iv, signature, rawData);
}
})
}
}
})
//授权成功后,跳转进⼊⼩程序⾸页
wx.switchTab({
url: '../rec/rec'
})
} else {
/
/⽤户按了拒绝按钮
wx.showModal({
title: '警告',
content: '您点击了拒绝授权,将⽆法进⼊⼩程序,请授权之后再进⼊', showCancel: false,
confirmText: '返回授权',
success: function (res) {
if (firm) {
console.log('⽤户点击了“返回授权”')
}
}
})
}
},
})
2.php 端代码
<?php
namespace Home\Controller;
use Home\Common\CommonController;
/**
* ⼩程序登录类
*/
class WxloginController extends CommonController
{
public function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
//解密⽤户敏感数据
public function login()
{
// 接收参数
$data = $_GET;
//echo json_encode($data);
// 引⼊解密⽂件在⼩程序开发⽂档下载
vendor('Wx.wxBizDataCrypt');
vendor('Wx.errorCode');
$appid = "wx49b4769062bf";
$appsecret = "34d69fed605a49be9b6b0bc";
$grant_type = "authorization_code"; //授权(必填)
$code = $data['code']; //有效期5分钟登录会话
$encryptedData=urldecode($data['encryptedData']);
//echo json_encode($encryptedData);
$iv = urldecode($data['iv']);
//echo json_encode($iv);
$signature = $data['signature'];
$rawData = $data['rawData'];
// 拼接url
$url = "api.weixin.qq/sns/jscode2session?"."appid=".$appid."&secret=".$appsecret."&js_co
de=".$code."&grant_type=".$grant_type; $res = json_decode($this->httpGet($url),true);
$sessionKey = $res['session_key']; //取出json⾥对应的值
$signature2 = sha1(htmlspecialchars_decode($rawData).$sessionKey);
// 验证签名
if ($signature2 !== $signature){
echo json_encode("验签失败");
}
// 获取解密后的数据
$pc = new \WXBizDataCrypt($appid, $sessionKey);
$errCode = $pc->decryptData($encryptedData, $iv, $data);
if ($errCode == 0) {
echo json_encode($data);
} else {
echo json_encode($errCode);
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论