diff --git a/app/Request/Api/LoginRequest.php b/app/Request/Api/LoginRequest.php index a2bc5ad..d23621d 100644 --- a/app/Request/Api/LoginRequest.php +++ b/app/Request/Api/LoginRequest.php @@ -24,10 +24,11 @@ class LoginRequest extends FormRequest return [ 'login_type' => 'required|string|in:wx_login,mobile_code', 'js_code' => 'required_if:login_type,wx_login|string', + 'phone_code' => 'required_if:login_type,wx_login|string', ]; } protected array $scenes = [ - 'login' => ['login_type', 'js_code'], + 'login' => ['login_type', 'js_code', 'phone_code'], ]; } diff --git a/app/Service/Api/Login/LoginBaseService.php b/app/Service/Api/Login/LoginBaseService.php index eedd911..bbd0864 100644 --- a/app/Service/Api/Login/LoginBaseService.php +++ b/app/Service/Api/Login/LoginBaseService.php @@ -79,11 +79,11 @@ abstract class LoginBaseService extends BaseService */ protected string $lockKey; - /** - * 登录code login_type = wx_login 必填 - * @var string - */ - protected string $jsCode = ''; +// /** +// * 登录code login_type = wx_login 必填 +// * @var string +// */ +// protected string $jsCode = ''; /** * 手机号 login_type = mobile_code 必填 diff --git a/app/Service/Api/Login/WxFastLoginService.php b/app/Service/Api/Login/WxFastLoginService.php index c568040..e053b6c 100644 --- a/app/Service/Api/Login/WxFastLoginService.php +++ b/app/Service/Api/Login/WxFastLoginService.php @@ -37,25 +37,45 @@ class WxFastLoginService extends LoginBaseService */ public function handle(): array { - $this->jsCode = $this->request->input('js_code'); + $jsCode = $this->request->input('js_code'); + $phoneCode = $this->request->input('phone_code'); - if (empty($this->jsCode)) throw new ErrException('登录参数错误'); + if (empty($jsCode) || empty($phoneCode)) throw new ErrException('登录参数错误'); - $wxInfo = $this->jsCodeGetOpenId($this->jsCode); + $wxInfo = $this->jsCodeGetOpenId($jsCode); $this->openId = $wxInfo['openid']; $this->unionId = $wxInfo['unionid'] ?? ''; + $this->jsCodeGetPhoneNumber($phoneCode); + $this->mobile = $wxPhone['phone_info']['purePhoneNumber'] ?? ''; + $this->checkLock(self::LOGIN_TYPE); $this->register(); $this->login(); + $this->updateMobile(); + $this->redis->delLock($this->lockKey); return $this->getReturn(); } + /** + * @return void + */ + private function updateMobile(): void + { + if (empty($this->mobile)) return; + + if ($this->userInfo->mobile == $this->mobile) return; + + $this->userInfo->mobile = $this->mobile; + + if (!$this->userInfo->save()) throw new ErrException('更新手机号失败'); + } + /** * 注册 * @return void