diff --git a/app/common/repositories/wechat/WechatUserRepository.php b/app/common/repositories/wechat/WechatUserRepository.php index 190f34a..ee67845 100644 --- a/app/common/repositories/wechat/WechatUserRepository.php +++ b/app/common/repositories/wechat/WechatUserRepository.php @@ -106,16 +106,14 @@ class WechatUserRepository extends BaseRepository $routineInfo['city'] = $routine['city'] ?? '';//城市 $routineInfo['province'] = $routine['province'] ?? '';//省份 $routineInfo['country'] = $routine['country'] ?? '';//国家 - $routineInfo['headimgurl'] = $routine['avatarUrl'] !== $def ?: '';//头像 + $routineInfo['headimgurl'] = $routine['avatarUrl'] !== $def ? $routine['avatarUrl'] : '';//头像 $routineInfo['routine_openid'] = $routineOpenid;//openid $routineInfo['session_key'] = $routine['session_key'] ?? '';//会话密匙 $routineInfo['unionid'] = $routine['unionId'];//用户在开放平台的唯一标识符 $routineInfo['user_type'] = 'routine';//用户类型 $wechatUser = null; - if ($routineInfo['unionid']) - $wechatUser = $this->dao->unionIdByWechatUser($routineInfo['unionid']); - if (!$wechatUser) - $wechatUser = $this->dao->routineIdByWechatUser($routineOpenid); + if ($routineInfo['unionid']) $wechatUser = $this->dao->unionIdByWechatUser($routineInfo['unionid']); + if (!$wechatUser) $wechatUser = $this->dao->routineIdByWechatUser($routineOpenid); return Db::transaction(function () use ($createUser, $routineInfo, $wechatUser) { if ($wechatUser) { $routineInfo['nickname'] = $wechatUser['nickname']; diff --git a/app/controller/api/Auth.php b/app/controller/api/Auth.php index a12ec07..4d7af3b 100644 --- a/app/controller/api/Auth.php +++ b/app/controller/api/Auth.php @@ -429,14 +429,12 @@ class Auth extends BaseController private function authInfo($auth, $createUser = false) { - if (!in_array($auth['type'] ?? '', ['wechat', 'routine', 'apple', 'app_wechat']) || !isset($auth['auth'])) - throw new ValidateException('授权信息类型有误'); + if (!in_array($auth['type'] ?? '', ['wechat', 'routine', 'apple', 'app_wechat']) || !isset($auth['auth'])) throw new ValidateException('授权信息类型有误'); $data = $auth['auth']; if ($auth['type'] === 'routine') { $code = $data['code'] ?? ''; $userInfoCong = Cache::get('eb_api_code_' . $code); - if (!$code && !$userInfoCong) - throw new ValidateException('授权失败,参数有误'); + if (!$code && !$userInfoCong) throw new ValidateException('授权失败,参数有误'); $miniProgramService = MiniProgramService::create(); if ($code && !$userInfoCong) { try { @@ -458,13 +456,17 @@ class Auth extends BaseController $userInfo['unionId'] = $userInfoCong['unionid'] ?? $userInfo['unionId'] ?? ''; if (!$userInfo['openId']) throw new ValidateException('openid获取失败'); + $userInfo['nickName'] = $auth['auth']['new_nickname'] ?? $userInfo['nickName']; + $userInfo['avatarUrl'] = $auth['auth']['new_avatar'] ?? $userInfo['avatarUrl']; + /** @var WechatUserRepository $make */ $make = app()->make(WechatUserRepository::class); $user = $make->syncRoutineUser($userInfo['openId'], $userInfo, $createUser); - if (!$user) - throw new ValidateException('授权失败'); + if (!$user) throw new ValidateException('授权失败'); + return $user; - } else if ($auth['type'] === 'wechat') { + } + else if ($auth['type'] === 'wechat') { $request = $this->request; $oauth = WechatService::create()->getApplication()->oauth; $oauth->setRequest(new Request($data, $data, [], [], [], $request->server(), $request->getContent())); @@ -483,7 +485,8 @@ class Auth extends BaseController if (!$user) throw new ValidateException('授权失败[003]'); return $user; - } else if ($auth['type'] === 'app_wechat') { + } + else if ($auth['type'] === 'app_wechat') { $oauth = WechatService::create()->getApplication()->oauth; try { $wechatInfo = $oauth->user(new AccessToken(['access_token'=>$data['code'],'openid'=>$data['openid']]))->getOriginal(); @@ -494,7 +497,8 @@ class Auth extends BaseController if (!$user) throw new ValidateException('授权失败'); return $user; - } else if ($auth['type'] === 'apple') { + } + else if ($auth['type'] === 'apple') { $identityToken = $data['userInfo']['identityToken']; $tks = explode('.', $identityToken); if (count($tks) != 3) { @@ -516,12 +520,11 @@ class Auth extends BaseController } } - public function authLogin() - { + public function authLogin(){ $auth = $this->request->param('auth'); $users = $this->authInfo($auth, systemConfig('is_phone_login') !== '1'); - if (!$users) - return app('json')->fail('授权失败'); + + if (!$users) return app('json')->fail('授权失败'); $authInfo = $users[0]; $userRepository = app()->make(UserRepository::class); $user = $users[1] ?? $userRepository->wechatUserIdBytUser($authInfo['wechat_user_id']);