From 41a0d0c41877d8a3dbe62d7649b2afa2cefae0cc Mon Sep 17 00:00:00 2001 From: wuhui_zzw <1760308791@qq.com> Date: Tue, 14 Nov 2023 17:29:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E5=90=8E=E8=8E=B7=E5=8F=96=E6=98=B5=E7=A7=B0?= =?UTF-8?q?=E5=A4=B4=E5=83=8F=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wechat/WechatUserRepository.php | 8 ++--- app/controller/api/Auth.php | 29 ++++++++++--------- 2 files changed, 19 insertions(+), 18 deletions(-) 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']);