laravel使用EasyWeChat 授權登入

helong3314發表於2021-06-01

laravel安裝laravel-wechat 實現微信授權登入

一.下載laravel-wechat

https://packagist.org/packages/overtrue/laravel-wechat

二. 使用composer安裝laravel-wechat

composer require "overtrue/laravel-wechat:~4.0"

三.配置

在 config/app.php 註冊 ServiceProvider 和 Facade (Laravel 5.5 + 無需手動註冊)

'providers' => [
    // ...
    Overtrue\LaravelWeChat\ServiceProvider::class,
],
'aliases' => [
    // ...
    'EasyWeChat' => Overtrue\LaravelWeChat\Facade::class,
],

建立配置檔案:

php artisan vendor:publish --provider="Overtrue\LaravelWeChat\ServiceProvider

1.修改應用根目錄下的 config/wechat.php 中對應的引數即可。

WECHAT_LOG_LEVEL=info  //日誌級別 最好和laravel同級
WECHAT_LOG_FILE=/var/log/coupon/report.log  //日誌儲存檔案 最好和laravel 統一
WECHAT_OFFICIAL_ACCOUNT_APPID=申請的appid
WECHAT_OFFICIAL_ACCOUNT_SECRET=申請的appsecret
WECHAT_OFFICIAL_ACCOUNT_TOKEN=
WECHAT_OFFICIAL_ACCOUNT_AES_KEY=
<?php

namespace Modules\Easywechat\Http\Controllers;

use EasyWeChat\Factory;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class EasywechatController extends Controller
{
  public function serve(Request $request){
  $config = [
      'app_id' => env('WECHAT_OFFICIAL_ACCOUNT_APPID'),
      'secret' => env('WECHAT_OFFICIAL_ACCOUNT_SECRET'),
      'token' => env('WECHAT_OFFICIAL_ACCOUNT_TOKEN'),
      // 指定 API 呼叫返回結果的型別:array(default)/collection/object/raw/自定義類名
      'response_type' => 'array',

      'log' => [
      'level' => 'debug',
      ],
  ];
  //從wechat.php 配置檔案獲取
  $config = config('wechat.official_account.default');
  $app = Factory::officialAccount($config);
  $response = $app->server->serve();
  $response->send(); // Laravel 裡請使用:return $response;
  }
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章