php实现微信小程序授权登录功能(实现流程)(2)
js
// pages/test1/test1.js var app = getApp(); Page({ /** * 页面的初始数据 */ data: { //判断小程序的API,回调,参数,组件等是否在当前版本可用。 canIUse: wx.canIUse('button.open-type.getUserInfo'), isHide: false }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { var that = this; // 查看是否授权 wx.getSetting({ success: function (res) { if (!res.authSetting['scope.userInfo']) { // 还未授权,显示授权按钮 that.setData({ isHide: true }); } else { // 已授权,隐藏授权按钮,显示正文 that.setData({ isHide: false }); } } }) }, //授权登陆按钮 bindGetUserInfo: function (e) { var that = this; console.log(e) if (e.detail.userInfo) { //用户授权登陆,并跳转首页 // that.getOpenid() wx.login({ success: function (res) { // 请求自己后台获取用户openid wx.request({ url: app.domain + 'teacherapi/Wx_Decode/WxDecode', method: 'POST', header: { 'content-type': 'application/x-www-form-urlencoded' }, data: { encryptedData: e.detail.encryptedData, signature: e.detail.signature, rawData: e.detail.rawData, iv: e.detail.iv, code: res.code }, success: function (res_user) { if (res_user.data.status == 0) { var data = JSON.parse(res_user.data.msg) //json转对象 //授权成功返回的数据,根据自己需求操作 console.log(data) //授权成功后,隐藏授权按钮,显示正文 that.setData({ isHide: false }); } }, fail: function () { that.showModal('获取授权信息失败') } }) } }) } else { //用户按了拒绝授权按钮,提示引导授权 that.showModal('请授权后使用小程序') } }, //未授权弹窗 showModal: function (e) { wx.showModal({ title: '提示', content: e, showCancel: false, confirmText: '返回授权', success: function (res) { if (res.confirm) { console.log('用户点击了“返回授权”') } } }) }, })
php
<?php namespace app\teacherapi\controller; use think\Controller; /** * @date: 2018-12 * 微信操作类 */ class WxDecode extends Controller { 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; } /** * @author: zxf * @date: 2018-12-08 * @description: 解密微信用户敏感数据 * @return array */ public function WxDecode() { // 接收参数 $data = request() -> param(); // 引入解密文件 在微信小程序开发文档下载 vendor('wx.WXBizDataCrypt'); vendor('wx.ErrorCode'); $appid = config('TESTPPID'); $appsecret = config('TESTSECREET'); $grant_type = "authorization_code"; //授权(必填) $code = $data['code']; //有效期5分钟 登录会话 $encryptedData=$data['encryptedData']; $iv = $data['iv']; $signature = $data['signature']; $rawData = $data['rawData']; // 拼接url $url = "https://api.weixin.qq.com/sns/jscode2session?"."appid=".$appid."&secret=".$appsecret."&js_code=".$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){ return json("验签失败"); } // 获取解密后的数据 $pc = new \WXBizDataCrypt($appid, $sessionKey); $errCode = $pc->decryptData($encryptedData, $iv, $data ); if ($errCode == 0) { return return_succ($data); } else { return return_error($errCode); } } }
内容版权声明:除非注明,否则皆为本站原创文章。