From 16ab38200bb75831509cdcd40cdf25f9ab629eb6 Mon Sep 17 00:00:00 2001 From: gzydong <837215079@qq.com> Date: Thu, 13 May 2021 18:01:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Amqp/Consumer/ChatMessageConsumer.php | 2 +- app/Controller/Api/V1/ArticleController.php | 36 +++--- app/Controller/Api/V1/AuthController.php | 20 +-- app/Controller/Api/V1/CController.php | 4 +- app/Controller/Api/V1/ContactsController.php | 51 +++----- app/Controller/Api/V1/DownloadController.php | 10 +- app/Controller/Api/V1/EmoticonController.php | 16 +-- app/Controller/Api/V1/GroupController.php | 99 ++++++--------- app/Controller/Api/V1/TalkController.php | 127 ++++++++----------- app/Controller/Api/V1/UploadController.php | 8 +- app/Controller/Api/V1/UsersController.php | 30 ++--- app/Controller/WebSocketController.php | 49 +++---- app/Exception/ResponseException.php | 14 ++ app/Service/MessageHandleService.php | 31 ++--- app/helper.php | 15 +++ 15 files changed, 233 insertions(+), 279 deletions(-) create mode 100644 app/Exception/ResponseException.php diff --git a/app/Amqp/Consumer/ChatMessageConsumer.php b/app/Amqp/Consumer/ChatMessageConsumer.php index 5e73670..9e0992e 100644 --- a/app/Amqp/Consumer/ChatMessageConsumer.php +++ b/app/Amqp/Consumer/ChatMessageConsumer.php @@ -129,7 +129,7 @@ class ChatMessageConsumer extends ConsumerMessage // [加锁]防止消息重复消费 $lockName = sprintf('ws:message-lock:%s:%s', SERVER_RUN_ID, $data['uuid']); - if (!$redis->rawCommand('SET', $lockName, 1, 'NX', 'EX', 60)) { + if (!$redis->set($lockName, 1, ['nx', 'ex' => 60])) { return Result::ACK; } diff --git a/app/Controller/Api/V1/ArticleController.php b/app/Controller/Api/V1/ArticleController.php index a8732f3..37c43d9 100644 --- a/app/Controller/Api/V1/ArticleController.php +++ b/app/Controller/Api/V1/ArticleController.php @@ -136,7 +136,7 @@ class ArticleController extends CController $class_id = $this->articleService->editArticleClass($this->uid(), $params['class_id'], $params['class_name']); if (!$class_id) { - return $this->response->fail('笔记分类编辑失败...'); + return $this->response->fail('笔记分类编辑失败!'); } return $this->response->success(['id' => $class_id]); @@ -156,7 +156,7 @@ class ArticleController extends CController ]); if (!$this->articleService->delArticleClass($this->uid(), (int)$params['class_id'])) { - return $this->response->fail('笔记分类删除失败...'); + return $this->response->fail('笔记分类删除失败!'); } return $this->response->success([], '笔记分类删除成功...'); @@ -191,7 +191,7 @@ class ArticleController extends CController return $isTrue ? $this->response->success([], '排序完成...') - : $this->response->fail('排序失败...'); + : $this->response->fail('排序失败!'); } /** @@ -212,7 +212,7 @@ class ArticleController extends CController return $isTrue ? $this->response->success([], '合并完成...') - : $this->response->fail('合并完成...'); + : $this->response->fail('合并失败!'); } /** @@ -233,7 +233,7 @@ class ArticleController extends CController return $id ? $this->response->success(['id' => $id]) - : $this->response->fail('笔记标签编辑失败...'); + : $this->response->fail('笔记标签编辑失败!'); } /** @@ -253,7 +253,7 @@ class ArticleController extends CController return $isTrue ? $this->response->success([], '笔记标签删除完成...') - : $this->response->fail('笔记标签删除失败...'); + : $this->response->fail('笔记标签删除失败!'); } /** @@ -284,7 +284,7 @@ class ArticleController extends CController return $id ? $this->response->success(['aid' => $id], '笔记编辑成功...') - : $this->response->fail('笔记编辑失败...', ['id' => null]); + : $this->response->fail('笔记编辑失败!', ['id' => null]); } /** @@ -304,7 +304,7 @@ class ArticleController extends CController return $isTrue ? $this->response->success([], '笔记删除成功...') - : $this->response->fail('笔记删除失败...'); + : $this->response->fail('笔记删除失败!'); } /** @@ -324,7 +324,7 @@ class ArticleController extends CController return $isTrue ? $this->response->success([], '笔记恢复成功...') - : $this->response->fail('笔记恢复失败...'); + : $this->response->fail('笔记恢复失败!'); } /** @@ -379,7 +379,7 @@ class ArticleController extends CController return $isTrue ? $this->response->success([], '笔记移动成功...') - : $this->response->fail('笔记移动失败...'); + : $this->response->fail('笔记移动失败!'); } /** @@ -404,7 +404,7 @@ class ArticleController extends CController return $isTrue ? $this->response->success([], '笔记标记成功...') - : $this->response->fail('笔记标记失败...'); + : $this->response->fail('笔记标记失败!'); } /** @@ -425,7 +425,7 @@ class ArticleController extends CController return $isTrue ? $this->response->success([], 'success...') - : $this->response->fail('编辑失败...'); + : $this->response->fail('编辑失败!'); } /** @@ -446,7 +446,7 @@ class ArticleController extends CController return $isTrue ? $this->response->success([], '笔记删除成功...') - : $this->response->fail('笔记删除失败...'); + : $this->response->fail('笔记删除失败!'); } /** @@ -464,7 +464,7 @@ class ArticleController extends CController $file = $this->request->file('annex'); if (!$file->isValid()) { - return $this->response->fail('上传文件验证失败...'); + return $this->response->fail('上传文件验证失败!'); } $annex = [ @@ -486,7 +486,7 @@ class ArticleController extends CController $annex['save_dir'] = $path; $annex['id'] = $this->articleService->insertArticleAnnex($this->uid(), (int)$params['article_id'], $annex); if (!$annex['id']) { - return $this->response->fail('附件上传失败,请稍后再试...'); + return $this->response->fail('附件上传失败,请稍后再试!'); } return $this->response->success($annex, '笔记附件上传成功...'); @@ -509,7 +509,7 @@ class ArticleController extends CController return $isTrue ? $this->response->success([], '笔记附件删除成功...') - : $this->response->fail('笔记附件删除失败...'); + : $this->response->fail('笔记附件删除失败!'); } /** @@ -529,7 +529,7 @@ class ArticleController extends CController return $isTrue ? $this->response->success([], '笔记附件恢复成功...') - : $this->response->fail('笔记附件恢复失败...'); + : $this->response->fail('笔记附件恢复失败!'); } /** @@ -575,6 +575,6 @@ class ArticleController extends CController return $isTrue ? $this->response->success([], '笔记附件删除成功...') - : $this->response->fail('笔记附件删除失败...'); + : $this->response->fail('笔记附件删除失败!'); } } diff --git a/app/Controller/Api/V1/AuthController.php b/app/Controller/Api/V1/AuthController.php index dcb6baf..fc5fb04 100644 --- a/app/Controller/Api/V1/AuthController.php +++ b/app/Controller/Api/V1/AuthController.php @@ -59,7 +59,7 @@ class AuthController extends CController $userInfo = $this->userService->login($params['mobile'], $params['password']); if (!$userInfo) { - return $this->response->fail('账号不存在或密码填写错误...'); + return $this->response->fail('账号不存在或密码填写错误!'); } try { @@ -68,7 +68,7 @@ class AuthController extends CController 'platform' => $params['platform'], ]); } catch (\Exception $exception) { - return $this->response->error('登录异常,请稍后再试...'); + return $this->response->error('登录异常,请稍后再试!'); } return $this->response->success([ @@ -114,7 +114,7 @@ class AuthController extends CController ]); if (!$this->smsCodeService->check('user_register', $params['mobile'], $params['sms_code'])) { - return $this->response->fail('验证码填写错误...'); + return $this->response->fail('验证码填写错误!'); } $isTrue = $this->userService->register([ @@ -124,7 +124,7 @@ class AuthController extends CController ]); if (!$isTrue) { - return $this->response->fail('账号注册失败...'); + return $this->response->fail('账号注册失败!'); } // 删除验证码缓存 @@ -147,12 +147,12 @@ class AuthController extends CController ]); if (!$this->smsCodeService->check('forget_password', $params['mobile'], $params['sms_code'])) { - return $this->response->fail('验证码填写错误'); + return $this->response->fail('验证码填写错误!'); } $isTrue = $this->userService->resetPassword($params['mobile'], $params['password']); if (!$isTrue) { - return $this->response->fail('重置密码失败'); + return $this->response->fail('重置密码失败!'); } // 删除验证码缓存 @@ -189,16 +189,16 @@ class AuthController extends CController ]); if (!$this->smsCodeService->isUsages($params['type'])) { - return $this->response->fail('验证码发送失败...'); + return $this->response->fail('验证码发送失败!'); } if ($params['type'] == 'forget_password') { if (!User::where('mobile', $params['mobile'])->value('id')) { - return $this->response->fail('手机号未被注册使用...'); + return $this->response->fail('手机号未被注册使用!'); } } else if ($params['type'] == 'change_mobile' || $params['type'] == 'user_register') { if (User::where('mobile', $params['mobile'])->value('id')) { - return $this->response->fail('手机号已被他(她)人注册...'); + return $this->response->fail('手机号已被他(她)人注册!'); } } @@ -206,7 +206,7 @@ class AuthController extends CController [$isTrue, $result] = $this->smsCodeService->send($params['type'], $params['mobile']); if (!$isTrue) { // ... 处理发送失败逻辑,当前默认发送成功 - return $this->response->fail('验证码发送失败'); + return $this->response->fail('验证码发送失败!'); } // 测试环境下直接返回验证码 diff --git a/app/Controller/Api/V1/CController.php b/app/Controller/Api/V1/CController.php index 48a8cf3..44af527 100644 --- a/app/Controller/Api/V1/CController.php +++ b/app/Controller/Api/V1/CController.php @@ -34,8 +34,6 @@ class CController extends AbstractController */ public function uid() { - $data = $this->request->getAttribute('auth_data'); - - return $data['user_id'] ?? 0; + return $this->request->getAttribute('auth_data')['user_id'] ?? 0; } } diff --git a/app/Controller/Api/V1/ContactsController.php b/app/Controller/Api/V1/ContactsController.php index 36c320a..dc2f52c 100644 --- a/app/Controller/Api/V1/ContactsController.php +++ b/app/Controller/Api/V1/ContactsController.php @@ -17,7 +17,6 @@ use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\Middleware; use App\Middleware\JWTAuthMiddleware; use Psr\Http\Message\ResponseInterface; -use Hyperf\Amqp\Producer; use App\Amqp\Producer\ChatMessageProducer; use App\Service\ContactsService; use App\Service\SocketClientService; @@ -48,12 +47,6 @@ class ContactsController extends CController */ private $socketClientService; - /** - * @Inject - * @var Producer - */ - private $producer; - /** * 获取用户联系人列表 * @RequestMapping(path="list", methods="get") @@ -91,12 +84,12 @@ class ContactsController extends CController $user = $userService->findById($params['friend_id']); if (!$user) { - return $this->response->fail('用户不存在...'); + return $this->response->fail('用户不存在!'); } $user_id = $this->uid(); if (!$this->contactsService->addContact($user_id, intval($params['friend_id']), $params['remarks'])) { - return $this->response->fail('添加好友申请失败...'); + return $this->response->fail('添加好友申请失败!'); } // 好友申请未读消息数自增 @@ -104,15 +97,13 @@ class ContactsController extends CController // 判断对方是否在线。如果在线发送消息通知 if ($this->socketClientService->isOnlineAll(intval($params['friend_id']))) { - $this->producer->produce( - new ChatMessageProducer(SocketConstants::EVENT_FRIEND_APPLY, [ - 'sender' => $user_id, - 'receive' => intval($params['friend_id']), - 'type' => 1, - 'status' => 1, - 'remark' => '' - ]) - ); + push_amqp(new ChatMessageProducer(SocketConstants::EVENT_FRIEND_APPLY, [ + 'sender' => $user_id, + 'receive' => intval($params['friend_id']), + 'type' => 1, + 'status' => 1, + 'remark' => '' + ])); } return $this->response->success([], '发送好友申请成功...'); @@ -133,7 +124,7 @@ class ContactsController extends CController $user_id = $this->uid(); if (!$this->contactsService->deleteContact($user_id, intval($params['friend_id']))) { - return $this->response->fail('好友关系解除失败...'); + return $this->response->fail('好友关系解除失败!'); } // 删除好友会话列表 @@ -162,7 +153,7 @@ class ContactsController extends CController $user_id = $this->uid(); $isTrue = $this->contactsService->acceptInvitation($user_id, intval($params['apply_id']), $params['remarks']); if (!$isTrue) { - return $this->response->fail('处理失败...'); + return $this->response->fail('处理失败!'); } $friend_id = $info = UsersFriendsApply::where('id', $params['apply_id']) @@ -171,16 +162,14 @@ class ContactsController extends CController // 判断对方是否在线。如果在线发送消息通知 if ($this->socketClientService->isOnlineAll($friend_id)) { - // 待完善 - $this->producer->produce( - new ChatMessageProducer(SocketConstants::EVENT_FRIEND_APPLY, [ - 'sender' => $user_id, - 'receive' => $friend_id, - 'type' => 1, - 'status' => 1, - 'remark' => '' - ]) - ); + // TODO 待完善 + push_amqp(new ChatMessageProducer(SocketConstants::EVENT_FRIEND_APPLY, [ + 'sender' => $user_id, + 'receive' => $friend_id, + 'type' => 1, + 'status' => 1, + 'remark' => '' + ])); } return $this->response->success([], '处理成功...'); @@ -300,7 +289,7 @@ class ContactsController extends CController $user_id = $this->uid(); $isTrue = $this->contactsService->editContactRemark($user_id, intval($params['friend_id']), $params['remarks']); if (!$isTrue) { - return $this->response->fail('备注修改失败...'); + return $this->response->fail('备注修改失败!'); } FriendRemarkCache::set($user_id, intval($params['friend_id']), $params['remarks']); diff --git a/app/Controller/Api/V1/DownloadController.php b/app/Controller/Api/V1/DownloadController.php index cd357df..be6d15f 100644 --- a/app/Controller/Api/V1/DownloadController.php +++ b/app/Controller/Api/V1/DownloadController.php @@ -48,7 +48,7 @@ class DownloadController extends CController $recordsInfo = ChatRecord::select(['msg_type', 'source', 'user_id', 'receive_id'])->where('id', $params['cr_id'])->first(); if (!$recordsInfo) { - return $this->response->fail('文件不存在...'); + return $this->response->fail('文件不存在!'); } $user_id = $this->uid(); @@ -57,18 +57,18 @@ class DownloadController extends CController if ($recordsInfo->user_id != $user_id) { if ($recordsInfo->source == 1) { if ($recordsInfo->receive_id != $user_id) { - return $this->response->fail('非法请求...'); + return $this->response->fail('非法请求!'); } } else { if (!Group::isMember($recordsInfo->receive_id, $user_id)) { - return $this->response->fail('非法请求...'); + return $this->response->fail('非法请求!'); } } } $fileInfo = ChatRecordsFile::select(['save_dir', 'original_name'])->where('record_id', $params['cr_id'])->first(); if (!$fileInfo || !file_exists($uploadService->driver($fileInfo->save_dir))) { - return $this->response->fail('文件不存在或没有下载权限...'); + return $this->response->fail('文件不存在或没有下载权限!'); } return $response->download($uploadService->driver($fileInfo->save_dir), $fileInfo->original_name); @@ -95,7 +95,7 @@ class DownloadController extends CController ->first(); if (!$info || !file_exists($uploadService->driver($info->save_dir))) { - return $this->response->fail('文件不存在或没有下载权限...'); + return $this->response->fail('文件不存在或没有下载权限!'); } return $response->download($uploadService->driver($info->save_dir), $info->original_name); diff --git a/app/Controller/Api/V1/EmoticonController.php b/app/Controller/Api/V1/EmoticonController.php index e80943f..7c152a1 100644 --- a/app/Controller/Api/V1/EmoticonController.php +++ b/app/Controller/Api/V1/EmoticonController.php @@ -111,7 +111,7 @@ class EmoticonController extends CController // 移除表情包 $isTrue = $this->emoticonService->removeSysEmoticon($user_id, $params['emoticon_id']); if (!$isTrue) { - return $this->response->fail('移除表情包失败...'); + return $this->response->fail('移除表情包失败!'); } return $this->response->success([], '移除表情包成功...'); @@ -119,11 +119,11 @@ class EmoticonController extends CController // 添加表情包 $emoticonInfo = Emoticon::where('id', $params['emoticon_id'])->first(['id', 'name', 'url']); if (!$emoticonInfo) { - return $this->response->fail('添加表情包失败...'); + return $this->response->fail('添加表情包失败!'); } if (!$this->emoticonService->installSysEmoticon($user_id, $params['emoticon_id'])) { - return $this->response->fail('添加表情包失败...'); + return $this->response->fail('添加表情包失败!'); } $data = [ @@ -151,7 +151,7 @@ class EmoticonController extends CController $file = $this->request->file('emoticon'); if (!$file->isValid()) { return $this->response->fail( - '图片上传失败,请稍后再试...', + '图片上传失败,请稍后再试!', ResponseCode::VALIDATION_ERROR ); } @@ -167,12 +167,12 @@ class EmoticonController extends CController // 读取图片信息 $imgInfo = @getimagesize($file->getRealPath()); if (!$imgInfo) { - return $this->response->fail('表情包上传失败...'); + return $this->response->fail('表情包上传失败!'); } $save_path = $uploadService->media($file, 'media/images/emoticon', create_image_name($ext, $imgInfo[0], $imgInfo[1])); if (!$save_path) { - return $this->response->fail('图片上传失败'); + return $this->response->fail('图片上传失败!'); } $result = EmoticonDetail::create([ @@ -184,7 +184,7 @@ class EmoticonController extends CController ]); if (!$result) { - return $this->response->fail('表情包上传失败...'); + return $this->response->fail('表情包上传失败!'); } return $this->response->success([ @@ -209,7 +209,7 @@ class EmoticonController extends CController [$isTrue, $data] = $this->emoticonService->collect($this->uid(), $params['record_id']); if (!$isTrue) { - return $this->response->fail('添加表情失败'); + return $this->response->fail('添加表情失败!'); } return $this->response->success([ diff --git a/app/Controller/Api/V1/GroupController.php b/app/Controller/Api/V1/GroupController.php index f16b0fd..531444b 100644 --- a/app/Controller/Api/V1/GroupController.php +++ b/app/Controller/Api/V1/GroupController.php @@ -15,7 +15,6 @@ use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\Middleware; use App\Middleware\JWTAuthMiddleware; -use Hyperf\Amqp\Producer; use App\Model\UsersFriend; use App\Model\UsersChatList; use App\Model\Group\Group; @@ -42,12 +41,6 @@ class GroupController extends CController */ private $groupService; - /** - * @Inject - * @var Producer - */ - private $producer; - /** * @Inject * @var SocketRoomService @@ -78,7 +71,7 @@ class GroupController extends CController ], $friend_ids); if (!$isTrue) { - return $this->response->fail('创建群聊失败,请稍后再试...'); + return $this->response->fail('创建群聊失败,请稍后再试!'); } // 加入聊天室 @@ -88,14 +81,12 @@ class GroupController extends CController } // ... 消息推送队列 - $this->producer->produce( - new ChatMessageProducer(SocketConstants::EVENT_TALK, [ - 'sender' => $user_id, // 发送者ID - 'receive' => (int)$data['group_id'], // 接收者ID - 'source' => 2, // 接收者类型[1:好友;2:群组;] - 'record_id' => (int)$data['record_id'] - ]) - ); + push_amqp(new ChatMessageProducer(SocketConstants::EVENT_TALK, [ + 'sender' => $user_id, // 发送者ID + 'receive' => (int)$data['group_id'], // 接收者ID + 'source' => 2, // 接收者类型[1:好友;2:群组;] + 'record_id' => (int)$data['record_id'] + ])); return $this->response->success([ 'group_id' => $data['group_id'] @@ -117,7 +108,7 @@ class GroupController extends CController $isTrue = $this->groupService->dismiss($params['group_id'], $this->uid()); if (!$isTrue) { - return $this->response->fail('群组解散失败...'); + return $this->response->fail('群组解散失败!'); } $this->socketRoomService->delRoom($params['group_id']); @@ -146,7 +137,7 @@ class GroupController extends CController $user_id = $this->uid(); [$isTrue, $record_id] = $this->groupService->invite($user_id, $params['group_id'], $uids); if (!$isTrue) { - return $this->response->fail('邀请好友加入群聊失败...'); + return $this->response->fail('邀请好友加入群聊失败!'); } // 加入聊天室 @@ -154,15 +145,13 @@ class GroupController extends CController $this->socketRoomService->addRoomMember($uid, $params['group_id']); } - // ...消息推送队列 - $this->producer->produce( - new ChatMessageProducer(SocketConstants::EVENT_TALK, [ - 'sender' => $user_id, // 发送者ID - 'receive' => (int)$params['group_id'], // 接收者ID - 'source' => 2, // 接收者类型[1:好友;2:群组;] - 'record_id' => $record_id - ]) - ); + // 消息推送队列 + push_amqp(new ChatMessageProducer(SocketConstants::EVENT_TALK, [ + 'sender' => $user_id, // 发送者ID + 'receive' => (int)$params['group_id'], // 接收者ID + 'source' => 2, // 接收者类型[1:好友;2:群组;] + 'record_id' => $record_id + ])); return $this->response->success([], '好友已成功加入群聊...'); } @@ -183,21 +172,19 @@ class GroupController extends CController $user_id = $this->uid(); [$isTrue, $record_id] = $this->groupService->quit($user_id, $params['group_id']); if (!$isTrue) { - return $this->response->fail('退出群组失败...'); + return $this->response->fail('退出群组失败!'); } // 移出聊天室 $this->socketRoomService->delRoomMember($params['group_id'], $user_id); - // ...消息推送队列 - $this->producer->produce( - new ChatMessageProducer(SocketConstants::EVENT_TALK, [ - 'sender' => $user_id, // 发送者ID - 'receive' => (int)$params['group_id'], // 接收者ID - 'source' => 2, // 接收者类型[1:好友;2:群组;] - 'record_id' => $record_id - ]) - ); + // 消息推送队列 + push_amqp(new ChatMessageProducer(SocketConstants::EVENT_TALK, [ + 'sender' => $user_id, // 发送者ID + 'receive' => (int)$params['group_id'], // 接收者ID + 'source' => 2, // 接收者类型[1:好友;2:群组;] + 'record_id' => $record_id + ])); return $this->response->success([], '已成功退出群组...'); } @@ -228,7 +215,7 @@ class GroupController extends CController return $result ? $this->response->success([], '群组信息修改成功...') - : $this->response->fail('群组信息修改失败...'); + : $this->response->fail('群组信息修改失败!'); } /** @@ -249,12 +236,12 @@ class GroupController extends CController $user_id = $this->uid(); if (in_array($user_id, $params['members_ids'])) { - return $this->response->fail('群聊用户移除失败...'); + return $this->response->fail('群聊用户移除失败!'); } [$isTrue, $record_id] = $this->groupService->removeMember($params['group_id'], $user_id, $params['members_ids']); if (!$isTrue) { - return $this->response->fail('群聊用户移除失败...'); + return $this->response->fail('群聊用户移除失败!'); } // 移出聊天室 @@ -262,15 +249,13 @@ class GroupController extends CController $this->socketRoomService->delRoomMember($params['group_id'], $uid); } - // ... 消息推送队列 - $this->producer->produce( - new ChatMessageProducer(SocketConstants::EVENT_TALK, [ - 'sender' => $user_id, // 发送者ID - 'receive' => (int)$params['group_id'], // 接收者ID - 'source' => 2, // 接收者类型[1:好友;2:群组;] - 'record_id' => $record_id - ]) - ); + // 消息推送队列 + push_amqp(new ChatMessageProducer(SocketConstants::EVENT_TALK, [ + 'sender' => $user_id, // 发送者ID + 'receive' => (int)$params['group_id'], // 接收者ID + 'source' => 2, // 接收者类型[1:好友;2:群组;] + 'record_id' => $record_id + ])); return $this->response->success([], '已成功退出群组...'); } @@ -340,7 +325,7 @@ class GroupController extends CController return $isTrue ? $this->response->success([], '群名片修改成功...') - : $this->response->error('群名片修改失败...'); + : $this->response->error('群名片修改失败!'); } /** @@ -392,7 +377,7 @@ class GroupController extends CController // 判断用户是否是群成员 if (!Group::isMember($group_id, $user_id)) { - return $this->response->fail('非法操作...'); + return $this->response->fail('非法操作!'); } $members = GroupMember::select([ @@ -427,7 +412,7 @@ class GroupController extends CController // 判断用户是否是群成员 if (!Group::isMember($group_id, $user_id)) { - return $this->response->fail('非管理员禁止操作...'); + return $this->response->fail('非管理员禁止操作!'); } $rows = GroupNotice::leftJoin('users', 'users.id', '=', 'group_notice.creator_id') @@ -476,7 +461,7 @@ class GroupController extends CController // 判断用户是否是管理员 if (!Group::isManager($user_id, $params['group_id'])) { - return $this->response->fail('非管理员禁止操作...'); + return $this->response->fail('非管理员禁止操作!'); } // 判断是否是新增数据 @@ -493,7 +478,7 @@ class GroupController extends CController ]); if (!$result) { - return $this->response->fail('添加群公告信息失败...'); + return $this->response->fail('添加群公告信息失败!'); } // ... TODO 推送群消息(预留) @@ -510,7 +495,7 @@ class GroupController extends CController return $result ? $this->response->success([], '修改群公告信息成功...') - : $this->response->fail('修改群公告信息成功...'); + : $this->response->fail('修改群公告信息失败!'); } /** @@ -531,7 +516,7 @@ class GroupController extends CController // 判断用户是否是管理员 if (!Group::isManager($user_id, $params['group_id'])) { - return $this->response->fail('非法操作...'); + return $this->response->fail('非法操作!'); } $result = GroupNotice::where('id', $params['notice_id']) @@ -543,6 +528,6 @@ class GroupController extends CController return $result ? $this->response->success([], '公告删除成功...') - : $this->response->fail('公告删除失败...'); + : $this->response->fail('公告删除失败!'); } } diff --git a/app/Controller/Api/V1/TalkController.php b/app/Controller/Api/V1/TalkController.php index 7b07668..17d8a5c 100644 --- a/app/Controller/Api/V1/TalkController.php +++ b/app/Controller/Api/V1/TalkController.php @@ -15,7 +15,6 @@ use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\Middleware; use App\Middleware\JWTAuthMiddleware; -use Hyperf\Amqp\Producer; use Hyperf\Utils\Str; use Psr\Http\Message\ResponseInterface; use App\Model\EmoticonDetail; @@ -52,12 +51,6 @@ class TalkController extends CController */ public $unreadTalkCache; - /** - * @Inject - * @var Producer - */ - private $producer; - /** * 获取用户对话列表 * @RequestMapping(path="list", methods="get") @@ -98,17 +91,17 @@ class TalkController extends CController $user_id = $this->uid(); if ($params['type'] == 1) { if (!UsersFriend::isFriend($user_id, $params['receive_id'])) { - return $this->response->fail('暂不属于好友关系,无法进行聊天...'); + return $this->response->fail('暂不属于好友关系,无法进行聊天!'); } } else { if (!Group::isMember($params['receive_id'], $user_id)) { - return $this->response->fail('暂不属于群成员,无法进行群聊...'); + return $this->response->fail('暂不属于群成员,无法进行群聊!'); } } $result = UsersChatList::addItem($user_id, $params['receive_id'], $params['type']); if (!$result) { - return $this->response->fail('创建失败...'); + return $this->response->fail('创建失败!'); } $data = [ @@ -164,7 +157,7 @@ class TalkController extends CController return UsersChatList::delItem($this->uid(), $params['list_id']) ? $this->response->success([], '对话列表删除成功...') - : $this->response->fail('对话列表删除失败...'); + : $this->response->fail('对话列表删除失败!'); } /** @@ -183,7 +176,7 @@ class TalkController extends CController return UsersChatList::topItem($this->uid(), $params['list_id'], $params['type'] == 1) ? $this->response->success([], '对话列表置顶(或取消置顶)成功...') - : $this->response->fail('对话列表置顶(或取消置顶)失败...'); + : $this->response->fail('对话列表置顶(或取消置顶)失败!'); } /** @@ -205,7 +198,7 @@ class TalkController extends CController return $isTrue ? $this->response->success([], '免打扰设置成功...') - : $this->response->fail('免打扰设置失败...'); + : $this->response->fail('免打扰设置失败!'); } /** @@ -227,7 +220,7 @@ class TalkController extends CController $this->unreadTalkCache->del($this->uid(), $params['receive']); } - return $this->response->success([], 'success'); + return $this->response->success(); } /** @@ -245,11 +238,9 @@ class TalkController extends CController [$isTrue, $message,] = $this->talkService->revokeRecord($this->uid(), $params['record_id']); if ($isTrue) { - $this->producer->produce( - new ChatMessageProducer(SocketConstants::EVENT_REVOKE_TALK, [ - 'record_id' => $params['record_id'] - ]) - ); + push_amqp(new ChatMessageProducer(SocketConstants::EVENT_REVOKE_TALK, [ + 'record_id' => $params['record_id'] + ])); } return $isTrue @@ -283,7 +274,7 @@ class TalkController extends CController return $isTrue ? $this->response->success([], '删除成功...') - : $this->response->fail('删除失败...'); + : $this->response->fail('删除失败!'); } /** @@ -333,7 +324,7 @@ class TalkController extends CController } if (!$ids) { - return $this->response->fail('转发失败...'); + return $this->response->fail('转发失败!'); } if ($receive_user_ids) { @@ -342,16 +333,14 @@ class TalkController extends CController } } - // ... 消息推送队列 + // 消息推送队列 foreach ($ids as $value) { - $this->producer->produce( - new ChatMessageProducer(SocketConstants::EVENT_TALK, [ - 'sender' => $user_id, // 发送者ID - 'receive' => intval($value['receive_id']), // 接收者ID - 'source' => intval($value['source']), // 接收者类型 1:好友;2:群组 - 'record_id' => $value['record_id'] - ]) - ); + push_amqp(new ChatMessageProducer(SocketConstants::EVENT_TALK, [ + 'sender' => $user_id, // 发送者ID + 'receive' => intval($value['receive_id']), // 接收者ID + 'source' => intval($value['source']), // 接收者类型 1:好友;2:群组 + 'record_id' => $value['record_id'] + ])); } return $this->response->success([], '转发成功...'); @@ -381,7 +370,7 @@ class TalkController extends CController 'rows' => [], 'record_id' => 0, 'limit' => $limit - ], '非群聊成员不能查看群聊信息...'); + ], '非群聊成员不能查看群聊信息!'); } $result = $this->talkService->getChatRecords( @@ -442,7 +431,7 @@ class TalkController extends CController 'rows' => [], 'record_id' => 0, 'limit' => $limit - ], '非群聊成员不能查看群聊信息...'); + ], '非群聊成员不能查看群聊信息!'); } if (in_array($params['msg_type'], [1, 2, 4, 5])) { @@ -540,18 +529,16 @@ class TalkController extends CController ]); if (!$record_id) { - return $this->response->fail('图片上传失败'); + return $this->response->fail('图片上传失败!'); } - // ... 消息推送队列 - $this->producer->produce( - new ChatMessageProducer(SocketConstants::EVENT_TALK, [ - 'sender' => $user_id, // 发送者ID - 'receive' => intval($params['receive_id']), // 接收者ID - 'source' => intval($params['source']), // 接收者类型[1:好友;2:群组;] - 'record_id' => $record_id - ]) - ); + // 消息推送队列 + push_amqp(new ChatMessageProducer(SocketConstants::EVENT_TALK, [ + 'sender' => $user_id, // 发送者ID + 'receive' => intval($params['receive_id']), // 接收者ID + 'source' => intval($params['source']), // 接收者类型[1:好友;2:群组;] + 'record_id' => $record_id + ])); LastMsgCache::set([ 'text' => '[图片消息]', @@ -590,18 +577,16 @@ class TalkController extends CController ]); if (!$record_id) { - return $this->response->fail('消息发送失败'); + return $this->response->fail('消息发送失败!'); } - // ...消息推送队列 - $this->producer->produce( - new ChatMessageProducer(SocketConstants::EVENT_TALK, [ - 'sender' => $user_id, // 发送者ID - 'receive' => intval($params['receive_id']), // 接收者ID - 'source' => intval($params['source']), // 接收者类型[1:好友;2:群组;] - 'record_id' => $record_id - ]) - ); + // 消息推送队列 + push_amqp(new ChatMessageProducer(SocketConstants::EVENT_TALK, [ + 'sender' => $user_id, // 发送者ID + 'receive' => intval($params['receive_id']), // 接收者ID + 'source' => intval($params['source']), // 接收者类型[1:好友;2:群组;] + 'record_id' => $record_id + ])); LastMsgCache::set([ 'text' => '[代码消息]', @@ -657,18 +642,16 @@ class TalkController extends CController ]); if (!$record_id) { - return $this->response->fail('表情发送失败'); + return $this->response->fail('表情发送失败!'); } - // ... 消息推送队列 - $this->producer->produce( - new ChatMessageProducer(SocketConstants::EVENT_TALK, [ - 'sender' => $user_id, // 发送者ID - 'receive' => intval($params['receive_id']), // 接收者ID - 'source' => intval($params['source']), // 接收者类型[1:好友;2:群组;] - 'record_id' => $record_id - ]) - ); + // 消息推送队列 + push_amqp(new ChatMessageProducer(SocketConstants::EVENT_TALK, [ + 'sender' => $user_id, // 发送者ID + 'receive' => intval($params['receive_id']), // 接收者ID + 'source' => intval($params['source']), // 接收者类型[1:好友;2:群组;] + 'record_id' => $record_id + ])); LastMsgCache::set([ 'text' => '[文件消息]', @@ -699,7 +682,7 @@ class TalkController extends CController ]); if (!$emoticon) { - return $this->response->fail('表情不存在...'); + return $this->response->fail('表情不存在!'); } $record_id = $this->talkService->createEmoticonMessage([ @@ -717,18 +700,16 @@ class TalkController extends CController ]); if (!$record_id) { - return $this->response->fail('表情发送失败'); + return $this->response->fail('表情发送失败!'); } - // ... 消息推送队列 - $this->producer->produce( - new ChatMessageProducer(SocketConstants::EVENT_TALK, [ - 'sender' => $user_id, // 发送者ID - 'receive' => intval($params['receive_id']), // 接收者ID - 'source' => intval($params['source']), // 接收者类型[1:好友;2:群组;] - 'record_id' => $record_id - ]) - ); + // 消息推送队列 + push_amqp(new ChatMessageProducer(SocketConstants::EVENT_TALK, [ + 'sender' => $user_id, // 发送者ID + 'receive' => intval($params['receive_id']), // 接收者ID + 'source' => intval($params['source']), // 接收者类型[1:好友;2:群组;] + 'record_id' => $record_id + ])); LastMsgCache::set([ 'text' => '[表情包消息]', diff --git a/app/Controller/Api/V1/UploadController.php b/app/Controller/Api/V1/UploadController.php index fcd7604..dd4ee38 100644 --- a/app/Controller/Api/V1/UploadController.php +++ b/app/Controller/Api/V1/UploadController.php @@ -56,7 +56,7 @@ class UploadController extends CController $path = '/media/images/avatar/' . date('Ymd') . '/' . uniqid() . date('His') . '.png'; $this->uploadService->makeDirectory($this->uploadService->driver('/media/images/avatar/' . date('Ymd') . '/')); @file_put_contents($this->uploadService->driver($path), $data); - + return $this->response->success(['avatar' => get_media_url($path)]); } @@ -76,7 +76,7 @@ class UploadController extends CController $data = $this->splitUploadService->create($this->uid(), $params['file_name'], $params['file_size']); - return $data ? $this->response->success($data) : $this->response->fail('获取文件拆分信息失败...'); + return $data ? $this->response->success($data) : $this->response->fail('获取文件拆分信息失败!'); } /** @@ -105,13 +105,13 @@ class UploadController extends CController $user_id = $this->uid(); $uploadRes = $this->splitUploadService->upload($user_id, $file, $params['hash'], intval($params['split_index']), intval($params['size'])); if (!$uploadRes) { - return $this->response->fail('上传文件失败...'); + return $this->response->fail('上传文件失败!'); } if (($params['split_index'] + 1) == $params['split_num']) { $fileInfo = $this->splitUploadService->merge($user_id, $params['hash']); if (!$fileInfo) { - return $this->response->fail('上传文件失败...'); + return $this->response->fail('上传文件失败!'); } return $this->response->success([ diff --git a/app/Controller/Api/V1/UsersController.php b/app/Controller/Api/V1/UsersController.php index 5439599..5b989fb 100644 --- a/app/Controller/Api/V1/UsersController.php +++ b/app/Controller/Api/V1/UsersController.php @@ -105,7 +105,7 @@ class UsersController extends CController return $isTrue ? $this->response->success([], '个人信息修改成功...') - : $this->response->fail('个人信息修改失败...'); + : $this->response->fail('个人信息修改失败!'); } /** @@ -125,7 +125,7 @@ class UsersController extends CController return $isTrue ? $this->response->success([], '头像修改成功...') - : $this->response->fail('头像修改失败...'); + : $this->response->fail('头像修改失败!'); } /** @@ -143,7 +143,7 @@ class UsersController extends CController return $this->response->success($data); } - return $this->response->fail('查询失败...'); + return $this->response->fail('用户查询失败!'); } /** @@ -164,13 +164,13 @@ class UsersController extends CController // 验证密码是否正确 if (!Hash::check($this->request->post('old_password'), $userInfo->password)) { - return $this->response->fail('旧密码验证失败...'); + return $this->response->fail('旧密码验证失败!'); } $isTrue = $this->userService->resetPassword($userInfo->mobile, $params['new_password']); return $isTrue ? $this->response->success([], '密码修改成功...') - : $this->response->fail('密码修改失败...'); + : $this->response->fail('密码修改失败!'); } /** @@ -190,17 +190,17 @@ class UsersController extends CController ]); if (!$smsCodeService->check('change_mobile', $params['mobile'], $params['sms_code'])) { - return $this->response->fail('验证码填写错误...'); + return $this->response->fail('验证码填写错误!'); } $user_id = $this->uid(); if (!Hash::check($params['password'], User::where('id', $user_id)->value('password'))) { - return $this->response->fail('账号密码验证失败...'); + return $this->response->fail('账号密码验证失败!'); } [$isTrue,] = $this->userService->changeMobile($user_id, $params['mobile']); if (!$isTrue) { - return $this->response->fail('手机号更换失败...'); + return $this->response->fail('手机号更换失败!'); } // 清除缓存信息 @@ -226,18 +226,18 @@ class UsersController extends CController $sendEmailCode = new SendEmailCode(); if (!$sendEmailCode->check(SendEmailCode::CHANGE_EMAIL, $params['email'], $params['email_code'])) { - return $this->response->fail('验证码填写错误...'); + 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('账号密码验证失败...'); + return $this->response->fail('账号密码验证失败!'); } $isTrue = User::where('id', $uid)->update(['email' => $params['email']]); if (!$isTrue) { - return $this->response->fail('邮箱设置失败...'); + return $this->response->fail('邮箱设置失败!'); } $sendEmailCode->delCode(SendEmailCode::CHANGE_EMAIL, $params['email']); @@ -261,18 +261,18 @@ class UsersController extends CController $user_id = $this->uid(); if (in_array($user_id, [2054, 2055])) { - return $this->response->fail('测试账号不支持修改手机号...'); + return $this->response->fail('测试账号不支持修改手机号!'); } if (User::where('mobile', $params['mobile'])->exists()) { - return $this->response->fail('手机号已被他人注册...'); + return $this->response->fail('手机号已被他人注册!'); } $data = ['is_debug' => true]; [$isTrue, $result] = $smsCodeService->send('change_mobile', $params['mobile']); if (!$isTrue) { // ... 处理发送失败逻辑,当前默认发送成功 - return $this->response->fail('验证码发送失败'); + return $this->response->fail('验证码发送失败!'); } // 测试环境下直接返回验证码 @@ -297,7 +297,7 @@ class UsersController extends CController $isTrue = $sendEmailCode->send(SendEmailCode::CHANGE_EMAIL, '绑定邮箱', $params['email']); if (!$isTrue) { - return $this->response->fail('验证码发送失败...'); + return $this->response->fail('验证码发送失败!'); } return $this->response->success([], '验证码发送成功...'); diff --git a/app/Controller/WebSocketController.php b/app/Controller/WebSocketController.php index 50034fc..91d6e3a 100644 --- a/app/Controller/WebSocketController.php +++ b/app/Controller/WebSocketController.php @@ -16,7 +16,6 @@ use App\Constants\SocketConstants; use Hyperf\Contract\OnCloseInterface; use Hyperf\Contract\OnMessageInterface; use Hyperf\Contract\OnOpenInterface; -use Hyperf\Amqp\Producer; use Swoole\Http\Request; use Swoole\Websocket\Frame; use Swoole\Http\Response; @@ -27,7 +26,6 @@ use App\Service\MessageHandleService; use App\Service\SocketRoomService; use App\Model\Group\GroupMember; use App\Amqp\Producer\ChatMessageProducer; -use App\Support\SocketIOParser; /** * Class WebSocketController @@ -42,12 +40,6 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos */ private $jwt; - /** - * @Inject - * @var Producer - */ - private $producer; - /** * @inject * @var SocketClientService @@ -91,7 +83,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos // 若开启单点登录,则主动关闭之前登录的连接 if ($isOnline) { - // ... 预留 + // TODO 预留 } // 绑定fd与用户关系 @@ -105,13 +97,11 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos if (!$isOnline) { // 推送消息至队列 - $this->producer->produce( - new ChatMessageProducer(SocketConstants::EVENT_ONLINE_STATUS, [ - 'user_id' => $userInfo['user_id'], - 'status' => 1, - 'notify' => '好友上线通知...' - ]) - ); + push_amqp(new ChatMessageProducer(SocketConstants::EVENT_ONLINE_STATUS, [ + 'user_id' => $userInfo['user_id'], + 'status' => 1, + 'notify' => '好友上线通知...' + ])); } } @@ -126,21 +116,14 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos // 判断是否为心跳检测 if ($frame->data == 'PING') return; - //$result = SocketIOParser::decode($frame->data); $result = json_decode($frame->data, true); - if (!isset(self::EVENTS[$result['event']])) { - return; - } + if (!isset(self::EVENTS[$result['event']])) return; // 回调事件处理函数 call_user_func_array([ $this->messageHandleService, self::EVENTS[$result['event']] - ], [ - $server, - $frame, - $result['data'] - ]); + ], [$server, $frame, $result['data']]); } /** @@ -152,7 +135,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos */ public function onClose($server, int $fd, int $reactorId): void { - $user_id = $this->socketClientService->findFdUserId($fd); + $user_id = (int)$this->socketClientService->findFdUserId($fd); stdout_log()->notice("客户端FD:{$fd} 已关闭连接 ,用户ID为【{$user_id}】,关闭时间:" . date('Y-m-d H:i:s')); @@ -160,16 +143,14 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos $this->socketClientService->removeRelation($fd); // 判断是否存在异地登录 - $isOnline = $this->socketClientService->isOnlineAll(intval($user_id)); + $isOnline = $this->socketClientService->isOnlineAll($user_id); if (!$isOnline) { // 推送消息至队列 - $this->producer->produce( - new ChatMessageProducer(SocketConstants::EVENT_ONLINE_STATUS, [ - 'user_id' => $user_id, - 'status' => 0, - 'notify' => '好友离线通知通知...' - ]) - ); + push_amqp(new ChatMessageProducer(SocketConstants::EVENT_ONLINE_STATUS, [ + 'user_id' => $user_id, + 'status' => 0, + 'notify' => '好友离线通知通知...' + ])); } } } diff --git a/app/Exception/ResponseException.php b/app/Exception/ResponseException.php new file mode 100644 index 0000000..f6001a9 --- /dev/null +++ b/app/Exception/ResponseException.php @@ -0,0 +1,14 @@ +producer->produce( - new ChatMessageProducer('event_talk', [ - 'sender' => intval($data['send_user']), // 发送者ID - 'receive' => intval($data['receive_user']), // 接收者ID - 'source' => intval($data['source_type']), // 接收者类型[1:好友;2:群组;] - 'record_id' => $result->id - ]) - ); + // 推送消息 + push_amqp(new ChatMessageProducer('event_talk', [ + 'sender' => intval($data['send_user']), // 发送者ID + 'receive' => intval($data['receive_user']), // 接收者ID + 'source' => intval($data['source_type']), // 接收者类型[1:好友;2:群组;] + 'record_id' => $result->id + ])); } /** @@ -111,11 +104,9 @@ class MessageHandleService */ public function onKeyboard($server, Frame $frame, $data) { - $this->producer->produce( - new ChatMessageProducer('event_keyboard', [ - 'send_user' => intval($data['send_user']), // 发送者ID - 'receive_user' => intval($data['receive_user']), // 接收者ID - ]) - ); + push_amqp(new ChatMessageProducer('event_keyboard', [ + 'send_user' => intval($data['send_user']), // 发送者ID + 'receive_user' => intval($data['receive_user']), // 接收者ID + ])); } } diff --git a/app/helper.php b/app/helper.php index 796fe7f..cef6fce 100644 --- a/app/helper.php +++ b/app/helper.php @@ -5,6 +5,7 @@ |-------------------------------------------------------------------------- */ +use Hyperf\Amqp\Message\ProducerMessage; use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\HttpServer\Contract\ResponseInterface; use Hyperf\Logger\LoggerFactory; @@ -15,6 +16,7 @@ use Swoole\Websocket\Frame; use Swoole\WebSocket\Server as WebSocketServer; use Hyperf\Utils\Str; use Hyperf\Redis\Redis; +use Hyperf\Amqp\Producer; /** * 容器实例 @@ -232,3 +234,16 @@ function parse_ids($ids) { return array_unique(explode(',', trim($ids))); } + +/** + * 推送消息至 RabbitMQ 队列 + * + * @param ProducerMessage $message + * @param bool $confirm + * @param int $timeout + * @return bool + */ +function push_amqp(ProducerMessage $message, bool $confirm = false, int $timeout = 5) +{ + return container()->get(Producer::class)->produce($message, $confirm, $timeout); +}