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

530 lines
16 KiB
PHP
Raw Normal View History

2020-11-04 11:57:16 +08:00
<?php
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-29 14:44:11 +08:00
use Hyperf\Amqp\Producer;
2020-11-09 17:41:22 +08:00
use App\Constants\ResponseCode;
use App\Helper\Hash;
use App\Model\User;
use App\Model\UsersChatList;
use App\Model\UsersFriend;
use App\Support\SendEmailCode;
2020-11-07 22:57:10 +08:00
use App\Service\FriendService;
use App\Service\UserService;
2020-11-29 14:44:11 +08:00
use App\Service\SocketClientService;
use App\Service\SmsCodeService;
2020-11-27 19:48:41 +08:00
use App\Amqp\Producer\ChatMessageProducer;
2020-11-29 14:44:11 +08:00
use App\Cache\ApplyNumCache;
2020-11-09 17:41:22 +08:00
/**
* Class UsersController
*
* @Controller(path="/api/v1/users")
* @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 FriendService
*/
2020-11-29 17:39:24 +08:00
private $friendService;
2020-11-04 11:57:16 +08:00
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
* @inject
2020-11-29 14:44:11 +08:00
* @var SocketClientService
2020-11-09 17:41:22 +08:00
*/
2020-11-29 14:44:11 +08:00
private $socketClientService;
2020-11-09 17:41:22 +08:00
2020-11-27 19:48:41 +08:00
/**
* @Inject
* @var Producer
*/
private $producer;
2020-11-09 17:41:22 +08:00
/**
* 获取我的好友列表
*
2020-11-07 22:57:10 +08:00
* @RequestMapping(path="friends", methods="get")
*/
public function getUserFriends()
{
2020-11-09 17:41:22 +08:00
$rows = UsersFriend::getUserFriends($this->uid());
2020-11-07 22:57:10 +08:00
2020-11-29 14:44:11 +08:00
$runArr = $this->socketClientService->getServerRunIdAll();
2020-11-09 17:41:22 +08:00
foreach ($rows as $k => $row) {
2020-11-29 14:44:11 +08:00
$rows[$k]['online'] = $this->socketClientService->isOnlineAll($row['id'], $runArr);
2020-11-09 17:41:22 +08:00
}
return $this->response->success($rows);
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 解除好友关系
*
* @RequestMapping(path="remove-friend", methods="post")
2020-11-07 22:57:10 +08:00
*/
public function removeFriend()
{
2020-12-01 13:54:40 +08:00
$params = $this->request->inputs(['friend_id']);
2020-11-09 17:41:22 +08:00
$this->validate($params, [
'friend_id' => 'required|integer'
]);
$user_id = $this->uid();
if (!$this->friendService->removeFriend($user_id, $params['friend_id'])) {
return $this->response->fail('好友关系解除成功...');
}
//删除好友会话列表
UsersChatList::delItem($user_id, $params['friend_id'], 2);
UsersChatList::delItem($params['friend_id'], $user_id, 2);
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
* 获取用户群聊列表
*
2020-11-07 22:57:10 +08:00
* @RequestMapping(path="user-groups", methods="get")
*/
public function getUserGroups()
{
2020-11-09 17:41:22 +08:00
return $this->response->success(
$this->userService->getUserChatGroups($this->uid())
);
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")
*/
public function getUserDetail()
{
2020-11-09 17:41:22 +08:00
$userInfo = $this->userService->findById($this->uid(), ['mobile', 'nickname', 'avatar', 'motto', 'email', 'gender']);
return $this->response->success([
'mobile' => $userInfo->mobile,
'nickname' => $userInfo->nickname,
'avatar' => $userInfo->avatar,
'motto' => $userInfo->motto,
'email' => $userInfo->email,
'gender' => $userInfo->gender
]);
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")
*/
public function getUserSetting()
{
2020-11-09 17:41:22 +08:00
$userInfo = $this->userService->findById($this->uid(), ['id', 'nickname', 'avatar', 'motto', 'gender']);
return $this->response->success([
'user_info' => [
'uid' => $userInfo->id,
'nickname' => $userInfo->nickname,
'avatar' => $userInfo->avatar,
'motto' => $userInfo->motto,
2020-11-29 17:39:24 +08:00
'gender' => $userInfo->gender
2020-11-09 17:41:22 +08:00
],
'setting' => [
'theme_mode' => '',
'theme_bag_img' => '',
'theme_color' => '',
'notify_cue_tone' => '',
'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")
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',
2020-12-01 13:54:40 +08:00
'motto' => 'present|max:100',
2020-11-14 17:37:55 +08:00
'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([], '个人信息修改成功...')
: $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")
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([], '头像修改成功...')
: $this->response->fail('头像修改失败...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 通过手机号查找用户
*
2020-11-13 23:09:56 +08:00
* @RequestMapping(path="search-user", methods="post")
2020-11-07 22:57:10 +08:00
*/
public function searchUserInfo()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['user_id', 'mobile']);
2020-11-14 17:37:55 +08:00
if (isset($params['user_id']) && !empty($params['user_id'])) {
2020-11-13 23:09:56 +08:00
$this->validate($params, ['user_id' => 'present|integer']);
2020-11-09 17:41:22 +08:00
$where['uid'] = $params['user_id'];
2020-11-14 17:37:55 +08:00
} else if (isset($params['mobile']) && !empty($params['mobile'])) {
2020-11-13 23:09:56 +08:00
$this->validate($params, ['mobile' => "present|regex:/^1[345789][0-9]{9}$/"]);
2020-11-09 17:41:22 +08:00
$where['mobile'] = $params['mobile'];
} else {
return $this->response->fail('请求参数不正确...', [], ResponseCode::VALIDATION_ERROR);
}
if ($data = $this->userService->searchUserInfo($where, $this->uid())) {
return $this->response->success($data);
}
return $this->response->fail('查询失败...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 编辑好友备注信息
*
* @RequestMapping(path="edit-friend-remark", methods="post")
2020-11-07 22:57:10 +08:00
*/
public function editFriendRemark()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['friend_id', 'remarks']);
$this->validate($params, [
'friend_id' => 'required|integer',
2020-11-29 17:39:24 +08:00
'remarks' => "required"
2020-11-09 17:41:22 +08:00
]);
$isTrue = $this->friendService->editFriendRemark($this->uid(), $params['friend_id'], $params['remarks']);
2020-11-29 17:39:24 +08:00
2020-11-09 17:41:22 +08:00
return $isTrue
? $this->response->success([], '备注修改成功...')
: $this->response->fail('备注修改失败...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 发送添加好友申请
*
* @RequestMapping(path="send-friend-apply", methods="post")
2020-11-07 22:57:10 +08:00
*/
public function sendFriendApply()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['friend_id', 'remarks']);
$this->validate($params, [
'friend_id' => 'required|integer',
2020-11-29 17:39:24 +08:00
'remarks' => 'present'
2020-11-09 17:41:22 +08:00
]);
$user = $this->userService->findById($params['friend_id']);
if (!$user) {
return $this->response->fail('用户不存在...');
}
2020-11-27 19:48:41 +08:00
$user_id = $this->uid();
if (!$this->friendService->addFriendApply($user_id, (int)$params['friend_id'], $params['remarks'])) {
2020-11-09 17:41:22 +08:00
return $this->response->fail('发送好友申请失败...');
}
2020-11-07 22:57:10 +08:00
2020-11-27 19:48:41 +08:00
// 好友申请未读消息数自增
ApplyNumCache::setInc((int)$params['friend_id']);
2020-11-09 17:41:22 +08:00
//判断对方是否在线。如果在线发送消息通知
2020-11-29 14:44:11 +08:00
if ($this->socketClientService->isOnlineAll((int)$params['friend_id'])) {
2020-11-27 19:48:41 +08:00
$this->producer->produce(
new ChatMessageProducer('event_friend_apply', [
'sender' => $user_id,
'receive' => (int)$params['friend_id'],
'type' => 1,
'status' => 1,
'remark' => ''
])
);
}
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="handle-friend-apply", methods="post")
2020-11-07 22:57:10 +08:00
*/
public function handleFriendApply()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['apply_id', 'remarks']);
$this->validate($params, [
'apply_id' => 'required|integer',
2020-11-29 17:39:24 +08:00
'remarks' => 'present'
2020-11-09 17:41:22 +08:00
]);
2020-12-01 13:54:40 +08:00
$user_id = $this->uid();
2020-11-27 19:48:41 +08:00
$isTrue = $this->friendService->handleFriendApply($this->uid(), (int)$params['apply_id'], $params['remarks']);
if (!$isTrue) {
return $this->response->fail('处理失败...');
2020-11-09 17:41:22 +08:00
}
2020-11-27 19:48:41 +08:00
//判断对方是否在线。如果在线发送消息通知
2020-11-29 14:44:11 +08:00
if ($this->socketClientService->isOnlineAll((int)$params['friend_id'])) {
2020-12-01 13:54:40 +08:00
// 待修改
$this->producer->produce(
new ChatMessageProducer('event_friend_apply', [
'sender' => $user_id,
'receive' => (int)$params['friend_id'],
'type' => 1,
'status' => 1,
'remark' => ''
])
);
2020-11-28 19:59:10 +08:00
}
2020-11-27 19:48:41 +08:00
return $this->response->success([], '处理成功...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 删除好友申请记录
*
* @RequestMapping(path="delete-friend-apply", methods="post")
2020-11-07 22:57:10 +08:00
*/
public function deleteFriendApply()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['apply_id']);
$this->validate($params, [
2020-11-29 17:39:24 +08:00
'apply_id' => 'required|integer'
2020-11-09 17:41:22 +08:00
]);
2020-11-27 19:48:41 +08:00
$isTrue = $this->friendService->delFriendApply($this->uid(), (int)$params['apply_id']);
2020-11-09 17:41:22 +08:00
return $isTrue
? $this->response->success([], '删除成功...')
: $this->response->fail('删除失败...');
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="friend-apply-records", methods="get")
*/
public function getFriendApplyRecords()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['page', 'page_size']);
$this->validate($params, [
'page' => 'present|integer',
2020-11-29 17:39:24 +08:00
'page_size' => 'present|integer'
2020-11-09 17:41:22 +08:00
]);
$page = $this->request->input('page', 1);
$page_size = $this->request->input('page_size', 10);
$user_id = $this->uid();
$data = $this->friendService->findApplyRecords($user_id, $page, $page_size);
return $this->response->success($data);
2020-11-07 22:57:10 +08:00
}
/**
2020-11-13 23:09:56 +08:00
* 获取好友申请未读数
*
2020-11-07 22:57:10 +08:00
* @RequestMapping(path="friend-apply-num", methods="get")
*/
public function getApplyUnreadNum()
{
2020-11-13 23:09:56 +08:00
$num = ApplyNumCache::get($this->uid());
return $this->response->success([
'unread_num' => $num ? $num : 0
]);
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 修改我的密码
*
* @RequestMapping(path="change-password", methods="post")
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']);
// 验证密码是否正确
if (!Hash::check($this->request->post('old_password'), $userInfo->password)) {
return $this->response->fail('旧密码验证失败...');
}
$isTrue = $this->userService->resetPassword($userInfo->mobile, $params['new_password']);
return $isTrue
? $this->response->success([], '密码修改成功...')
: $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
* @return \Psr\Http\Message\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, [
2020-11-14 17:37:55 +08:00
'mobile' => "required|regex:/^1[345789][0-9]{9}$/",
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'])) {
return $this->response->fail('验证码填写错误...');
}
$user_id = $this->uid();
if (!Hash::check($params['password'], User::where('id', $user_id)->value('password'))) {
return $this->response->fail('账号密码验证失败...');
}
[$isTrue,] = $this->userService->changeMobile($user_id, $params['mobile']);
if (!$isTrue) {
return $this->response->fail('手机号更换失败...');
}
// 清除缓存信息
$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")
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, [
2020-11-14 17:37:55 +08:00
'email' => 'required|email',
2020-11-09 17:41:22 +08:00
'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'])) {
return $this->response->fail('验证码填写错误...');
}
$uid = $this->uid();
$user_password = User::where('id', $uid)->value('password');
if (!Hash::check($params['password'], $user_password)) {
return $this->response->fail('账号密码验证失败...');
}
$isTrue = User::where('id', $uid)->update(['email' => $params['email']]);
if (!$isTrue) {
return $this->response->fail('邮箱设置失败...');
}
$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
* @return \Psr\Http\Message\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, [
2020-11-29 17:39:24 +08:00
'mobile' => "present|regex:/^1[345789][0-9]{9}$/"
2020-11-09 17:41:22 +08:00
]);
$user_id = $this->uid();
if (in_array($user_id, [2054, 2055])) {
return $this->response->fail('测试账号不支持修改手机号...');
}
if (User::where('mobile', $params['mobile'])->exists()) {
return $this->response->fail('手机号已被他人注册...');
}
$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
// ... 处理发送失败逻辑,当前默认发送成功
2020-11-29 17:39:24 +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
* @return \Psr\Http\Message\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) {
return $this->response->fail('验证码发送失败...');
}
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
}