hyperf-chat/app/Controller/Api/V1/AuthController.php

163 lines
4.5 KiB
PHP
Raw Normal View History

2020-11-04 11:57:16 +08:00
<?php
2021-09-05 15:47:01 +08:00
declare(strict_types=1);
2020-11-04 11:57:16 +08:00
namespace App\Controller\Api\V1;
2022-01-21 21:47:06 +08:00
use App\Constants\SmsConstant;
2021-07-20 23:18:53 +08:00
use App\Event\LoginEvent;
2020-11-04 16:47:17 +08:00
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use App\Service\UserService;
2020-11-04 17:36:52 +08:00
use App\Service\SmsCodeService;
2021-09-05 15:47:01 +08:00
use Psr\Http\Message\ResponseInterface;
2020-11-04 11:57:16 +08:00
2020-11-04 16:47:17 +08:00
/**
* 授权相关控制器
2021-07-05 21:52:44 +08:00
* @Controller(prefix="/api/v1/auth")
2020-11-04 16:47:17 +08:00
*/
2020-11-04 11:57:16 +08:00
class AuthController extends CController
{
2020-11-04 16:47:17 +08:00
/**
* @Inject
* @var UserService
*/
private $userService;
2020-11-04 22:58:49 +08:00
/**
* @Inject
* @var SmsCodeService
*/
2022-01-21 21:47:06 +08:00
private $sms;
2020-11-04 22:58:49 +08:00
2020-11-04 16:47:17 +08:00
/**
* 授权登录接口
2021-09-05 15:47:01 +08:00
*
2020-11-04 16:47:17 +08:00
* @RequestMapping(path="login", methods="post")
*/
2021-09-05 15:47:01 +08:00
public function login(): ResponseInterface
2020-11-04 16:47:17 +08:00
{
2020-12-01 13:54:40 +08:00
$params = $this->request->inputs(['mobile', 'password', 'platform']);
2022-01-21 22:19:42 +08:00
2020-12-01 13:54:40 +08:00
$this->validate($params, [
2021-05-24 19:03:27 +08:00
'mobile' => "required|phone",
2020-11-04 16:47:17 +08:00
'password' => 'required',
2020-11-13 23:09:56 +08:00
'platform' => 'required|in:h5,ios,windows,mac,web',
2020-11-04 16:47:17 +08:00
]);
2021-07-10 10:55:25 +08:00
$user = $this->userService->login($params['mobile'], $params['password']);
if (!$user) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('账号不存在或密码填写错误!');
2020-11-04 16:47:17 +08:00
}
try {
2021-07-10 10:55:25 +08:00
$token = $this->guard()->login($user);
2020-11-04 16:47:17 +08:00
} catch (\Exception $exception) {
2021-05-13 18:01:34 +08:00
return $this->response->error('登录异常,请稍后再试!');
2020-11-04 16:47:17 +08:00
}
2021-07-20 23:18:53 +08:00
event()->dispatch(new LoginEvent($this->request, $user));
2021-07-10 10:55:25 +08:00
2020-11-04 16:47:17 +08:00
return $this->response->success([
2022-01-16 10:29:16 +08:00
'type' => 'Bearer',
'access_token' => $token,
'expires_in' => $this->guard()->getJwtManager()->getTtl(),
2022-01-21 22:19:42 +08:00
]);
2020-11-04 16:47:17 +08:00
}
/**
* 退出登录接口
2021-09-05 15:47:01 +08:00
*
2020-11-04 17:36:52 +08:00
* @RequestMapping(path="logout", methods="post")
2020-11-04 16:47:17 +08:00
*/
2021-09-05 15:47:01 +08:00
public function logout(): ResponseInterface
2020-11-04 16:47:17 +08:00
{
2021-05-23 17:04:20 +08:00
$this->guard()->check() && $this->guard()->logout();
2020-11-04 16:47:17 +08:00
2022-01-21 22:19:42 +08:00
return $this->response->success();
2020-11-04 16:47:17 +08:00
}
/**
* 账号注册接口
2021-09-05 15:47:01 +08:00
*
2020-11-04 17:36:52 +08:00
* @RequestMapping(path="register", methods="post")
2020-11-04 16:47:17 +08:00
*/
2021-09-05 15:47:01 +08:00
public function register(): ResponseInterface
2020-11-04 16:47:17 +08:00
{
2020-11-04 17:36:52 +08:00
$params = $this->request->all();
2022-01-21 21:47:06 +08:00
2020-11-04 17:36:52 +08:00
$this->validate($params, [
2020-12-01 17:47:25 +08:00
'nickname' => "required|max:20",
2021-05-24 19:03:27 +08:00
'mobile' => "required|phone",
2020-12-01 17:47:25 +08:00
'password' => 'required|max:16',
2020-11-14 17:37:55 +08:00
'sms_code' => 'required|digits:6',
2020-11-13 23:09:56 +08:00
'platform' => 'required|in:h5,ios,windows,mac,web',
2020-11-04 17:36:52 +08:00
]);
2020-11-04 16:47:17 +08:00
2022-01-21 21:47:06 +08:00
if (!$this->sms->check(SmsConstant::SmsRegisterChannel, (string)$params['mobile'], (string)$params['sms_code'])) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('验证码填写错误!');
2020-11-04 17:36:52 +08:00
}
$isTrue = $this->userService->register([
2021-04-20 16:30:57 +08:00
'mobile' => $params['mobile'],
2020-11-04 17:36:52 +08:00
'password' => $params['password'],
'nickname' => strip_tags($params['nickname']),
]);
2020-11-04 22:58:49 +08:00
if (!$isTrue) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('账号注册失败!');
2020-11-04 17:36:52 +08:00
}
2022-01-21 21:47:06 +08:00
$this->sms->delCode(SmsConstant::SmsRegisterChannel, $params['mobile']);
2020-11-28 19:59:10 +08:00
2022-01-21 21:47:06 +08:00
return $this->response->success();
2020-11-04 16:47:17 +08:00
}
/**
* 账号找回接口
2021-09-05 15:47:01 +08:00
*
2020-11-04 16:47:17 +08:00
* @RequestMapping(path="forget", methods="post")
*/
2021-09-05 15:47:01 +08:00
public function forget(): ResponseInterface
2020-11-04 16:47:17 +08:00
{
2020-12-01 17:47:25 +08:00
$params = $this->request->inputs(['mobile', 'password', 'sms_code']);
2022-01-21 22:19:42 +08:00
2020-11-04 22:58:49 +08:00
$this->validate($params, [
2021-05-24 19:03:27 +08:00
'mobile' => "required|phone",
2020-12-01 17:47:25 +08:00
'password' => 'required|max:16',
2020-11-14 17:37:55 +08:00
'sms_code' => 'required|digits:6',
2020-11-04 22:58:49 +08:00
]);
2022-01-21 21:47:06 +08:00
if (!$this->sms->check(SmsConstant::SmsForgetAccountChannel, (string)$params['mobile'], (string)$params['sms_code'])) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('验证码填写错误!');
2020-11-04 22:58:49 +08:00
}
2020-11-04 16:47:17 +08:00
2020-11-04 22:58:49 +08:00
$isTrue = $this->userService->resetPassword($params['mobile'], $params['password']);
2020-11-28 19:59:10 +08:00
if (!$isTrue) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('重置密码失败!');
2020-11-04 22:58:49 +08:00
}
2022-01-21 21:47:06 +08:00
$this->sms->delCode(SmsConstant::SmsForgetAccountChannel, $params['mobile']);
2020-11-28 19:59:10 +08:00
2022-01-21 22:19:42 +08:00
return $this->response->success();
2020-11-04 16:47:17 +08:00
}
2020-11-04 11:57:16 +08:00
2020-11-04 16:47:17 +08:00
/**
* 授权刷新接口
2021-09-11 12:41:28 +08:00
*
2020-11-04 16:47:17 +08:00
* @RequestMapping(path="refresh", methods="post")
*/
2021-09-05 15:47:01 +08:00
public function refresh(): ResponseInterface
2020-11-04 16:47:17 +08:00
{
2021-05-23 17:04:20 +08:00
if ($this->guard()->guest()) {
2021-06-29 17:30:43 +08:00
return $this->response->fail('token 刷新失败!');
2021-05-23 16:52:01 +08:00
}
2020-11-04 16:47:17 +08:00
return $this->response->success([
2022-01-21 21:47:06 +08:00
'type' => 'Bearer',
'token' => $this->guard()->refresh(),
'expire' => $this->guard()->getJwtManager()->getTtl()
2020-11-04 22:58:49 +08:00
]);
}
2020-11-04 11:57:16 +08:00
}