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

210 lines
6.5 KiB
PHP
Raw Normal View History

2020-11-04 11:57:16 +08:00
<?php
2020-12-26 21:33:40 +08:00
/**
* This is my open source code, please do not use it for commercial applications.
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code
*
* @author Yuandong<837215079@qq.com>
* @link https://github.com/gzydong/hyperf-chat
*/
2020-11-04 11:57:16 +08:00
namespace App\Controller\Api\V1;
2020-11-04 16:47:17 +08:00
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
2020-11-29 14:44:11 +08:00
use App\Model\User;
2020-11-04 16:47:17 +08:00
use App\Service\UserService;
2020-11-04 17:36:52 +08:00
use App\Service\SmsCodeService;
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
*/
private $smsCodeService;
2020-11-04 16:47:17 +08:00
/**
* 授权登录接口
* @RequestMapping(path="login", methods="post")
*/
public function login()
{
2020-12-01 13:54:40 +08:00
$params = $this->request->inputs(['mobile', 'password', 'platform']);
$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
]);
2020-12-01 13:54:40 +08:00
$userInfo = $this->userService->login($params['mobile'], $params['password']);
2020-11-04 16:47:17 +08:00
if (!$userInfo) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('账号不存在或密码填写错误!');
2020-11-04 16:47:17 +08:00
}
try {
2021-05-23 17:04:20 +08:00
$token = $this->guard()->login($userInfo);
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
}
return $this->response->success([
'authorize' => [
2021-06-29 17:30:43 +08:00
'type' => 'Bearer',
2020-11-13 23:09:56 +08:00
'access_token' => $token,
2021-06-29 17:30:43 +08:00
'expires_in' => $this->guard()->getJwtManager()->getTtl(),
2020-11-04 16:47:17 +08:00
],
'user_info' => [
2021-05-23 16:52:01 +08:00
'nickname' => $userInfo->nickname,
'avatar' => $userInfo->avatar,
'gender' => $userInfo->gender,
'motto' => $userInfo->motto,
'email' => $userInfo->email,
2020-11-04 16:47:17 +08:00
]
2021-05-23 17:04:20 +08:00
], '账号登录成功...');
2020-11-04 16:47:17 +08:00
}
/**
* 退出登录接口
2020-11-04 17:36:52 +08:00
* @RequestMapping(path="logout", methods="post")
2020-11-04 16:47:17 +08:00
*/
public function logout()
{
2021-05-23 17:04:20 +08:00
$this->guard()->check() && $this->guard()->logout();
2020-11-04 16:47:17 +08:00
2021-05-23 17:04:20 +08:00
return $this->response->success([], '退出登录成功...');
2020-11-04 16:47:17 +08:00
}
/**
* 账号注册接口
2020-11-04 17:36:52 +08:00
* @RequestMapping(path="register", methods="post")
2020-11-04 16:47:17 +08:00
*/
2020-11-04 22:58:49 +08:00
public function register()
2020-11-04 16:47:17 +08:00
{
2020-11-04 17:36:52 +08:00
$params = $this->request->all();
$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
2020-11-04 22:58:49 +08:00
if (!$this->smsCodeService->check('user_register', $params['mobile'], $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
}
2020-11-28 19:59:10 +08:00
// 删除验证码缓存
2020-11-04 22:58:49 +08:00
$this->smsCodeService->delCode('user_register', $params['mobile']);
2020-11-28 19:59:10 +08:00
2020-11-04 22:58:49 +08:00
return $this->response->success([], '账号注册成功...');
2020-11-04 16:47:17 +08:00
}
/**
* 账号找回接口
* @RequestMapping(path="forget", methods="post")
*/
public function forget()
{
2020-12-01 17:47:25 +08:00
$params = $this->request->inputs(['mobile', 'password', 'sms_code']);
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
]);
if (!$this->smsCodeService->check('forget_password', $params['mobile'], $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
}
2020-11-28 19:59:10 +08:00
// 删除验证码缓存
$this->smsCodeService->delCode('forget_password', $params['mobile']);
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
/**
* 授权刷新接口
* @RequestMapping(path="refresh", methods="post")
*/
public function refresh()
{
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([
'authorize' => [
2021-06-29 17:30:43 +08:00
'type' => 'Bearer',
2021-05-23 17:04:20 +08:00
'token' => $this->guard()->refresh(),
'expire' => $this->guard()->getJwtManager()->getTtl()
2020-11-04 16:47:17 +08:00
]
2020-11-28 19:59:10 +08:00
]);
2020-11-04 16:47:17 +08:00
}
2020-11-04 22:58:49 +08:00
/**
* 发送验证码
2020-11-27 19:48:41 +08:00
* @RequestMapping(path="send-verify-code", methods="post")
2020-11-04 22:58:49 +08:00
*/
public function sendVerifyCode()
{
2020-12-01 17:47:25 +08:00
$params = $this->request->inputs(['type', 'mobile']);
2020-11-04 22:58:49 +08:00
$this->validate($params, [
2021-04-20 16:30:57 +08:00
'type' => "required",
2021-05-24 19:03:27 +08:00
'mobile' => "required|phone"
2020-11-04 22:58:49 +08:00
]);
if (!$this->smsCodeService->isUsages($params['type'])) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('验证码发送失败!');
2020-11-04 22:58:49 +08:00
}
if ($params['type'] == 'forget_password') {
if (!User::where('mobile', $params['mobile'])->value('id')) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('手机号未被注册使用!');
2020-11-04 22:58:49 +08:00
}
} else if ($params['type'] == 'change_mobile' || $params['type'] == 'user_register') {
if (User::where('mobile', $params['mobile'])->value('id')) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('手机号已被他(她)人注册!');
2020-11-04 22:58:49 +08:00
}
}
$data = ['is_debug' => true];
[$isTrue, $result] = $this->smsCodeService->send($params['type'], $params['mobile']);
2020-11-28 19:59:10 +08:00
if (!$isTrue) {
2020-11-04 22:58:49 +08:00
// ... 处理发送失败逻辑,当前默认发送成功
2021-05-13 18:01:34 +08:00
return $this->response->fail('验证码发送失败!');
2020-11-04 22:58:49 +08:00
}
2020-11-29 14:44:11 +08:00
2020-11-28 19:59:10 +08:00
// 测试环境下直接返回验证码
$data['sms_code'] = $result['data']['code'];
2020-11-04 22:58:49 +08:00
return $this->response->success($data, '验证码发送成功...');
}
2020-11-04 11:57:16 +08:00
}