diff --git a/src/Providers/WeChat.php b/src/Providers/WeChat.php index f7a0ba8..ebf889e 100644 --- a/src/Providers/WeChat.php +++ b/src/Providers/WeChat.php @@ -114,7 +114,7 @@ protected function getTokenUrl(): string 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,6 +128,19 @@ 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');