Client.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * This file is part of the apiadmin/tiktok.
  4. */
  5. namespace App\Libs\TikTok\MiniProgram\QrCode;
  6. use App\Libs\TikTok\Kernel\BaseClient;
  7. use App\Libs\TikTok\Kernel\Exceptions\HttpException;
  8. use App\Libs\TikTok\Kernel\Exceptions\InvalidConfigException;
  9. use App\Libs\TikTok\Kernel\Http\StreamResponse;
  10. use App\Libs\TikTok\Kernel\Support\Collection;
  11. use GuzzleHttp\Exception\GuzzleException;
  12. use Psr\Http\Message\ResponseInterface;
  13. /**
  14. *
  15. */
  16. class Client extends BaseClient {
  17. /**
  18. * Get QrCode unLimit.【获取抖音小程序码,目前只支持永久码】
  19. * @param string $appName
  20. * @param array $optional
  21. * @return array|Collection|object|ResponseInterface|string
  22. * @throws GuzzleException
  23. * @throws HttpException
  24. * @throws InvalidConfigException
  25. * @author zhaoxiang <zhaoxiang051405@gmail.com>
  26. */
  27. public function getUnLimit(string $appName, array $optional = []) {
  28. if (!empty($optional['path'])) {
  29. $optional['path'] = urlencode($optional['path']);
  30. }
  31. $params = array_merge([
  32. 'set_icon' => true,
  33. 'appname' => $appName
  34. ], $optional);
  35. return $this->getStream('apps/qrcode', $params);
  36. }
  37. /**
  38. * Get stream.
  39. *
  40. * @param string $endpoint
  41. * @param array $params
  42. *
  43. * @return array|Collection|object|ResponseInterface|string
  44. *
  45. * @throws InvalidConfigException
  46. * @throws GuzzleException|HttpException
  47. */
  48. protected function getStream(string $endpoint, array $params) {
  49. $response = $this->requestRaw($endpoint, 'POST', ['json' => $params]);
  50. if (false === stripos($response->getHeaderLine('Content-Type'), 'application/json')) {
  51. return StreamResponse::buildFromPsrResponse($response);
  52. }
  53. return $this->castResponseToType($response, $this->app['config']->get('response_type'));
  54. }
  55. }