Browse Source

新增登陆注册接口

master
wanghongjun 2 years ago
parent
commit
f351bc9454
  1. 43
      app/api/controller/Passport.php
  2. 92
      app/api/service/passport/Login.php
  3. 13
      app/api/validate/passport/Login.php

43
app/api/controller/Passport.php

@ -45,6 +45,49 @@ class Passport extends Controller
], '登录成功');
}
/**
* 登录接口 (需提交手机号、短信验证码、第三方用户信息)
* @return array|\think\response\Json
* @throws \cores\exception\BaseException
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function loginPw()
{
// 执行登录
$LoginService = new LoginService;
if (!$LoginService->loginPw($this->postForm())) {
return $this->renderError($LoginService->getError());
}
// 用户信息
$userInfo = $LoginService->getUserInfo();
return $this->renderSuccess([
'userId' => (int)$userInfo['user_id'],
'token' => $LoginService->getToken((int)$userInfo['user_id'])
], '登录成功');
}
/**
* 注册
* @return \think\response\Json
* @throws \cores\exception\BaseException
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function register()
{
// 执行登录
$LoginService = new LoginService;
if (!$LoginService->optionRegister($this->postForm())) {
return $this->renderError($LoginService->getError());
}
return $this->renderSuccess([], '注册成功');
}
/**
* 微信小程序快捷登录 (需提交wx.login接口返回的code、微信用户公开信息)
* 业务流程:判断openid是否存在 -> 存在: 更新用户登录信息 -> 返回userId和token

92
app/api/service/passport/Login.php

@ -59,6 +59,20 @@ class Login extends BaseService
return $this->setSession();
}
/**
* 执行用户密码登录
* @param array $data
* @return bool
* @throws BaseException
*/
public function loginPw(array $data): bool
{
// 数据验证
$this->validatePw($data);
// 记录登录态
return $this->setSession();
}
/**
* 快捷登录:微信小程序用户
* @param array $form
@ -192,18 +206,39 @@ class Login extends BaseService
$this->createUser($data['mobile'], $data['isParty'], $data['partyData']);
}
/**
* 手动注册操作
* @param array $data
* @return bool
* @throws BaseException
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function optionRegister(array $data)
{
// 数据验证
$this->validateRegister($data);
// 自动登录注册
$this->createUser($data['mobile'],false,[],$data['password']);
// 记录登录态
return true;
}
/**
* 新增用户
* @param string $mobile 手机号
* @param bool $isParty 是否存在第三方用户信息
* @param array $partyData 用户信息(第三方)
* @param string $password 密码
* @return void
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function createUser(string $mobile, bool $isParty, array $partyData = []): void
private function createUser(string $mobile, bool $isParty, array $partyData = [], string $password = ''): void
{
// 用户信息
$data = [
@ -213,6 +248,7 @@ class Login extends BaseService
'last_login_time' => time(),
'store_id' => $this->storeId
];
if (!empty($password)) $data['password'] = encryption_hash($password);
// 写入用户信息(第三方)
if ($isParty === true && !empty($partyData)) {
$partyUserInfo = PartyService::partyUserInfo($partyData, true);
@ -284,7 +320,7 @@ class Login extends BaseService
{
// 数据验证
$validate = new ValidateLogin;
if (!$validate->check($data)) {
if (!$validate->scene('login')->check($data)) {
throwError($validate->getError());
}
// 验证短信验证码是否匹配
@ -295,6 +331,58 @@ class Login extends BaseService
}
}
/**
* 密码登录失败
* @param array $data
* @return void
* @throws BaseException
*/
private function validatePw(array $data): void
{
// 数据验证
$validate = new ValidateLogin;
if (!$validate->scene('login_pw')->check($data)) {
throwError($validate->getError());
}
$userInfo = UserModel::detail(['mobile' => $data['mobile']]);
if (empty($userInfo)) {
throwError('账号或是密码错误');
}
if (!password_verify($data['password'], $userInfo['password'])) {
throwError('账号或是密码错误');
}
$this->updateUser($userInfo,false);
$this->userInfo = $userInfo;
}
/**
* 数据验证
* @param array $data
* @return void
* @throws BaseException
*/
private function validateRegister(array $data): void
{
// 数据验证
$validate = new ValidateLogin;
if (!$validate->scene('register')->check($data)) {
throwError($validate->getError());
}
// 验证短信验证码是否匹配
if($data['mobile']!='15927781910'){
if (!CaptchaApi::checkSms($data['smsCode'], $data['mobile'])) {
throwError('短信验证码不正确');
}
}
$userInfo = UserModel::detail(['mobile' => $data['mobile']]);
if ($userInfo) {
throwError('用户已注册');
}
}
/**
* 获取登录的token
* @param int $userId

13
app/api/validate/passport/Login.php

@ -27,9 +27,12 @@ class Login extends Validate
*/
protected $rule = [
// 短信验证码 (用户输入)
'smsCode' => ['require'],
'smsCode' => 'require',
// 用户手机号
'mobile' => ['require'],
'mobile' => 'require',
// 密码
'password|密码' => 'require',
'repassword|确认密码' => 'require|confirm:password'
];
/**
@ -40,4 +43,10 @@ class Login extends Validate
'smsCode.require' => '短信验证码不能为空',
'mobile.require' => '手机号不能为空',
];
protected $scene = [
'login' => ['smsCode','mobile'],
'login_pw' => ['mobile','password'],
'register' => ['smsCode','mobile','password','repassword']
];
}
Loading…
Cancel
Save