From 2c95b496de2092545b13a8a0f56b37fb499e1b64 Mon Sep 17 00:00:00 2001 From: lpf <2563526478@qq.com> Date: Fri, 27 Sep 2024 14:30:00 +0800 Subject: [PATCH] fix bug --- src/Providers/WeChat.php | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Providers/WeChat.php b/src/Providers/WeChat.php index f7a0ba8..4699ce7 100644 --- a/src/Providers/WeChat.php +++ b/src/Providers/WeChat.php @@ -87,7 +87,7 @@ protected function buildAuthUrlFromBase(string $url): string { $query = \http_build_query($this->getCodeFields(), '', '&', $this->encodingType); - return $url.'?'.$query.'#wechat_redirect'; + return $url . '?' . $query . '#wechat_redirect'; } protected function getCodeFields(): array @@ -108,13 +108,13 @@ protected function getCodeFields(): array protected function getTokenUrl(): string { - return \sprintf($this->baseUrl.'/oauth2%s/access_token', empty($this->component) ? '' : '/component'); + return \sprintf($this->baseUrl . '/oauth2%s/access_token', empty($this->component) ? '' : '/component'); } public function userFromCode(string $code): Contracts\UserInterface { if (\in_array('snsapi_base', $this->scopes)) { - return $this->mapUserToObject($this->fromJsonBody($this->getTokenFromCode($code))); + return $this->getSnsapiBaseUserFromCode($code); } $token = $this->tokenFromCode($code); @@ -128,11 +128,23 @@ public function userFromCode(string $code): Contracts\UserInterface ->setTokenResponse($token); } + protected function getSnsapiBaseUserFromCode(string $code): Contracts\UserInterface + { + $token = $this->fromJsonBody($this->getTokenFromCode($code)); + $user = [ + 'openid' => $token['openid'], + ]; + if (isset($token['unionid'])) { + $user['unionid'] = $token['unionid']; + } + return $this->mapUserToObject($token)->setProvider($this)->setRaw($user)->setAccessToken($token[$this->accessTokenKey]); + } + protected function getUserByToken(string $token): array { $language = $this->withCountryCode ? null : (isset($this->parameters['lang']) ? $this->parameters['lang'] : 'zh_CN'); - $response = $this->getHttpClient()->get($this->baseUrl.'/userinfo', [ + $response = $this->getHttpClient()->get($this->baseUrl . '/userinfo', [ 'query' => \array_filter([ Contracts\RFC6749_ABNF_ACCESS_TOKEN => $token, 'openid' => $this->openid, @@ -205,11 +217,11 @@ protected function prepareForComponent(array $component): void } } - if (\count($config) !== 2) { + if (2 !== \count($config)) { throw new Exceptions\InvalidArgumentException('Please check your config arguments were available.'); } - if (\count($this->scopes) === 1 && \in_array('snsapi_login', $this->scopes)) { + if (1 === \count($this->scopes) && \in_array('snsapi_login', $this->scopes)) { $this->scopes = ['snsapi_base']; }