feat : login

This commit is contained in:
2025-03-19 16:56:38 +08:00
parent 307c0ec930
commit ac1cd0725d
3 changed files with 30 additions and 9 deletions

View File

@@ -24,10 +24,11 @@ class LoginRequest extends FormRequest
return [ return [
'login_type' => 'required|string|in:wx_login,mobile_code', 'login_type' => 'required|string|in:wx_login,mobile_code',
'js_code' => 'required_if:login_type,wx_login|string', 'js_code' => 'required_if:login_type,wx_login|string',
'phone_code' => 'required_if:login_type,wx_login|string',
]; ];
} }
protected array $scenes = [ protected array $scenes = [
'login' => ['login_type', 'js_code'], 'login' => ['login_type', 'js_code', 'phone_code'],
]; ];
} }

View File

@@ -79,11 +79,11 @@ abstract class LoginBaseService extends BaseService
*/ */
protected string $lockKey; protected string $lockKey;
/** // /**
* 登录code login_type = wx_login 必填 // * 登录code login_type = wx_login 必填
* @var string // * @var string
*/ // */
protected string $jsCode = ''; // protected string $jsCode = '';
/** /**
* 手机号 login_type = mobile_code 必填 * 手机号 login_type = mobile_code 必填

View File

@@ -37,25 +37,45 @@ class WxFastLoginService extends LoginBaseService
*/ */
public function handle(): array 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->openId = $wxInfo['openid'];
$this->unionId = $wxInfo['unionid'] ?? ''; $this->unionId = $wxInfo['unionid'] ?? '';
$this->jsCodeGetPhoneNumber($phoneCode);
$this->mobile = $wxPhone['phone_info']['purePhoneNumber'] ?? '';
$this->checkLock(self::LOGIN_TYPE); $this->checkLock(self::LOGIN_TYPE);
$this->register(); $this->register();
$this->login(); $this->login();
$this->updateMobile();
$this->redis->delLock($this->lockKey); $this->redis->delLock($this->lockKey);
return $this->getReturn(); 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 * @return void