第一次寫專欄文章,如程式碼規範有錯誤或者不周全的地方,請社群朋友們指正。如果有更好的方法,請留言給我,謝謝各位大神。
這篇文章旨在Laravel使用EasyWechat擴充套件進行多公眾號管理,具體步驟如下:
- 建立公眾號參數列
- 建立微信控制器
- 建立輔助函式
- 配置路由
- 設定路由忽略
首先我們新建一個Laravel專案,配置好資料庫引數,引入Easywechat擴充套件(在此感謝安正超)
然後建立資料表:
php artisan make:model Wechat -m
來在生成的wechats的migration檔案裡寫入微信相關資料欄位:
public function up()
{
Schema::create('wechats', function (Blueprint $table) {
$table->increments('id');
$table->string('aid')->nullable();
$table->string('wechat_app_id')->nullable(); //微信公眾號設定引數
$table->string('wechat_secret')->nullable();
$table->string('wechat_token')->nullable();
$table->string('wechat_aes_key')->nullable();
$table->string('pay_mch_id')->nullable(); //微信支付設定引數
$table->string('pay_api_key')->nullable();
$table->string('pay_cert_path')->nullable();
$table->string('pay_key_path')->nullable();
$table->string('op_app_id')->nullable(); //微信開放平臺設定引數
$table->string('op_secret')->nullable();
$table->string('op_token')->nullable();
$table->string('op_aes_key')->nullable();
$table->string('work_corp_id')->nullable(); //微信企業號設定引數
$table->string('work_agent_id')->nullable();
$table->string('work_secret')->nullable();
$table->timestamps();
});
}
接下來在App\Handlers建立一個輔助函式WechatConfigHandler.php檔案,程式碼如下:
<?php
namespace App\Handlers;
use App\Wechat;
use EasyWeChat\Factory;
class WechatConfigHandler
{
//[1-1]微信公眾號設定
public function app_config($account)
{
$wechat = Wechat::where('aid',$account)->first();
if (!$wechat) {
return $config = [];
}
$config = [
'app_id' => $wechat->wechat_app_id, // AppID
'secret' => $wechat->wechat_secret, // AppSecret
'token' => $wechat->wechat_token, // Token
'aes_key' => $wechat->wechat_aes_key, // EncodingAESKey,相容與安全模式下請一定要填寫!!!
'response_type' => 'array',
'oauth' => [
//'scopes' => array_map('trim', explode(',', env('WECHAT_OFFICIAL_ACCOUNT_OAUTH_SCOPES', 'snsapi_userinfo'))),
'scopes' => 'snsapi_userinfo',
//'callback' => env('WECHAT_OFFICIAL_ACCOUNT_OAUTH_CALLBACK', '/oauth_callback'),
'callback' => '/oauth_callback/'.$account,
],
'log' => [
'level' => 'debug',
'file' => storage_path('logs/wechat.log'), //這個必須要有,要不除錯有問題,你都會找不到原因
],
];
return $config;
}
//[1-2]生成微信公眾號相關
public function app($account)
{
$app = Factory::officialAccount($this->app_config($account));
return $app;
}
//[2-1]微信支付設定
public function pay_config($account)
{
$wechat = Wechat::where('aid',$account)->first();
if (!$wechat) {
return $config = [];
}
$config = [
'app_id' => $wechat->wechat_app_id, // AppID
'secret' => $wechat->wechat_secret, // AppSecret
'mch_id' => $wechat->pay_mch_id,
'key' => $wechat->pay_api_key, // API 金鑰
// 如需使用敏感介面(如退款、傳送紅包等)需要配置 API 證照路徑(登入商戶平臺下載 API 證照)
'cert_path' => $wechat->pay_api_key, // XXX: 絕對路徑!!!!
'key_path' => $wechat->pay_api_key, // XXX: 絕對路徑!!!!
'notify_url' => '預設的訂單回撥地址', // 你也可以在下單時單獨設定來想覆蓋它
];
return $config;
}
//[2-2]生成微信支付相關
public function pay($account)
{
$pay = Factory::payment($this->pay_config($account));
return $pay;
}
//[3-1]微信小程式設定
public function mini_config($account)
{
$wechat = Wechat::where('aid',$account)->first();
if (!$wechat) {
return $config = [];
}
$config = [
'app_id' => $wechat->wechat_app_id, // AppID
'secret' => $wechat->wechat_secret, // AppSecret
'response_type' => 'array',
];
return $config;
}
//[3-2]微信小程式相關
public function miniProgram($account)
{
$miniProgram = Factory::miniProgram($this->mini_config($account));
return $miniProgram;
}
//[4-1]微信開放平臺設定引數
public function opconfig($account)
{
$wechat = Wechat::where('aid',$account)->first();
if (!$wechat) {
return $config = [];
}
$config = [
'app_id' => $wechat->op_app_id,
'secret' => $wechat->op_secret,
'token' => $wechat->op_token,
'aes_key' => $wechat->op_aes_key
];
return $config;
}
//[4-2]微信開放平臺相關
public function openPlatform($account)
{
$openPlatform = Factory::openPlatform($this->opconfig($account));
return $openPlatform;
}
//[5-1]微信企業號設定引數
public function workconfig($account)
{
$wechat = Wechat::where('aid',$account)->first();
if (!$wechat) {
return $config = [];
}
$config = [
'corp_id' => $wechat->work_corp_id,
'agent_id' => $wechat->work_agent_id,
'secret' => $wechat->work_secret,
'response_type' => 'array',
];
return $config;
}
//[5-2]微信企業號相關
public function work($account)
{
$work = Factory::work($this->workconfig($account));
return $work;
}
}
建立一個微信控制器,進行微信公眾平臺對接:
php artisan make:controller WechatController
寫入方法以下方法:
protected $wechat;
public function __construct(WechatConfigHandler $wechat)
{
$this->wechat = $wechat;
}
public function serve($account)
{
$app = $this->wechat->app($account);
$app->server->push(function($message){
switch ($message['MsgType']) {
case 'event':
if ($message['Event'] == 'subscribe') {
return '歡迎關注 Johnson!';
}
break;
case 'text':
return '收到文字訊息';
break;
case 'image':
return '收到圖片訊息';
break;
case 'voice':
return '收到語音訊息';
break;
case 'video':
return '收到影片訊息';
break;
case 'location':
return '收到座標訊息';
break;
case 'link':
return '收到連結訊息';
break;
// ... 其它訊息
default:
return '收到其它訊息';
break;
}
});
$response = $app->server->serve();
return $response;
}
public function oauth_callback($account)
{
$app = $this->wechat->app($account);
$user = $app->oauth->user();
session(['wechat.oauth_user' => $user->toArray()]);
//不管在哪個頁面檢測使用者登入狀態,都要寫入session值:target_url
$targetUrl = session()->has('target_url') ? session('target_url') : '/' ;
//header('location:'. $targetUrl);
return redirect()->to($targetUrl);
}
寫到這裡,千萬別忘了,中間鍵Middleware資料夾裡VerifyCsrfToken.php修改路由忽略:
protected $except = [
'wechat/*'
];
然後在使用的時候,可以這樣寫:
$app = $this->wechat->app($account); //公眾號
$app = $this->wechat->pay($account); //微信支付
$app = $this->wechat->miniProgram($account); //微信小程式
$app = $this->wechat->work($account); //企業號應用
微信公眾平臺開發者選項裡要設定為:
其他內容自己配置吧,到這裡就結束了。
哦,別忘了配置路由:
Route::any('wechat/{account}','WechatController@serve');
Route::any('/oauth_callback/{account}','WechatController@oauth_callback');
這裡wechat/1
就是資料庫裡,aid為1的公眾號接入,其他的功能大家可以自行測試,也不知道這樣實現是否符合邏輯,再次感謝安正超。
本作品採用《CC 協議》,轉載必須註明作者和本文連結