Client.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace EasyWeChat\MiniProgram\AppCode;
  11. use EasyWeChat\Kernel\BaseClient;
  12. use EasyWeChat\Kernel\Http\StreamResponse;
  13. /**
  14. * Class Client.
  15. *
  16. * @author mingyoung <mingyoungcheung@gmail.com>
  17. */
  18. class Client extends BaseClient
  19. {
  20. /**
  21. * Get AppCode.
  22. *
  23. * @param string $path
  24. * @param array $optional
  25. *
  26. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  27. */
  28. public function get(string $path, array $optional = [])
  29. {
  30. $params = array_merge([
  31. 'path' => $path,
  32. ], $optional);
  33. return $this->getStream('wxa/getwxacode', $params);
  34. }
  35. /**
  36. * Get AppCode unlimit.
  37. *
  38. * @param string $scene
  39. * @param array $optional
  40. *
  41. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  42. */
  43. public function getUnlimit(string $scene, array $optional = [])
  44. {
  45. $params = array_merge([
  46. 'scene' => $scene,
  47. ], $optional);
  48. return $this->getStream('wxa/getwxacodeunlimit', $params);
  49. }
  50. /**
  51. * Create QrCode.
  52. *
  53. * @param string $path
  54. * @param int|null $width
  55. *
  56. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  57. */
  58. public function getQrCode(string $path, int $width = null)
  59. {
  60. return $this->getStream('cgi-bin/wxaapp/createwxaqrcode', compact('path', 'width'));
  61. }
  62. /**
  63. * Get stream.
  64. *
  65. * @param string $endpoint
  66. * @param array $params
  67. *
  68. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  69. */
  70. protected function getStream(string $endpoint, array $params)
  71. {
  72. $response = $this->requestRaw($endpoint, 'POST', ['json' => $params]);
  73. if (false !== stripos($response->getHeaderLine('Content-disposition'), 'attachment')) {
  74. return StreamResponse::buildFromPsrResponse($response);
  75. }
  76. return $this->castResponseToType($response, $this->app['config']->get('response_type'));
  77. }
  78. }