Provider.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace SocialiteProviders\Weixin;
  3. use SocialiteProviders\Manager\OAuth2\AbstractProvider;
  4. use SocialiteProviders\Manager\OAuth2\User;
  5. class Provider extends AbstractProvider
  6. {
  7. /**
  8. * Unique Provider Identifier.
  9. */
  10. public const IDENTIFIER = 'WEIXIN';
  11. /**
  12. * @var string
  13. */
  14. protected $openId;
  15. /**
  16. * {@inheritdoc}.
  17. */
  18. protected $scopes = ['snsapi_userinfo'];
  19. /**
  20. * set Open Id.
  21. *
  22. * @param string $openId
  23. */
  24. public function setOpenId($openId)
  25. {
  26. $this->openId = $openId;
  27. }
  28. /**
  29. * {@inheritdoc}.
  30. */
  31. protected function getAuthUrl($state)
  32. {
  33. return $this->buildAuthUrlFromBase('https://open.weixin.qq.com/connect/oauth2/authorize', $state);
  34. }
  35. /**
  36. * {@inheritdoc}.
  37. */
  38. protected function buildAuthUrlFromBase($url, $state)
  39. {
  40. $query = http_build_query($this->getCodeFields($state), '', '&', $this->encodingType);
  41. return $url.'?'.$query.'#wechat_redirect';
  42. }
  43. /**
  44. * {@inheritdoc}.
  45. */
  46. protected function getCodeFields($state = null)
  47. {
  48. return [
  49. 'appid' => $this->clientId, 'redirect_uri' => $this->redirectUrl,
  50. 'response_type' => 'code',
  51. 'scope' => $this->formatScopes($this->scopes, $this->scopeSeparator),
  52. 'state' => $state,
  53. ];
  54. }
  55. /**
  56. * {@inheritdoc}.
  57. */
  58. protected function getTokenUrl()
  59. {
  60. return 'https://api.weixin.qq.com/sns/oauth2/access_token';
  61. }
  62. /**
  63. * {@inheritdoc}.
  64. */
  65. protected function getUserByToken($token)
  66. {
  67. if (in_array('snsapi_base', $this->scopes, true)) {
  68. $user = ['openid' => $this->openId];
  69. } else {
  70. $response = $this->getHttpClient()->get('https://api.weixin.qq.com/sns/userinfo', [
  71. 'query' => [
  72. 'access_token' => $token,
  73. 'openid' => $this->openId,
  74. 'lang' => 'zh_CN',
  75. ],
  76. ]);
  77. $user = json_decode($response->getBody(), true);
  78. }
  79. return $user;
  80. }
  81. /**
  82. * {@inheritdoc}.
  83. */
  84. protected function mapUserToObject(array $user)
  85. {
  86. return (new User())->setRaw($user)->map([
  87. 'id' => $user['openid'],
  88. 'unionid' => isset($user['unionid']) ? $user['unionid'] : null,
  89. 'nickname' => isset($user['nickname']) ? $user['nickname'] : null,
  90. 'avatar' => isset($user['headimgurl']) ? $user['headimgurl'] : null,
  91. 'name' => null,
  92. 'email' => null,
  93. ]);
  94. }
  95. /**
  96. * {@inheritdoc}.
  97. */
  98. protected function getTokenFields($code)
  99. {
  100. return [
  101. 'appid' => $this->clientId, 'secret' => $this->clientSecret,
  102. 'code' => $code, 'grant_type' => 'authorization_code',
  103. ];
  104. }
  105. /**
  106. * {@inheritdoc}.
  107. */
  108. public function getAccessTokenResponse($code)
  109. {
  110. $response = $this->getHttpClient()->get($this->getTokenUrl(), [
  111. 'query' => $this->getTokenFields($code),
  112. ]);
  113. $this->credentialsResponseBody = json_decode($response->getBody(), true);
  114. if (isset($this->credentialsResponseBody['openid'])) {
  115. $this->openId = $this->credentialsResponseBody['openid'];
  116. }
  117. return $this->credentialsResponseBody;
  118. }
  119. }