hyperf 接入 EasyWechat 多商户配置 (公众号,小程序)

hyperf接入EasyWechat

安装 EasyWechat组件

1. composer require overtrue/easywechat
2.APP\Factory目录新建立一个WechatFactory.php(见配置)
3. 在config/autoload里新加一个wechat.php(见配置)

配置

###wechat.php 以小程序为例

多小程序配置把各自的小程序账号分别存入进去

WechatFactory.php 代码贴图有点长直接贴代码吧

<?php

declare(strict_types = 1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

namespace App\Factory;

use App\Exception\InputException;
use App\Exception\InvalidArgumentException;
use EasyWeChat\Factory;
use EasyWeChat\MiniProgram\Application;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Guzzle\CoroutineHandler;
use Hyperf\Guzzle\HandlerStackFactory;
use Hyperf\HttpServer\Contract\RequestInterface;
use Overtrue\Socialite\Providers\AbstractProvider;
use Psr\Container\ContainerInterface;
use Psr\SimpleCache\CacheInterface;

/**
 * Class WechatFactory
 * @package App\Factory
 * @property \EasyWeChat\MiniProgram\Auth\AccessToken           $access_token
 * @property \EasyWeChat\MiniProgram\DataCube\Client            $data_cube
 * @property \EasyWeChat\MiniProgram\AppCode\Client             $app_code
 * @property \EasyWeChat\MiniProgram\Auth\Client                $auth
 * @property \EasyWeChat\OfficialAccount\Server\Guard           $server
 * @property \EasyWeChat\MiniProgram\Encryptor                  $encryptor
 * @property \EasyWeChat\MiniProgram\TemplateMessage\Client     $template_message
 * @property \EasyWeChat\OfficialAccount\CustomerService\Client $customer_service
 * @property \EasyWeChat\MiniProgram\Plugin\Client              $plugin
 * @property \EasyWeChat\MiniProgram\Plugin\DevClient           $plugin_dev
 * @property \EasyWeChat\MiniProgram\UniformMessage\Client      $uniform_message
 * @property \EasyWeChat\MiniProgram\ActivityMessage\Client     $activity_message
 * @property \EasyWeChat\MiniProgram\Express\Client             $logistics
 * @property \EasyWeChat\MiniProgram\NearbyPoi\Client           $nearby_poi
 * @property \EasyWeChat\MiniProgram\OCR\Client                 $ocr
 * @property \EasyWeChat\MiniProgram\Soter\Client               $soter
 * @property \EasyWeChat\BasicService\Media\Client              $media
 * @property \EasyWeChat\BasicService\ContentSecurity\Client    $content_security
 * @property \EasyWeChat\MiniProgram\Mall\ForwardsMall          $mall
 * @property \EasyWeChat\MiniProgram\SubscribeMessage\Client    $subscribe_message
 * @property \EasyWeChat\MiniProgram\RealtimeLog\Client         $realtime_log
 * @property \EasyWeChat\MiniProgram\Search\Client              $search
 * @property \EasyWeChat\MiniProgram\Live\Client                $live
 */
class WechatFactory
{
    private $container;
    /**
     * @var Application
     */
    private $app;

    /**
     * @param \Psr\Container\ContainerInterface $container
     */
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
        AbstractProvider::setGuzzleOptions([
            'http_errors' => false,
            'handler'     => HandlerStack::create(new CoroutineHandler()),
        ]);
    }

    /**
     * 注册小程序APP
     */
    private function register()
    {
        //此参数主要是区别不同配置的小程序
        $channel = $this->container->get(RequestInterface::class)->input('channel','xxx');
        $key     = 'wechat.mini_program' . ".{$channel}";
        $config  = $this->container->get(ConfigInterface::class)->get($key);
        //自定义错误
        if (empty($config)) {
            throw new InputException('通道来源错误,暂无该配置!', 400);
        }
        $app = Factory::miniProgram($config);

        $config            = $app['config']->get('http', []);
        $config['handler'] = $this->container->get(HandlerStackFactory::class)->create();
        $app->rebind('http_client', new Client($config));
        $cache = $this->container->get(CacheInterface::class);
        $app->rebind('cache', $cache);
        $app['guzzle_handler'] = $this->container->get(HandlerStackFactory::class)->create();
        $this->app             = $app;
    }

   /**
     * @param $id
     *
     * @return mixed
     */
    public function __get($id)
    {
        $this->register();
        if ($this->app->shouldDelegate($id)) {
            return $this->app->delegateTo($id);
        }
        return $this->app->offsetGet($id);
    }
}

如何调用

<?php
declare(strict_types = 1);

namespace App\Controller;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;

/**
 * Class TestController
 * @Controller(prefix="wechat")
 */
class WechatController extends AbstractController
{
    /**
     * @\Hyperf\Di\Annotation\Inject()
     * @var \App\Factory\WechatFactory
     */
    protected $wechatFactory;
    /**
     * @RequestMapping(path="session", methods="get,post")
     */
    public function session()
    {
        $code    = $this->request->input('code');
        $session = $this->wechatFactory->auth->session($code);
        dump($session);
    }
}

效果

本作品采用《CC 协议》,转载必须注明作者和本文链接
CodingHePing
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!