WeChatOpenPlatformProvider.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /*
  3. * This file is part of the overtrue/socialite.
  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 Overtrue\Socialite\Providers;
  11. use Symfony\Component\HttpFoundation\Request;
  12. /**
  13. * Class WeChatProvider.
  14. *
  15. * @see https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318590&token=&lang=zh_CN [WeChat - 公众开放平台代公众号 OAuth 文档]
  16. */
  17. class WeChatOpenPlatformProvider extends WeChatProvider
  18. {
  19. /**
  20. * Component AppId.
  21. *
  22. * @deprecated 2.0 Will be removed in the future
  23. *
  24. * @var string
  25. */
  26. protected $componentAppId;
  27. /**
  28. * Component Access Token.
  29. *
  30. * @deprecated 2.0 Will be removed in the future
  31. *
  32. * @var string
  33. */
  34. protected $componentAccessToken;
  35. /**
  36. * @var \EasyWeChat\OpenPlatform\AccessToken|array
  37. */
  38. protected $credentials;
  39. /**
  40. * {@inheritdoc}.
  41. */
  42. protected $scopes = ['snsapi_base'];
  43. /**
  44. * Create a new provider instance.
  45. * (Overriding).
  46. *
  47. * @param \Symfony\Component\HttpFoundation\Request $request
  48. * @param string $clientId
  49. * @param \EasyWeChat\OpenPlatform\AccessToken|array $credentials
  50. * @param string|null $redirectUrl
  51. */
  52. public function __construct(Request $request, $clientId, $credentials, $redirectUrl = null)
  53. {
  54. parent::__construct($request, $clientId, null, $redirectUrl);
  55. $this->credentials = $credentials;
  56. if (is_array($credentials)) {
  57. list($this->componentAppId, $this->componentAccessToken) = $credentials;
  58. }
  59. }
  60. /**
  61. * {@inheritdoc}.
  62. */
  63. public function getCodeFields($state = null)
  64. {
  65. $this->with(['component_appid' => $this->componentAppId()]);
  66. return parent::getCodeFields($state);
  67. }
  68. /**
  69. * {@inheritdoc}.
  70. */
  71. protected function getTokenUrl()
  72. {
  73. return $this->baseUrl.'/oauth2/component/access_token';
  74. }
  75. /**
  76. * {@inheritdoc}.
  77. */
  78. protected function getTokenFields($code)
  79. {
  80. return [
  81. 'appid' => $this->clientId,
  82. 'component_appid' => $this->componentAppId(),
  83. 'component_access_token' => $this->componentAccessToken(),
  84. 'code' => $code,
  85. 'grant_type' => 'authorization_code',
  86. ];
  87. }
  88. /**
  89. * Get component app id.
  90. *
  91. * @return string
  92. */
  93. protected function componentAppId()
  94. {
  95. return $this->componentAppId ?: $this->credentials->getAppId();
  96. }
  97. /**
  98. * Get component access token.
  99. *
  100. * @return string
  101. */
  102. protected function componentAccessToken()
  103. {
  104. return $this->componentAccessToken ?: $this->credentials->getToken();
  105. }
  106. }