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

220 lines
6.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-22 20:08:19 +08:00
use App\Constant\SmsConstant;
2022-01-22 21:02:53 +08:00
use App\Repository\UserRepository;
2020-11-07 22:57:10 +08:00
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
2020-11-24 23:23:12 +08:00
use App\Middleware\JWTAuthMiddleware;
2020-11-09 17:41:22 +08:00
use App\Model\User;
use App\Support\SendEmailCode;
2022-01-22 20:08:19 +08:00
use App\Helper\HashHelper;
2020-11-07 22:57:10 +08:00
use App\Service\UserService;
2020-11-29 14:44:11 +08:00
use App\Service\SmsCodeService;
2020-12-26 21:33:40 +08:00
use Psr\Http\Message\ResponseInterface;
2020-11-09 17:41:22 +08:00
/**
* Class UsersController
2021-07-05 21:52:44 +08:00
* @Controller(prefix="/api/v1/users")
2020-11-09 17:41:22 +08:00
* @Middleware(JWTAuthMiddleware::class)
*
* @package App\Controller\Api\V1
*/
2020-11-04 11:57:16 +08:00
class UsersController extends CController
{
2020-11-07 22:57:10 +08:00
/**
* @var UserService
*/
2020-11-29 17:39:24 +08:00
private $userService;
2020-11-07 22:57:10 +08:00
2022-01-22 21:02:53 +08:00
/**
* @var UserRepository
*/
private $userRepository;
public function __construct(UserService $userService, UserRepository $userRepository)
{
parent::__construct();
$this->userService = $userService;
$this->userRepository = $userRepository;
}
2020-11-07 22:57:10 +08:00
/**
2020-11-09 17:41:22 +08:00
* 获取我的信息
2020-11-07 22:57:10 +08:00
* @RequestMapping(path="detail", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-07 22:57:10 +08:00
*/
2022-01-21 21:46:17 +08:00
public function getDetail(): ResponseInterface
2020-11-07 22:57:10 +08:00
{
2021-09-05 15:47:01 +08:00
$userInfo = $this->user();
2021-04-22 16:54:01 +08:00
2020-11-09 17:41:22 +08:00
return $this->response->success([
2021-04-20 16:30:57 +08:00
'mobile' => $userInfo->mobile,
2020-11-09 17:41:22 +08:00
'nickname' => $userInfo->nickname,
2021-04-20 16:30:57 +08:00
'avatar' => $userInfo->avatar,
'motto' => $userInfo->motto,
'email' => $userInfo->email,
2021-09-05 15:47:01 +08:00
'gender' => $userInfo->gender,
2020-11-09 17:41:22 +08:00
]);
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 用户相关设置
2020-11-07 22:57:10 +08:00
* @RequestMapping(path="setting", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-07 22:57:10 +08:00
*/
2022-01-21 21:46:17 +08:00
public function getSetting(): ResponseInterface
2020-11-07 22:57:10 +08:00
{
2021-09-05 15:47:01 +08:00
$userInfo = $this->user();
2021-04-22 16:54:01 +08:00
2020-11-09 17:41:22 +08:00
return $this->response->success([
'user_info' => [
2021-04-20 16:30:57 +08:00
'uid' => $userInfo->id,
2020-11-09 17:41:22 +08:00
'nickname' => $userInfo->nickname,
2021-04-20 16:30:57 +08:00
'avatar' => $userInfo->avatar,
'motto' => $userInfo->motto,
2021-09-05 15:47:01 +08:00
'gender' => $userInfo->gender,
2020-11-09 17:41:22 +08:00
],
2021-04-20 16:30:57 +08:00
'setting' => [
'theme_mode' => '',
'theme_bag_img' => '',
'theme_color' => '',
'notify_cue_tone' => '',
2020-11-09 17:41:22 +08:00
'keyboard_event_notify' => ''
]
]);
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 编辑我的信息
2022-01-22 12:48:28 +08:00
*
* @RequestMapping(path="change/detail", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-07 22:57:10 +08:00
*/
2022-01-21 21:46:17 +08:00
public function editDetail(): ResponseInterface
2020-11-07 22:57:10 +08:00
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['nickname', 'avatar', 'motto', 'gender']);
2022-01-21 21:46:17 +08:00
2020-11-09 17:41:22 +08:00
$this->validate($params, [
'nickname' => 'required',
2021-04-20 16:30:57 +08:00
'motto' => 'present|max:100',
'gender' => 'required|in:0,1,2',
'avatar' => 'present|url'
2020-11-09 17:41:22 +08:00
]);
2022-01-22 21:02:53 +08:00
$this->userRepository->update(["id" => $this->uid()], $params);
2020-11-09 17:41:22 +08:00
2021-09-11 12:41:28 +08:00
return $this->response->success([], '个人信息修改成功...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 修改我的密码
2022-01-22 12:48:28 +08:00
*
* @RequestMapping(path="change/password", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-07 22:57:10 +08:00
*/
2022-01-21 21:46:17 +08:00
public function editPassword(): ResponseInterface
2020-11-07 22:57:10 +08:00
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['old_password', 'new_password']);
2022-01-21 21:46:17 +08:00
2020-11-09 17:41:22 +08:00
$this->validate($params, [
'old_password' => 'required',
2020-12-01 13:54:40 +08:00
'new_password' => 'required|min:6|max:16'
2020-11-09 17:41:22 +08:00
]);
2021-09-05 15:47:01 +08:00
$userInfo = $this->user();
2020-11-09 17:41:22 +08:00
// 验证密码是否正确
2021-09-11 12:41:28 +08:00
if (!HashHelper::check($params['old_password'], $userInfo->password)) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('旧密码验证失败!');
2020-11-09 17:41:22 +08:00
}
$isTrue = $this->userService->resetPassword($userInfo->mobile, $params['new_password']);
2021-09-11 12:41:28 +08:00
if (!$isTrue) {
return $this->response->fail('密码修改失败!');
}
return $this->response->success([], '密码修改成功...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 更换用户手机号
2022-01-22 12:48:28 +08:00
*
* @RequestMapping(path="change/mobile", methods="post")
2020-11-09 17:41:22 +08:00
*
* @param SmsCodeService $smsCodeService
2020-12-26 21:33:40 +08:00
* @return ResponseInterface
2020-11-07 22:57:10 +08:00
*/
2022-01-21 21:46:17 +08:00
public function editMobile(SmsCodeService $smsCodeService): ResponseInterface
2020-11-07 22:57:10 +08:00
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['mobile', 'password', 'sms_code']);
2022-01-21 21:46:17 +08:00
2020-11-09 17:41:22 +08:00
$this->validate($params, [
2021-05-24 19:03:27 +08:00
'mobile' => "required|phone",
2020-11-09 17:41:22 +08:00
'password' => 'required',
2020-11-29 17:39:24 +08:00
'sms_code' => 'required|digits:6'
2020-11-09 17:41:22 +08:00
]);
2022-01-21 21:46:17 +08:00
if (!$smsCodeService->check(SmsConstant::SmsChangeAccountChannel, $params['mobile'], (string)$params['sms_code'])) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('验证码填写错误!');
2020-11-09 17:41:22 +08:00
}
2021-09-05 15:47:01 +08:00
if (!HashHelper::check($params['password'], $this->user()->password)) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('账号密码验证失败!');
2020-11-09 17:41:22 +08:00
}
2021-09-05 15:47:01 +08:00
[$isTrue,] = $this->userService->changeMobile($this->uid(), $params['mobile']);
2020-11-09 17:41:22 +08:00
if (!$isTrue) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('手机号更换失败!');
2020-11-09 17:41:22 +08:00
}
2022-01-21 21:46:17 +08:00
$smsCodeService->delCode(SmsConstant::SmsChangeAccountChannel, $params['mobile']);
2020-11-29 17:39:24 +08:00
2020-11-09 17:41:22 +08:00
return $this->response->success([], '手机号更换成功...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 修改用户邮箱接口
2022-01-22 12:48:28 +08:00
*
* @RequestMapping(path="change/email", methods="post")
2021-04-22 16:14:34 +08:00
*
2022-01-21 21:46:17 +08:00
* @param \App\Support\SendEmailCode $emailCode
* @return \Psr\Http\Message\ResponseInterface
2020-11-07 22:57:10 +08:00
*/
2022-01-21 21:46:17 +08:00
public function editEmail(SendEmailCode $emailCode): ResponseInterface
2020-11-07 22:57:10 +08:00
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['email', 'password', 'email_code']);
2022-01-21 21:46:17 +08:00
2020-11-09 17:41:22 +08:00
$this->validate($params, [
2021-04-20 16:30:57 +08:00
'email' => 'required|email',
'password' => 'required',
2020-11-29 17:39:24 +08:00
'email_code' => 'required|digits:6'
2020-11-09 17:41:22 +08:00
]);
2022-01-21 21:46:17 +08:00
if (!$emailCode->check(SendEmailCode::CHANGE_EMAIL, $params['email'], (string)$params['email_code'])) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('验证码填写错误!');
2020-11-09 17:41:22 +08:00
}
2021-09-05 15:47:01 +08:00
if (!HashHelper::check($params['password'], $this->user()->password)) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('账号密码验证失败!');
2020-11-09 17:41:22 +08:00
}
2021-09-05 15:47:01 +08:00
$isTrue = User::where('id', $this->uid())->update(['email' => $params['email']]);
2020-11-09 17:41:22 +08:00
if (!$isTrue) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('邮箱设置失败!');
2020-11-09 17:41:22 +08:00
}
2022-01-21 21:46:17 +08:00
$emailCode->delCode(SendEmailCode::CHANGE_EMAIL, $params['email']);
2020-11-29 17:39:24 +08:00
2020-11-09 17:41:22 +08:00
return $this->response->success([], '邮箱设置成功...');
2020-11-07 22:57:10 +08:00
}
2020-11-09 17:41:22 +08:00
}