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;
|
|
|
|
|
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-31 22:33:41 +08:00
|
|
|
use App\Helpers\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
|
|
|
*/
|
2021-09-05 15:47:01 +08:00
|
|
|
public function getUserDetail(): 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
|
|
|
*/
|
2021-09-05 15:47:01 +08:00
|
|
|
public function getUserSetting(): 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
|
|
|
* 编辑我的信息
|
|
|
|
* @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
|
|
|
*/
|
2021-09-05 15:47:01 +08:00
|
|
|
public function editUserDetail(): 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']);
|
|
|
|
$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
|
|
|
]);
|
|
|
|
|
2021-09-11 12:41:28 +08:00
|
|
|
$params['updated_at'] = date("Y-m-d H:i:s");
|
|
|
|
|
|
|
|
User::where('id', $this->uid())->update($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
|
|
|
* 修改用户头像
|
|
|
|
* @RequestMapping(path="edit-avatar", methods="post")
|
2021-04-22 16:14:34 +08:00
|
|
|
*
|
|
|
|
* @return ResponseInterface
|
2020-11-07 22:57:10 +08:00
|
|
|
*/
|
2021-09-05 15:47:01 +08:00
|
|
|
public function editAvatar(): ResponseInterface
|
2020-11-07 22:57:10 +08:00
|
|
|
{
|
2020-11-09 17:41:22 +08:00
|
|
|
$params = $this->request->inputs(['avatar']);
|
|
|
|
$this->validate($params, [
|
|
|
|
'avatar' => 'required|url'
|
|
|
|
]);
|
|
|
|
|
2021-09-11 12:41:28 +08:00
|
|
|
User::where('id', $this->uid())->update([
|
|
|
|
'avatar' => $params['avatar'],
|
|
|
|
'updated_at' => date("Y-m-d H:i:s")
|
|
|
|
]);
|
2020-11-29 17:39:24 +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
|
|
|
* 修改我的密码
|
|
|
|
* @RequestMapping(path="change-password", methods="post")
|
2021-04-22 16:14:34 +08:00
|
|
|
*
|
|
|
|
* @return ResponseInterface
|
2020-11-07 22:57:10 +08:00
|
|
|
*/
|
2021-09-05 15:47:01 +08:00
|
|
|
public function editUserPassword(): 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']);
|
|
|
|
$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
|
|
|
* 更换用户手机号
|
|
|
|
* @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
|
|
|
*/
|
2021-09-05 15:47:01 +08:00
|
|
|
public function editUserMobile(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']);
|
|
|
|
$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
|
|
|
]);
|
|
|
|
|
2021-09-11 12:41:28 +08:00
|
|
|
if (!$smsCodeService->check('change_mobile', $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
|
|
|
}
|
|
|
|
|
|
|
|
// 清除缓存信息
|
|
|
|
$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
|
|
|
*/
|
2021-09-05 15:47:01 +08:00
|
|
|
public function editUserEmail(): 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']);
|
|
|
|
$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();
|
2021-09-11 12:41:28 +08:00
|
|
|
if (!$sendEmailCode->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
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
*/
|
2021-09-05 15:47:01 +08:00
|
|
|
public function sendMobileCode(SmsCodeService $smsCodeService): ResponseInterface
|
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
|
|
|
*/
|
2021-09-05 15:47:01 +08:00
|
|
|
public function sendChangeEmailCode(SendEmailCode $sendEmailCode): ResponseInterface
|
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
|
|
|
}
|