-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters