SDK ini dikembangkan berdasarkan EasyWechat. Untuk memfasilitasi pengembangan platform pihak ketiga untuk akun publik WeChat, SDK ini merangkum pengembangan platform pihak ketiga yang disahkan oleh akun publik WeChat, serta akses rilis seluruh jaringan otomatis fungsi telah diuji oleh platform akun publik yang ada. Versi resmi 1.0.0 sekarang telah dirilis. Jika Anda mengalami masalah dalam menggunakan SDK ini, silakan hubungi saya. Jika menurut Anda proyek ini bermanfaat bagi Anda, silakan beri bintang dan biarkan saya tahu. Terima kasih.
composer require chunhei2008/easy-open-wechat
wiki github
Kecuali bidang konfigurasi terkait platform pihak ketiga yang baru ditambahkan, bidang lainnya masih merupakan bidang konfigurasi EasyWechat.
$config = [
'debug' => false, //是否调试模式
'component_app_id' => 'component app id', //第三方公众平台app id
'component_app_secret' => 'component app secret', //第三方公众平台app secret
'token' => 'token', //公众号消息校验Token
'aes_key' => 'aea key', //公众号消息加解密Key
'redirect_uri' => 'auth callback uri', //授权回调页面URI
'log' => [ //日志
'level' => 'debug',
'file' => '/tmp/easyopenwechat.log',
],
];
Platform pihak ketiga dapat menempatkan pintu masuk "Otorisasi Akun Resmi WeChat" di situs web mereka untuk memandu operator akun publik ke halaman otorisasi. URL halaman otorisasi adalah https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=xxxx&pre_auth_code=xxxxx&redirect_uri=xxxx. Di URL ini, pihak platform pihak ketiga harus menyediakan pihak platform pihak ketiga appid, kode pra-otorisasi dan URI
<?php
/**
* auth.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/18 09:18
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
include "./vendor/autoload.php";
$config = [
'debug' => true,
'component_app_id' => '第三方平台app id',
'component_app_secret' => '第三方平台app secret',
'token' => '公众号消息校验Token
',
'aes_key' => '公众号消息加解密Key
',
'redirect_uri' => '授权回调页面url',
'log' => [
'level' => 'debug',
'file' => '/tmp/easyopenwechat.log',
],
];
$app = new Chunhei2008EasyOpenWechatFoundationApplication($config);
$page = $app->login->getLoginPage();
echo "<a href="$page">auth</a>";
Setelah proses otorisasi selesai, halaman otorisasi akan secara otomatis beralih ke URI panggilan balik, dan mengembalikan kode otorisasi dan waktu kedaluwarsa di parameter URL (redirect_url?auth_code=xxx&expires_in=600); platform dapat menggunakan kode otorisasi Sebagai imbalan atas kredensial panggilan antarmuka dari akun resmi yang diotorisasi (authorizer_a ccess_token, juga disebut sebagai token), lalu panggil kredensial melalui antarmuka ini, dan ikuti instruksi dalam dokumentasi pengembang akun resmi (mp.weixin.qq.com/wiki) untuk memanggil API terkait akun resmi (API mana yang dapat dipanggil tergantung pada Kumpulan izin mana yang diotorisasi oleh pengguna ke platform pihak ketiga juga bergantung pada izin antarmuka mana yang dimiliki akun resmi itu sendiri), menggunakan JS SDK dan kemampuan lainnya. Untuk detailnya, silakan lihat [Deskripsi Antarmuka Platform Pihak Ketiga Akun Resmi]
<?php
/**
* authcallback.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/18 09:55
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
include "./vendor/autoload.php";
$config = [
'debug' => true,
'component_app_id' => '第三方平台app id',
'component_app_secret' => '第三方平台app secret',
'token' => '公众号消息校验Token
',
'aes_key' => '公众号消息加解密Key
',
'redirect_uri' => '授权回调页面url',
'log' => [
'level' => 'debug',
'file' => '/tmp/easyopenwechat.log',
],
];
// 回调页面带回的授权码
$auth_code = $_GET['auth_code'];
$app = new Chunhei2008EasyOpenWechatFoundationApplication($config);
// 使用授权码获取授权公众号的信息,并且自动保存公众号的refresh_token等
$auth_info = $app->authorization->setAuthorizationCode($auth_code)->getAuthorizationInfo();
var_dump($auth_info);
Respons terhadap semua peristiwa otorisasi dalam proses otorisasi, termasuk respons pemantauan rilis di seluruh jaringan
<?php
include "./vendor/autoload.php";
$config = [
'debug' => true,
'component_app_id' => '第三方平台app id',
'component_app_secret' => '第三方平台app secret',
'token' => '公众号消息校验Token
',
'aes_key' => '公众号消息加解密Key
',
'redirect_uri' => '授权回调页面url',
'log' => [
'level' => 'debug',
'file' => '/tmp/easyopenwechat.log',
],
];
$app = new Chunhei2008EasyOpenWechatFoundationApplication($config);
$app->auth->handle()->send();
<?php
/**
* message.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/18 09:13
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
include "./vendor/autoload.php";
$config = [
'debug' => true,
'component_app_id' => '第三方平台app id',
'component_app_secret' => '第三方平台app secret',
'token' => '公众号消息校验Token
',
'aes_key' => '公众号消息加解密Key
',
'redirect_uri' => '授权回调页面url',
'log' => [
'level' => 'debug',
'file' => '/tmp/easyopenwechat.log',
],
];
//公众号消息与事件接收URL中带的$APPID$
$app_id = $_GET['app_id'];
$config['app_id'] = $app_id;
$app = new Chunhei2008EasyOpenWechatFoundationApplication($config);
//获取easywechat的app对象
$wechat = $app->wechat;
//余下的和EasyWechat开发一模一样
$response = $wechat->server->setMessageHandler(function ($message) {
return "您好!欢迎关注我! this is easy open wechat";
})->serve();
$response->send();
SDK menggunakan Cache secara default untuk menyimpan cache dan informasi penting dari akun publik. Namun, penyimpanan data informasi penting seperti authorizer_appid, authorizer_refresh_token, dll. paling baik disimpan di tempat penyimpanan persisten seperti database pertimbangan.
Implementasi defaultnya menggunakan Cache untuk penyimpanan
<?php
/**
* AuthorizerRefreshToken.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/17 11:44
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
namespace Chunhei2008EasyOpenWechatCore;
use Chunhei2008EasyOpenWechatContractsAuthorizerRefreshTokenContract;
use Chunhei2008EasyOpenWechatSupportLog;
use Chunhei2008EasyOpenWechatTraitsCacheTrait;
use DoctrineCommonCacheCache;
class AuthorizerRefreshToken implements AuthorizerRefreshTokenContract
{
use CacheTrait;
/**
* app id
*
* @var string
*/
protected $authorizerAppId = '';
/**
* cache key prefix
*/
const AUTHORIZER_REFRESH_TOKEN_CACHE_PREFIX = 'easyopenwechat.core.refresh_token.';
public function __construct(Cache $cache = null)
{
$this->cache = $cache;
$this->setCacheKeyField('authorizerAppId');
$this->setPrefix(static::AUTHORIZER_REFRESH_TOKEN_CACHE_PREFIX);
}
/**
*
* get refresh token
*
* @param $authorizerAppId
*
* @return mixed|string
*/
public function getRefreshToken($authorizerAppId)
{
$this->setAuthorizerAppId($authorizerAppId);
$cacheKey = $this->getCacheKey();
$authorizerRefreshToken = $this->getCache()->fetch($cacheKey);
Log::debug('Get refresh token from cache:', [$authorizerAppId, $authorizerRefreshToken]);
return $authorizerRefreshToken;
}
/**
* set refresh token
*
* @param $authorizerAppId
* @param $authorizerRefreshToken
*/
public function setRefreshToken($authorizerAppId, $authorizerRefreshToken)
{
$this->setAuthorizerAppId($authorizerAppId);
$cacheKey = $this->getCacheKey();
$this->getCache()->save($cacheKey, $authorizerRefreshToken);
Log::debug('Set refresh token:', [$authorizerAppId, $authorizerRefreshToken]);
}
/**
*
* remove refresh token
*
* @param $authorizerAppId
*/
public function removeRefreshToken($authorizerAppId)
{
$this->setAuthorizerAppId($authorizerAppId);
$cacheKey = $this->getCacheKey();
$this->getCache()->delete($cacheKey);
Log::debug('Remove refresh token:', [$authorizerAppId]);
}
/**
* set authorizer app id
*
* @param $authorizerAppId
*/
private function setAuthorizerAppId($authorizerAppId)
{
$this->authorizerAppId = $authorizerAppId;
}
}
<?php
/**
* AuthorizerRefreshToken.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/17 11:44
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
namespace Chunhei2008EasyOpenWechatCore;
use Chunhei2008EasyOpenWechatContractsAuthorizerRefreshTokenContract;
use Chunhei2008EasyOpenWechatSupportLog;
use Chunhei2008EasyOpenWechatTraitsCacheTrait;
use DoctrineCommonCacheCache;
class AuthorizerRefreshTokenDB implements AuthorizerRefreshTokenContract
{
/**
*
* get refresh token
*
* @param $authorizerAppId
*
* @return mixed|string
*/
public function getRefreshToken($authorizerAppId)
{
// get refresh token by app id from db
Log::debug('Get refresh token from cache:', [$authorizerAppId, $authorizerRefreshToken]);
return $authorizerRefreshToken;
}
/**
* set refresh token
*
* @param $authorizerAppId
* @param $authorizerRefreshToken
*/
public function setRefreshToken($authorizerAppId, $authorizerRefreshToken)
{
// save refresh token to db by app id
Log::debug('Set refresh token:', [$authorizerAppId, $authorizerRefreshToken]);
}
/**
*
* remove refresh token
*
* @param $authorizerAppId
*/
public function removeRefreshToken($authorizerAppId)
{
// delete refresh token from db by app id
Log::debug('Remove refresh token:', [$authorizerAppId]);
}
}
<?php
/**
* AuthorizerRefreshTokenDefaultProvider.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/17 11:50
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
namespace Chunhei2008EasyOpenWechatFoundationServiceProviders;
use Chunhei2008EasyOpenWechatCoreAuthorizerRefreshTokenDB;
use PimpleContainer;
use PimpleServiceProviderInterface;
class AuthorizerRefreshTokenDBProvider implements ServiceProviderInterface
{
public function register(Container $pimple)
{
//覆盖refresh token
$pimple['authorizer_refresh_token'] = function ($pimple) {
return new AuthorizerRefreshTokenDB(
$pimple['db']
);
};
}
}
<?php
/**
* message.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/18 09:13
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
include "./vendor/autoload.php";
$config = [
];
$app = new Chunhei2008EasyOpenWechatFoundationApplication($config);
$providers = [
AuthorizerRefreshTokenDBProvider::class
];
$app->addProviders($providers);
Tangani respons terhadap peristiwa otorisasi dan proses data terkait setelah otorisasi
<?php
namespace Chunhei2008EasyOpenWechatContracts;
use Chunhei2008EasyOpenWechatCoreAuthorization;
use Chunhei2008EasyOpenWechatCoreAuthorizationInfo;
use Chunhei2008EasyOpenWechatCoreComponentVerifyTicket;
/**
* AuthPushContract.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/16 09:25
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
interface AuthorizeHandlerContract
{
/**
* handle component verify ticket
*
* @param $message
* @param ComponentVerifyTicket $componentVerifyTicket
*
* @return mixed
*/
public function componentVerifyTicket($message, ComponentVerifyTicket $componentVerifyTicket);
/**
* handle authorized
*
* @param $message
* @param AuthorizationInfo $authorizationInfo
*
* @return mixed
*/
public function authorized($message, Authorization $authorization);
/**
* handle unauthorized
*
* @param $message
*
* @return mixed
*/
public function unauthorized($message, AuthorizerRefreshTokenContract $authorizerRefreshToken);
/**
*
* handle updateauthorized
*
* @param $message
* @param AuthorizationInfo $authorizationInfo
*
* @return mixed
*/
public function updateauthorized($message , Authorization $authorization);
}
<?php
/**
* AuthorizeHandlerServiceProvider.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/17 16:34
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
namespace Chunhei2008EasyOpenWechatFoundationServiceProviders;
use Chunhei2008EasyOpenWechatCoreAuthorizeHandler;
use PimpleContainer;
use PimpleServiceProviderInterface;
class AuthorizeHandlerCustomerServiceProvider implements ServiceProviderInterface
{
public function register(Container $pimple)
{
//覆盖
$pimple['authorize_handler'] = function ($pimple) {
return new AuthorizeHandlerCustomer();
};
}
}
<?php
/**
* message.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/18 09:13
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
include "./vendor/autoload.php";
$config = [
];
$app = new Chunhei2008EasyOpenWechatFoundationApplication($config);
//添加到容器
$providers = [
AuthorizeHandlerCustomerServiceProvider::class
];
$app->addProviders($providers);