Client.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\OpenPlatform\Base;
  11. use EasyWeChat\Kernel\BaseClient;
  12. /**
  13. * Class Client.
  14. *
  15. * @author mingyoung <mingyoungcheung@gmail.com>
  16. */
  17. class Client extends BaseClient
  18. {
  19. /**
  20. * Get authorization info.
  21. *
  22. * @param string|null $authCode
  23. *
  24. * @return mixed
  25. *
  26. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  27. * @throws \GuzzleHttp\Exception\GuzzleException
  28. */
  29. public function handleAuthorize(string $authCode = null)
  30. {
  31. $params = [
  32. 'component_appid' => $this->app['config']['app_id'],
  33. 'authorization_code' => $authCode ?? $this->app['request']->get('auth_code'),
  34. ];
  35. return $this->httpPostJson('cgi-bin/component/api_query_auth', $params);
  36. }
  37. /**
  38. * Get authorizer info.
  39. *
  40. * @param string $appId
  41. *
  42. * @return mixed
  43. *
  44. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  45. * @throws \GuzzleHttp\Exception\GuzzleException
  46. */
  47. public function getAuthorizer(string $appId)
  48. {
  49. $params = [
  50. 'component_appid' => $this->app['config']['app_id'],
  51. 'authorizer_appid' => $appId,
  52. ];
  53. return $this->httpPostJson('cgi-bin/component/api_get_authorizer_info', $params);
  54. }
  55. /**
  56. * Get options.
  57. *
  58. * @param string $appId
  59. * @param string $name
  60. *
  61. * @return mixed
  62. *
  63. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  64. * @throws \GuzzleHttp\Exception\GuzzleException
  65. */
  66. public function getAuthorizerOption(string $appId, string $name)
  67. {
  68. $params = [
  69. 'component_appid' => $this->app['config']['app_id'],
  70. 'authorizer_appid' => $appId,
  71. 'option_name' => $name,
  72. ];
  73. return $this->httpPostJson('cgi-bin/component/api_get_authorizer_option', $params);
  74. }
  75. /**
  76. * Set authorizer option.
  77. *
  78. * @param string $appId
  79. * @param string $name
  80. * @param string $value
  81. *
  82. * @return mixed
  83. *
  84. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  85. * @throws \GuzzleHttp\Exception\GuzzleException
  86. */
  87. public function setAuthorizerOption(string $appId, string $name, string $value)
  88. {
  89. $params = [
  90. 'component_appid' => $this->app['config']['app_id'],
  91. 'authorizer_appid' => $appId,
  92. 'option_name' => $name,
  93. 'option_value' => $value,
  94. ];
  95. return $this->httpPostJson('cgi-bin/component/api_set_authorizer_option', $params);
  96. }
  97. /**
  98. * Get authorizer list.
  99. *
  100. * @param int $offset
  101. * @param int $count
  102. *
  103. * @return mixed
  104. *
  105. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  106. * @throws \GuzzleHttp\Exception\GuzzleException
  107. */
  108. public function getAuthorizers($offset = 0, $count = 500)
  109. {
  110. $params = [
  111. 'component_appid' => $this->app['config']['app_id'],
  112. 'offset' => $offset,
  113. 'count' => $count,
  114. ];
  115. return $this->httpPostJson('cgi-bin/component/api_get_authorizer_list', $params);
  116. }
  117. /**
  118. * Create pre-authorization code.
  119. *
  120. * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
  121. *
  122. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  123. * @throws \GuzzleHttp\Exception\GuzzleException
  124. */
  125. public function createPreAuthorizationCode()
  126. {
  127. $params = [
  128. 'component_appid' => $this->app['config']['app_id'],
  129. ];
  130. return $this->httpPostJson('cgi-bin/component/api_create_preauthcode', $params);
  131. }
  132. /**
  133. * OpenPlatform Clear quota.
  134. *
  135. * @see https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318587
  136. *
  137. * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
  138. *
  139. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  140. * @throws \GuzzleHttp\Exception\GuzzleException
  141. */
  142. public function clearQuota()
  143. {
  144. $params = [
  145. 'component_appid' => $this->app['config']['app_id'],
  146. ];
  147. return $this->httpPostJson('cgi-bin/component/clear_quota', $params);
  148. }
  149. }