php實(shí)現(xiàn)微信小程序授權(quán)登錄功能(實(shí)現(xiàn)流程)
先上圖
實(shí)現(xiàn)流程:
1、授權(quán)登陸按鈕和正文信息放到了同一個(gè)頁(yè)面,未授權(quán)的時(shí)候顯示登陸按鈕,已授權(quán)的時(shí)候隱藏登陸按鈕,顯示正文信息,當(dāng)然也可以授權(quán)和正文分開(kāi)成兩個(gè)頁(yè)面,在授權(quán)頁(yè)面的onload里判斷是否已授權(quán),若已授權(quán)就直接跳轉(zhuǎn)正文的頁(yè)面。這里只說(shuō)授權(quán)按鈕和正文在同一頁(yè)面的情況。
2、在onload里先判斷是否已授權(quán),如果已授權(quán),就隱藏授權(quán)登陸按鈕,顯示正文信息,如果沒(méi)有授權(quán),顯示授權(quán)登陸按鈕。
3、前端使用button的open-type="getUserInfo"
來(lái)操作,點(diǎn)擊授權(quán)按鈕之后,“e”中會(huì)攜帶userInfo,用戶(hù)的基本信息(和使用wx.getUserInfo接口獲取的數(shù)據(jù)一樣,所以我是在"e"里面直接取的,沒(méi)有調(diào)用wx.getUserInfo接口)
4、使用wx.login接口獲取登陸憑證code,使用code去后解密換取openid,傳輸code的時(shí)候帶上第3步獲取的用戶(hù)信息一塊發(fā)送給后臺(tái)解密(也可以不攜帶,攜帶的目的是為了驗(yàn)證簽名,這樣安全一些,不驗(yàn)證也可以)
5、后臺(tái)解密使用的是“auth.code2Session”接口,解密用到的SDK下載地址
“https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html”。
5、后臺(tái)解密之后(后臺(tái)語(yǔ)言用的是php),會(huì)返回openid等敏感信息,就還可以把這些信息存起來(lái)了。
6、獲取授權(quán)成功之后,再隱藏授權(quán)登陸按鈕,顯示正文信息。
7、如果用戶(hù)點(diǎn)擊拒絕授權(quán),提示引導(dǎo)用戶(hù)再次授權(quán)。
注意,要考慮到授權(quán)失敗的情況
以下是詳細(xì)代碼
wxml
<view wx:if="{{isHide}}"> <view wx:if="{{canIUse}}" > <view class='header'> <image src='/images/icon/wx_login.png'></image> </view> <view class='content'> <view>申請(qǐng)獲取以下權(quán)限</view> <text>獲得你的公開(kāi)信息(昵稱(chēng),頭像等)</text> </view> <button class='bottom' type='primary' open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="bindGetUserInfo"> 授權(quán)登錄 </button> </view> <view wx:else>請(qǐng)升級(jí)微信版本</view> </view> <view wx:else> <view>我的首頁(yè)內(nèi)容</view> </view>
wxss
.header { margin: 90rpx 0 90rpx 50rpx; border-bottom: 1px solid #ccc; text-align: center; width: 650rpx; height: 300rpx; line-height: 450rpx; } .header image { width: 200rpx; height: 200rpx; } .content { margin-left: 50rpx; margin-bottom: 90rpx; } .content text { display: block; color: #9d9d9d; margin-top: 40rpx; } .bottom { border-radius: 80rpx; margin: 70rpx 50rpx; font-size: 35rpx; }
js
// pages/test1/test1.js var app = getApp(); Page({ /** * 頁(yè)面的初始數(shù)據(jù) */ data: { //判斷小程序的API,回調(diào),參數(shù),組件等是否在當(dāng)前版本可用。 canIUse: wx.canIUse('button.open-type.getUserInfo'), isHide: false }, /** * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載 */ onLoad: function (options) { var that = this; // 查看是否授權(quán) wx.getSetting({ success: function (res) { if (!res.authSetting['scope.userInfo']) { // 還未授權(quán),顯示授權(quán)按鈕 that.setData({ isHide: true }); } else { // 已授權(quán),隱藏授權(quán)按鈕,顯示正文 that.setData({ isHide: false }); } } }) }, //授權(quán)登陸按鈕 bindGetUserInfo: function (e) { var that = this; console.log(e) if (e.detail.userInfo) { //用戶(hù)授權(quán)登陸,并跳轉(zhuǎn)首頁(yè) // that.getOpenid() wx.login({ success: function (res) { // 請(qǐng)求自己后臺(tái)獲取用戶(hù)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轉(zhuǎn)對(duì)象 //授權(quán)成功返回的數(shù)據(jù),根據(jù)自己需求操作 console.log(data) //授權(quán)成功后,隱藏授權(quán)按鈕,顯示正文 that.setData({ isHide: false }); } }, fail: function () { that.showModal('獲取授權(quán)信息失敗') } }) } }) } else { //用戶(hù)按了拒絕授權(quán)按鈕,提示引導(dǎo)授權(quán) that.showModal('請(qǐng)授權(quán)后使用小程序') } }, //未授權(quán)彈窗 showModal: function (e) { wx.showModal({ title: '提示', content: e, showCancel: false, confirmText: '返回授權(quán)', success: function (res) { if (res.confirm) { console.log('用戶(hù)點(diǎn)擊了“返回授權(quán)”') } } }) }, })
php
<?php namespace app\teacherapi\controller; use think\Controller; /** * @date: 2018-12 * 微信操作類(lèi) */ 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: 解密微信用戶(hù)敏感數(shù)據(jù) * @return array */ public function WxDecode() { // 接收參數(shù) $data = request() -> param(); // 引入解密文件 在微信小程序開(kāi)發(fā)文檔下載 vendor('wx.WXBizDataCrypt'); vendor('wx.ErrorCode'); $appid = config('TESTPPID'); $appsecret = config('TESTSECREET'); $grant_type = "authorization_code"; //授權(quán)(必填) $code = $data['code']; //有效期5分鐘 登錄會(huì)話(huà) $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里對(duì)應(yīng)的值 $signature2 = sha1(htmlspecialchars_decode($rawData).$sessionKey); // 驗(yàn)證簽名 if ($signature2 !== $signature){ return json("驗(yàn)簽失敗"); } // 獲取解密后的數(shù)據(jù) $pc = new \WXBizDataCrypt($appid, $sessionKey); $errCode = $pc->decryptData($encryptedData, $iv, $data ); if ($errCode == 0) { return return_succ($data); } else { return return_error($errCode); } } }
- PHP接入Apple對(duì)access_token/identityToken進(jìn)行JWT驗(yàn)證流程詳解
- php獲取微信基礎(chǔ)接口憑證Access_token
- PHP cURL獲取微信公眾號(hào)access_token的實(shí)例
- PHP定時(shí)任務(wù)獲取微信access_token的方法
- 微信小程序開(kāi)發(fā)之獲取用戶(hù)手機(jī)號(hào)碼(php接口解密)
- PHP實(shí)現(xiàn)微信小程序在線(xiàn)支付功能(代碼實(shí)例)
- php實(shí)現(xiàn)微信小程序訂閱消息推送(access_token獲取緩存刷新)
相關(guān)文章
Laravel框架使用Seeder實(shí)現(xiàn)自動(dòng)填充數(shù)據(jù)功能
這篇文章主要介紹了Laravel框架使用Seeder實(shí)現(xiàn)自動(dòng)填充數(shù)據(jù)功能,結(jié)合實(shí)例形式分析了Laravel基于Seeder類(lèi)實(shí)現(xiàn)自動(dòng)填充數(shù)據(jù)的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-06-06php實(shí)現(xiàn)的短網(wǎng)址算法分享
這篇文章主要介紹了php實(shí)現(xiàn)的短網(wǎng)址算法,理論上支持1,073,741,824個(gè)短網(wǎng)址,個(gè)人使用足夠了,需要的朋友可以參考下2014-06-06CentOS 安裝 PHP5.5+Redis+XDebug+Nginx+MySQL全紀(jì)錄
這篇文章主要介紹了在CentOS系統(tǒng)環(huán)境下安裝 PHP5.5+Redis+XDebug+Nginx+MySQL開(kāi)發(fā)環(huán)境的全過(guò)程,非常的細(xì)致詳盡,推薦給有需要的小伙伴們參考下吧。2015-03-03Laravel 讀取 config 下的數(shù)據(jù)方法
今天小編就為大家分享一篇Laravel 讀取 config 下的數(shù)據(jù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10ThinkPHP實(shí)現(xiàn)批量刪除數(shù)據(jù)的代碼實(shí)例
這篇文章主要介紹了ThinkPHP實(shí)現(xiàn)批量刪除數(shù)據(jù)的代碼實(shí)例,需要的朋友可以參考下2014-07-07PHP代碼實(shí)現(xiàn)爬蟲(chóng)記錄——超管用
這篇文章主要通過(guò)創(chuàng)建crawler數(shù)據(jù)庫(kù),使用robot.php記錄來(lái)訪(fǎng)的爬蟲(chóng)信息從而將信息插入數(shù)據(jù)庫(kù),從而使用php代碼實(shí)現(xiàn)爬蟲(chóng)記錄,有需要的小伙可以來(lái)參考下。2015-07-07用PHP實(shí)現(xiàn)的服務(wù)端socket具體實(shí)例
這篇文章主要介紹了用PHP實(shí)現(xiàn)的服務(wù)端socket具體實(shí)例,有對(duì)這方面不懂的同學(xué)可以參考下2021-01-01