123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- namespace SocialiteProviders\Weixin;
- use SocialiteProviders\Manager\OAuth2\AbstractProvider;
- use SocialiteProviders\Manager\OAuth2\User;
- class Provider extends AbstractProvider
- {
- /**
- * Unique Provider Identifier.
- */
- public const IDENTIFIER = 'WEIXIN';
- /**
- * @var string
- */
- protected $openId;
- /**
- * {@inheritdoc}.
- */
- protected $scopes = ['snsapi_userinfo'];
- /**
- * set Open Id.
- *
- * @param string $openId
- */
- public function setOpenId($openId)
- {
- $this->openId = $openId;
- }
- /**
- * {@inheritdoc}.
- */
- protected function getAuthUrl($state)
- {
- return $this->buildAuthUrlFromBase('https://open.weixin.qq.com/connect/oauth2/authorize', $state);
- }
- /**
- * {@inheritdoc}.
- */
- protected function buildAuthUrlFromBase($url, $state)
- {
- $query = http_build_query($this->getCodeFields($state), '', '&', $this->encodingType);
- return $url.'?'.$query.'#wechat_redirect';
- }
- /**
- * {@inheritdoc}.
- */
- protected function getCodeFields($state = null)
- {
- return [
- 'appid' => $this->clientId, 'redirect_uri' => $this->redirectUrl,
- 'response_type' => 'code',
- 'scope' => $this->formatScopes($this->scopes, $this->scopeSeparator),
- 'state' => $state,
- ];
- }
- /**
- * {@inheritdoc}.
- */
- protected function getTokenUrl()
- {
- return 'https://api.weixin.qq.com/sns/oauth2/access_token';
- }
- /**
- * {@inheritdoc}.
- */
- protected function getUserByToken($token)
- {
- if (in_array('snsapi_base', $this->scopes, true)) {
- $user = ['openid' => $this->openId];
- } else {
- $response = $this->getHttpClient()->get('https://api.weixin.qq.com/sns/userinfo', [
- 'query' => [
- 'access_token' => $token,
- 'openid' => $this->openId,
- 'lang' => 'zh_CN',
- ],
- ]);
- $user = json_decode($response->getBody(), true);
- }
- return $user;
- }
- /**
- * {@inheritdoc}.
- */
- protected function mapUserToObject(array $user)
- {
- return (new User())->setRaw($user)->map([
- 'id' => $user['openid'],
- 'unionid' => isset($user['unionid']) ? $user['unionid'] : null,
- 'nickname' => isset($user['nickname']) ? $user['nickname'] : null,
- 'avatar' => isset($user['headimgurl']) ? $user['headimgurl'] : null,
- 'name' => null,
- 'email' => null,
- ]);
- }
- /**
- * {@inheritdoc}.
- */
- protected function getTokenFields($code)
- {
- return [
- 'appid' => $this->clientId, 'secret' => $this->clientSecret,
- 'code' => $code, 'grant_type' => 'authorization_code',
- ];
- }
- /**
- * {@inheritdoc}.
- */
- public function getAccessTokenResponse($code)
- {
- $response = $this->getHttpClient()->get($this->getTokenUrl(), [
- 'query' => $this->getTokenFields($code),
- ]);
- $this->credentialsResponseBody = json_decode($response->getBody(), true);
- if (isset($this->credentialsResponseBody['openid'])) {
- $this->openId = $this->credentialsResponseBody['openid'];
- }
- return $this->credentialsResponseBody;
- }
- }
|