Skip to content
This repository has been archived by the owner on Aug 3, 2021. It is now read-only.

自定义授权相关通知

wangyi edited this page Dec 22, 2016 · 1 revision

自定义授权相关通知

授权回调事件的处理

契约

<?php
namespace Chunhei2008\EasyOpenWechat\Contracts;

use Chunhei2008\EasyOpenWechat\Core\Authorization;
use Chunhei2008\EasyOpenWechat\Core\ComponentVerifyTicket;

interface AuthorizeHandlerContract
{
    /**
     * handle component verify ticket
     *
     * @param                       $message
     * @param ComponentVerifyTicket $componentVerifyTicket
     *
     * @return mixed
     */

    public function componentVerifyTicket($message, ComponentVerifyTicket $componentVerifyTicket);

    /**
     * handle authorized
     *
     * @param               $message
     * @param Authorization $authorization
     *
     * @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 Authorization $authorization
     *
     * @return mixed
     */

    public function updateauthorized($message , Authorization $authorization);

}

实现

<?php


namespace Chunhei2008\EasyOpenWechat\Core;


use Chunhei2008\EasyOpenWechat\Contracts\AuthorizeHandlerContract;
use Chunhei2008\EasyOpenWechat\Contracts\AuthorizerRefreshTokenContract;
use Chunhei2008\EasyOpenWechat\Support\Log;

class AuthorizeHandler implements AuthorizeHandlerContract
{

    public function componentVerifyTicket($message, ComponentVerifyTicket $componentVerifyTicket)
    {
        Log::debug('ComponentVerifyTicket event:', $message);
        $componentVerifyTicket->setVerifyTicket($message['ComponentVerifyTicket']);
    }

    public function authorized($message, Authorization $authorization)
    {
        Log::debug('Authorized event:', $message);
        $authorization->setAuthorizationCode($message['AuthorizationCode'])->getAuthorizationInfo();
    }

    public function unauthorized($message, AuthorizerRefreshTokenContract $authorizerRefreshToken)
    {
        Log::debug('Unauthorized event:', $message);
        $authorizerRefreshToken->removeRefreshToken($message['AuthorizerAppid']);
    }

    public function updateauthorized($message, Authorization $authorization)
    {
        Log::debug('Updateauthorized event:', $message);
        $authorization->setAuthorizationCode($message['AuthorizationCode'])->getAuthorizationInfo();
    }
}

对应的服务提供者

<?php

namespace Chunhei2008\EasyOpenWechat\Foundation\ServiceProviders;


use Chunhei2008\EasyOpenWechat\Core\AuthorizeHandler;
use Pimple\Container;
use Pimple\ServiceProviderInterface;

class AuthorizeHandlerCustomerServiceProvider implements ServiceProviderInterface
{
    public function register(Container $pimple)
    {
        $pimple['authorize_handler'] = function ($pimple) {
            return new AuthorizeHandler();
        };
    }

}

使用

$config = [
    'debug'                => true,
    'component_app_id'     => 'wxd954……',                                   //第三方平台app id
    'component_app_secret' => 'a7a48d271…………',                              //第三方平台app secret
    'token'                => 'easy-open-wechat-token',                     //公众号消息校验Token
    'aes_key'              => '90ID6sSTuY……Uh6BWDct',                       //公众号消息加解密Key
    'redirect_uri' => 'http://yourhostname/authcallback.php',               //公众号授权回调页面
    'log' => [
        'level' => 'debug',
        'file'  => '/tmp/easyopenwechat.log',
    ],
];
$providers = [
    AuthorizeHandlerCustomerServiceProvider:class     
];
$app = new \Chunhei2008\EasyOpenWechat\Foundation\Application($config);
//添加服务覆盖refresh_token
$app->addProviders($providers);

Clone this wiki locally