Laravel 集成微信用戶登錄和綁定的實現(xiàn)
最近主要在忙活微信與支付寶平臺的對接與開發(fā),本篇就基于后端層面來講述一下微信的登錄與綁定實現(xiàn)。
(一)、申請微信開放平臺
最首先的話就是需要去微信開發(fā)中心https://open.weixin.qq.com,創(chuàng)建一個賬號,然后創(chuàng)建自己的移動或網(wǎng)站應用。
創(chuàng)建完成后,就會需要騰訊的審核,整個過程在1-3天,基本上1天左右就能完成,審核通過如下圖所示。
(二) 、封裝微信相關接口
微信移動應用開發(fā)文檔:https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Login/Authorized_API_call_UnionID.html
審核通過后,就需要來封裝微信授權、可信息獲取的接口。
封裝微信授權 && 用戶信息獲取
微信授權接口:https://api.weixin.qq.com/sns/oauth2
需要填寫的參數(shù)如下:
| 參數(shù) | 是否必須 | 說明 | |
|---|---|---|---|
| appid | 是 | 應用唯一標識,在微信開放平臺提交應用審核通過后獲得 | |
| secret | 是 | 應用密鑰 AppSecret,在微信開放平臺提交應用審核通過后獲得 | |
| code | 是 | 填寫第一步獲取的 code 參數(shù) | |
| grant_type | 是 | 填 authorization_code |
下面通過我們的PHP代碼實現(xiàn):
<?php
namespace App\Helpers;
use GuzzleHttp\Client;
use Illuminate\Support\Arr;
class WechatAppUtils
{
protected $client = null;
protected $config = [];
public function __construct()
{
$this->config = [
'wechat_app' => [
'appid' => env('WECHAT_APPID'), //審核通過的APPID
'secret' => env('WECHAT_SECRET'), //應用APP SECRET 詳情見上圖
],
'time_out' => 5,
];
$this->client = new Client([
'time_out' => $this->config['time_out'],
]);
}
/**
* 獲取微信用戶access_token
*
* @param [String] $code
* @return Array
*/
public function accessToken($code)
{
$accessTokenUrl = 'https://api.weixin.qq.com/sns/oauth2/access_token';
$response = $this->client->request('GET', $accessTokenUrl, [
'query' => [
'grant_type' => 'authorization_code',
'code' => $code,
'appid' => Arr::get($this->config, 'wechat_app.appid'),
'secret' => Arr::get($this->config, 'wechat_app.secret'),
],
]);
$result = $response->getbody()->getContents();
return empty($result) ? null : json_decode($result, true);
}
/**
* 微信用戶信息
*
* @param [String] $accessToken
* @param [String] $openId
* @return Array
*/
public function userInfo($accessToken, $openId)
{
$userInfoUrl = 'https://api.weixin.qq.com/sns/userinfo';
$response = $this->client->request('GET', $userInfoUrl, [
'query' => [
'access_token' => $accessToken,
'openid' => $openId,
'lang' => 'zh_CN',
],
]);
$result = $response->getbody()->getContents();
return empty($result) ? null : json_decode($result, true);
}
}
上面的accessToken方法主要是實現(xiàn)用戶授權,效驗的code參數(shù)是客戶端傳遞過來的,當成功獲取收錢用戶的授權信息后,可以根據(jù)用戶的OPENID來調用userInfo方法查詢相關用戶的信息,包含了用戶的昵稱、頭像、性別等等。
具體客戶端開發(fā)文檔可以參考這篇:https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Login/Development_Guide.html。
上面的用到的Http Client是一個第三方拓展包,叫做GuzzleHttp,是一個PHP HTTP客戶端,可以輕松發(fā)送HTTP請求,并且可以輕松集成Web服務。
我們可以通過composer一鍵安裝:
composer require guzzlehttp/guzzle
(三)、完善用戶微信授權登錄
完成上述的封裝操作后,我們便開始講微信接入到我們自己的系統(tǒng)中與用戶進行關聯(lián)起來,下面是微信接入的一張時序圖。
如果用戶想使用微信登錄,首先會通過客戶端喚起微信,請求登錄第三方應用,然后微信會詢問用戶是否成功授權給XX應用,授權成功后,客戶端會得到一個授權碼:code,然后客戶端攜帶code請求我們的客戶端API,進行授權綁定,授權成功后,會得到授權用戶OPENID(應用下的唯一標識),反之拋出異常信息提示用戶。
建立OAuth表,用于儲存用戶的授權信息。
建立一張o_auths table 儲存用戶的授權信息,設計oauth_type字段使其成為一個多態(tài)模型,方便接入以后的微博、支付寶、QQ接入等等。
Schema::create('o_auths', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id')->index()->comment('用戶ID');
$table->morphs('o_auth');
$table->json('data')->nullable()->comment('授權信息');
$table->timestamps();
});
完善用戶授權綁定
建立好o_auths table,下面開始完善用戶授權綁定的邏輯:
function wechat(User $user, $code)
{
$utils = new WechatAppUtils;
//獲取微信token
$accessTokens = $utils->accessToken($code);
throw_if(!Arr::has($accessTokens, ['unionid', 'openid']), Exception::class, '授權失敗,請稍后再試!');
//建立oauth關聯(lián)
$oAuth = OAuth::firstOrNew(['oauth_type' => 'wechat', 'oauth_id' => $accessTokens['openid']]);
throw_if(isset($oAuth->id),Exception::class,'該微信已綁定,請直接登錄!');
$oAuth->user_id = $user->id;
$oAuth->data = Arr::only($accessTokens, ['openid', 'refresh_token']);
$oAuth->save();
return $oAuth;
}
首先會通過客戶端傳遞過來的Code獲取當前用戶授權,然后查詢該用戶是否已授權過,已授權過就提醒用戶直接去登錄,否則綁定授權信息,返回給客戶端。
完善微信登錄
完善好用戶授權后,登錄就顯得非常容易了,只需要簡單查詢授權記錄,存在則返回對應綁定的用戶,否則拋出異常信息提示用戶。
public function signIn($user, $code)
{
$utils = new WechatAppUtils;
//獲取微信token
$accessTokens = $utils->accessToken($code);
throw_if(!Arr::has($accessTokens, ['unionid', 'openid']), Exception::class, '授權失敗,請稍后再試!');
$oauth = $this->getUserOauth($user, 'wechat');
throw_if(is_null($oauth), UserException::class, '授權失敗,該賬戶未綁定!');
return $oauth;
}
public function getUserOauth(User $user, $oAuthType)
{
return OAuth::where(['oauth_type' => $oAuthType, 'user_id' => $user->id])->first();
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持我們。
上一篇:PHP+fiddler抓包采集微信文章閱讀數(shù)點贊數(shù)的思路詳解
欄 目:PHP編程
本文標題:Laravel 集成微信用戶登錄和綁定的實現(xiàn)
本文地址:http://www.jygsgssxh.com/a1/PHPbiancheng/10989.html
您可能感興趣的文章
- 01-11在phpstudy集成環(huán)境下的nginx服務器下配置url重寫
- 01-11Laravel 微信小程序后端搭建步驟詳解
- 01-11Laravel框架Blade模板簡介及模板繼承用法分析
- 01-11Laravel 微信小程序后端實現(xiàn)用戶登錄的示例代碼
- 01-11Laravel框架基礎語法與知識點整理【模板變量、輸出、include引入
- 01-11Laravel框架Eloquent ORM刪除數(shù)據(jù)操作示例
- 01-11Laravel框架Eloquent ORM修改數(shù)據(jù)操作示例
- 01-11Laravel框架Eloquent ORM簡介、模型建立及查詢數(shù)據(jù)操作詳解
- 01-11laravel5.1框架下的批量賦值實現(xiàn)方法分析
- 01-11laravel5.5框架的上傳圖片功能實例分析【僅傳到服務器端】


閱讀排行
本欄相關
- 04-02php本站才可以請求數(shù)據(jù) php本地數(shù)據(jù)庫
- 04-02關于txt數(shù)據(jù)庫php的信息
- 04-02php打印請求數(shù)據(jù) php打印輸出結果
- 04-02網(wǎng)頁里php操作數(shù)據(jù)庫 php網(wǎng)頁例子
- 04-02php插入數(shù)據(jù)庫為亂碼 php連接數(shù)據(jù)庫亂
- 04-02php數(shù)據(jù)庫地址 phpstudy 數(shù)據(jù)庫
- 04-02php數(shù)據(jù)庫數(shù)據(jù)相加 php數(shù)據(jù)庫添加數(shù)據(jù)
- 04-02數(shù)據(jù)權限架構思路php 數(shù)據(jù)權限設計方
- 04-02php數(shù)據(jù)庫輸入變量 php里輸出數(shù)據(jù)庫數(shù)
- 04-02php如何用導入數(shù)據(jù) php用來導入其他文
隨機閱讀
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實例總結
- 01-10使用C語言求解撲克牌的順子及n個骰子


