Skip to content

Commit

Permalink
add: miniprogram app code client
Browse files Browse the repository at this point in the history
  • Loading branch information
Player626 committed Mar 9, 2021
1 parent 9e4ace5 commit f27c757
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
77 changes: 77 additions & 0 deletions src/MiniProgram/AppCode/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php


namespace EasySwoole\WeChat\MiniProgram\AppCode;


use EasySwoole\WeChat\Kernel\Contracts\StreamResponseInterface;
use EasySwoole\WeChat\Kernel\Psr\StreamResponse;
use EasySwoole\WeChat\Kernel\ServiceProviders;
use EasySwoole\WeChat\MiniProgram\BaseClient;

class Client extends BaseClient
{
/**
* @param string $path
* @param array $optional
* @return StreamResponseInterface
* @throws \EasySwoole\WeChat\Kernel\Exceptions\HttpException
*/
public function get(string $path, array $optional = [])
{
$params = array_merge([
'path' => $path,
], $optional);

return $this->getStream('/wxa/getwxacode', $params);
}

/**
* @param string $scene
* @param array $optional
* @return StreamResponseInterface
* @throws \EasySwoole\WeChat\Kernel\Exceptions\HttpException
*/
public function getUnLimit(string $scene, array $optional = [])
{
$params = array_merge([
'scene' => $scene,
], $optional);

return $this->getStream('/wxa/getwxacodeunlimit', $params);
}

/**
* @param string $path
* @param int|null $width
* @return StreamResponseInterface
* @throws \EasySwoole\WeChat\Kernel\Exceptions\HttpException
*/
public function getQrCode(string $path, int $width = null)
{
return $this->getStream('/cgi-bin/wxaapp/createwxaqrcode', compact('path', 'width'));
}

/**
* @param string $endpoint
* @param array $params
* @return StreamResponse
* @throws \EasySwoole\WeChat\Kernel\Exceptions\HttpException
*/
protected function getStream(string $endpoint, array $params)
{
$response = $this->getClient()
->setMethod('POST')
->setBody($this->jsonDataToStream($params))
->send($this->buildUrl(
$endpoint,
['access_token' => $this->app[ServiceProviders::AccessToken]->getToken()])
);

if (false !== stripos($response->getHeaderLine('Content-disposition'), 'attachment')) {
return new StreamResponse($response->getBody());
}

$this->checkResponse($response);
}
}
21 changes: 21 additions & 0 deletions src/MiniProgram/AppCode/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace EasySwoole\WeChat\MiniProgram\AppCode;

use EasySwoole\WeChat\MiniProgram\Application;
use Pimple\Container;
use Pimple\ServiceProviderInterface;

/**
* Class ServiceProvider
* @package EasySwoole\WeChat\MiniProgram\AppCode
*/
class ServiceProvider implements ServiceProviderInterface
{
public function register(Container $app)
{
$app[Application::AppCode] = function ($app) {
return new Client($app);
};
}
}
5 changes: 4 additions & 1 deletion src/MiniProgram/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
* @package EasySwoole\WeChat\MiniProgram
* @property Auth\AccessToken $accessToken
* @property Auth\Client $auth
* @property AppCode\Client $appCode
*/
class Application extends ServiceContainer
{
const Auth = 'auth';
const AppCode = 'appCode';

protected $providers = [
Auth\ServiceProvider::class
Auth\ServiceProvider::class,
AppCode\ServiceProvider::class
];
}

0 comments on commit f27c757

Please sign in to comment.