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

306 lines
9.7 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
*/
2021-04-20 16:30:57 +08:00
2020-11-04 11:57:16 +08:00
namespace App\Controller\Api\V1;
2020-11-07 22:57:10 +08:00
use Hyperf\Di\Annotation\Inject;
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;
2021-08-28 23:41:01 +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
/**
* @Inject
* @var UserService
*/
2020-11-29 17:39:24 +08:00
private $userService;
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
*/
public function getUserDetail()
{
2020-11-09 17:41:22 +08:00
$userInfo = $this->userService->findById($this->uid(), ['mobile', 'nickname', 'avatar', 'motto', 'email', 'gender']);
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,
'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
*/
public function getUserSetting()
{
2020-11-09 17:41:22 +08:00
$userInfo = $this->userService->findById($this->uid(), ['id', 'nickname', 'avatar', 'motto', 'gender']);
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,
'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
* 编辑我的信息
* @RequestMapping(path="edit-user-detail", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-07 22:57:10 +08:00
*/
public function editUserDetail()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['nickname', 'avatar', 'motto', 'gender']);
$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
]);
$isTrue = User::where('id', $this->uid())->update($params);
return $isTrue
? $this->response->success([], '个人信息修改成功...')
2021-05-13 18:01:34 +08:00
: $this->response->fail('个人信息修改失败!');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 修改用户头像
* @RequestMapping(path="edit-avatar", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-07 22:57:10 +08:00
*/
public function editAvatar()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['avatar']);
$this->validate($params, [
'avatar' => 'required|url'
]);
$isTrue = User::where('id', $this->uid())->update(['avatar' => $params['avatar']]);
2020-11-29 17:39:24 +08:00
2020-11-09 17:41:22 +08:00
return $isTrue
? $this->response->success([], '头像修改成功...')
2021-05-13 18:01:34 +08:00
: $this->response->fail('头像修改失败!');
2020-11-07 22:57:10 +08:00
}
/**
2021-07-21 23:31:41 +08:00
* 通过用户ID查找用户
2020-11-13 23:09:56 +08:00
* @RequestMapping(path="search-user", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-07 22:57:10 +08:00
*/
2021-07-21 23:31:41 +08:00
public function search()
2020-11-07 22:57:10 +08:00
{
2021-01-28 20:07:14 +08:00
$params = $this->request->inputs(['user_id']);
$this->validate($params, ['user_id' => 'required|integer']);
2020-11-09 17:41:22 +08:00
2021-04-20 16:30:57 +08:00
if ($data = $this->userService->getUserCard($params['user_id'], $this->uid())) {
2020-11-09 17:41:22 +08:00
return $this->response->success($data);
}
2021-05-13 18:01:34 +08:00
return $this->response->fail('用户查询失败!');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +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
*/
public function editUserPassword()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['old_password', 'new_password']);
$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
]);
$userInfo = $this->userService->findById($this->uid(), ['id', 'password', 'mobile']);
// 验证密码是否正确
2021-08-28 23:41:01 +08:00
if (!HashHelper::check($this->request->post('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']);
return $isTrue
? $this->response->success([], '密码修改成功...')
2021-05-13 18:01:34 +08:00
: $this->response->fail('密码修改失败!');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 更换用户手机号
* @RequestMapping(path="change-mobile", methods="post")
*
* @param SmsCodeService $smsCodeService
2020-12-26 21:33:40 +08:00
* @return ResponseInterface
2020-11-07 22:57:10 +08:00
*/
2020-11-09 17:41:22 +08:00
public function editUserMobile(SmsCodeService $smsCodeService)
2020-11-07 22:57:10 +08:00
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['mobile', 'password', 'sms_code']);
$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
]);
if (!$smsCodeService->check('change_mobile', $params['mobile'], $params['sms_code'])) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('验证码填写错误!');
2020-11-09 17:41:22 +08:00
}
$user_id = $this->uid();
2021-08-28 23:41:01 +08:00
if (!HashHelper::check($params['password'], User::where('id', $user_id)->value('password'))) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('账号密码验证失败!');
2020-11-09 17:41:22 +08:00
}
[$isTrue,] = $this->userService->changeMobile($user_id, $params['mobile']);
if (!$isTrue) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('手机号更换失败!');
2020-11-09 17:41:22 +08:00
}
// 清除缓存信息
$smsCodeService->delCode('change_mobile', $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
* 修改用户邮箱接口
* @RequestMapping(path="change-email", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-07 22:57:10 +08:00
*/
public function editUserEmail()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['email', 'password', 'email_code']);
$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
]);
$sendEmailCode = new SendEmailCode();
if (!$sendEmailCode->check(SendEmailCode::CHANGE_EMAIL, $params['email'], $params['email_code'])) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('验证码填写错误!');
2020-11-09 17:41:22 +08:00
}
2021-04-20 16:30:57 +08:00
$uid = $this->uid();
2020-11-09 17:41:22 +08:00
$user_password = User::where('id', $uid)->value('password');
2021-08-28 23:41:01 +08:00
if (!HashHelper::check($params['password'], $user_password)) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('账号密码验证失败!');
2020-11-09 17:41:22 +08:00
}
$isTrue = User::where('id', $uid)->update(['email' => $params['email']]);
if (!$isTrue) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('邮箱设置失败!');
2020-11-09 17:41:22 +08:00
}
$sendEmailCode->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
* 修改手机号发送验证码
* @RequestMapping(path="send-mobile-code", methods="post")
*
* @param SmsCodeService $smsCodeService
2020-12-26 21:33:40 +08:00
* @return ResponseInterface
2020-11-07 22:57:10 +08:00
*/
2020-11-09 17:41:22 +08:00
public function sendMobileCode(SmsCodeService $smsCodeService)
2020-11-07 22:57:10 +08:00
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['mobile']);
$this->validate($params, [
2021-05-24 19:03:27 +08:00
'mobile' => "present|phone"
2020-11-09 17:41:22 +08:00
]);
$user_id = $this->uid();
if (in_array($user_id, [2054, 2055])) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('测试账号不支持修改手机号!');
2020-11-09 17:41:22 +08:00
}
if (User::where('mobile', $params['mobile'])->exists()) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('手机号已被他人注册!');
2020-11-09 17:41:22 +08:00
}
$data = ['is_debug' => true];
[$isTrue, $result] = $smsCodeService->send('change_mobile', $params['mobile']);
2020-11-29 17:39:24 +08:00
if (!$isTrue) {
2020-11-09 17:41:22 +08:00
// ... 处理发送失败逻辑,当前默认发送成功
2021-05-13 18:01:34 +08:00
return $this->response->fail('验证码发送失败!');
2020-11-09 17:41:22 +08:00
}
2020-11-29 17:39:24 +08:00
// 测试环境下直接返回验证码
$data['sms_code'] = $result['data']['code'];
2020-11-09 17:41:22 +08:00
return $this->response->success($data, '验证码发送成功...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 发送绑定邮箱的验证码
* @RequestMapping(path="send-change-email-code", methods="post")
*
* @param SendEmailCode $sendEmailCode
2020-12-26 21:33:40 +08:00
* @return ResponseInterface
2020-11-07 22:57:10 +08:00
*/
2020-11-09 17:41:22 +08:00
public function sendChangeEmailCode(SendEmailCode $sendEmailCode)
2020-11-07 22:57:10 +08:00
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['email']);
$this->validate($params, [
2020-11-29 17:39:24 +08:00
'email' => "required|email"
2020-11-09 17:41:22 +08:00
]);
$isTrue = $sendEmailCode->send(SendEmailCode::CHANGE_EMAIL, '绑定邮箱', $params['email']);
if (!$isTrue) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('验证码发送失败!');
2020-11-09 17:41:22 +08:00
}
2020-11-07 22:57:10 +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
}