From 93131bde183eb82ff20013ba87d342c72eca0a17 Mon Sep 17 00:00:00 2001 From: gzydong <837215079@qq.com> Date: Tue, 20 Jul 2021 23:12:18 +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 --- ...ediaFileType.php => MediaTypeConstant.php} | 4 +- ...MessageEvent.php => TalkEventConstant.php} | 2 +- .../{TalkMode.php => TalkModeConstant.php} | 2 +- app/Controller/Api/V1/ContactsController.php | 4 +- app/Controller/Api/V1/GroupController.php | 4 +- app/Controller/Api/V1/TalkController.php | 14 +++--- .../Api/V1/TalkMessageController.php | 16 +++---- app/Controller/WebSocketController.php | 9 ++-- app/Event/TalkEvent.php | 30 ++++++++++++ app/Listener/TalkMessageListener.php | 33 +++++++++++++ app/Service/ContactApplyService.php | 8 ++-- app/Service/EmoticonService.php | 4 +- app/Service/GroupService.php | 46 +++++++++---------- app/Service/Message/FormatMessageService.php | 1 + app/Service/Message/ReceiveHandleService.php | 22 ++++----- .../Message/SubscribeHandleService.php | 33 ++++++------- app/Service/TalkListService.php | 10 ++-- app/Service/TalkMessageService.php | 45 +++++++++--------- app/Service/TalkService.php | 32 ++++++------- app/Support/UserRelation.php | 6 +-- 更新计划.md | 11 ----- 21 files changed, 194 insertions(+), 142 deletions(-) rename app/Constants/{MediaFileType.php => MediaTypeConstant.php} (94%) rename app/Constants/{TalkMessageEvent.php => TalkEventConstant.php} (97%) rename app/Constants/{TalkMode.php => TalkModeConstant.php} (93%) create mode 100644 app/Event/TalkEvent.php create mode 100644 app/Listener/TalkMessageListener.php diff --git a/app/Constants/MediaFileType.php b/app/Constants/MediaTypeConstant.php similarity index 94% rename from app/Constants/MediaFileType.php rename to app/Constants/MediaTypeConstant.php index aa4faac..961b18d 100644 --- a/app/Constants/MediaFileType.php +++ b/app/Constants/MediaTypeConstant.php @@ -3,11 +3,11 @@ namespace App\Constants; /** - * Class FileMediaType + * Class MediaTypeConstant * * @package App\Constants */ -class MediaFileType +class MediaTypeConstant { const FILE_IMAGE = 1; //图片文件 const FILE_VIDEO = 2; //视频文件 diff --git a/app/Constants/TalkMessageEvent.php b/app/Constants/TalkEventConstant.php similarity index 97% rename from app/Constants/TalkMessageEvent.php rename to app/Constants/TalkEventConstant.php index d4c3909..3d82f7d 100644 --- a/app/Constants/TalkMessageEvent.php +++ b/app/Constants/TalkEventConstant.php @@ -7,7 +7,7 @@ namespace App\Constants; * * @package App\Constants */ -class TalkMessageEvent +class TalkEventConstant { /** * 对话消息通知 - 事件名 diff --git a/app/Constants/TalkMode.php b/app/Constants/TalkModeConstant.php similarity index 93% rename from app/Constants/TalkMode.php rename to app/Constants/TalkModeConstant.php index adce0cd..4e402b9 100644 --- a/app/Constants/TalkMode.php +++ b/app/Constants/TalkModeConstant.php @@ -7,7 +7,7 @@ namespace App\Constants; * * @package App\Constants */ -class TalkMode +class TalkModeConstant { /** * 私信 diff --git a/app/Controller/Api/V1/ContactsController.php b/app/Controller/Api/V1/ContactsController.php index 245d27c..8fc7a26 100644 --- a/app/Controller/Api/V1/ContactsController.php +++ b/app/Controller/Api/V1/ContactsController.php @@ -10,7 +10,7 @@ namespace App\Controller\Api\V1; -use App\Constants\TalkMode; +use App\Constants\TalkModeConstant; use App\Service\TalkListService; use App\Service\UserService; use Hyperf\Di\Annotation\Inject; @@ -77,7 +77,7 @@ class ContactsController extends CController return $this->response->fail('好友关系解除失败!'); } - container()->get(TalkListService::class)->deleteByType($user_id, $params['friend_id'], TalkMode::PRIVATE_CHAT); + container()->get(TalkListService::class)->deleteByType($user_id, $params['friend_id'], TalkModeConstant::PRIVATE_CHAT); // TODO 推送消息(待完善) diff --git a/app/Controller/Api/V1/GroupController.php b/app/Controller/Api/V1/GroupController.php index 64f1f5f..b7f91ef 100644 --- a/app/Controller/Api/V1/GroupController.php +++ b/app/Controller/Api/V1/GroupController.php @@ -10,7 +10,7 @@ namespace App\Controller\Api\V1; -use App\Constants\TalkMode; +use App\Constants\TalkModeConstant; use App\Service\GroupNoticeService; use App\Service\TalkListService; use App\Service\UserService; @@ -215,7 +215,7 @@ class GroupController extends CController 'is_manager' => $groupInfo->creator_id == $user_id, 'manager_nickname' => $groupInfo->nickname, 'visit_card' => GroupMember::visitCard($user_id, $group_id), - 'is_disturb' => (int)$service->isDisturb($user_id, $group_id, TalkMode::GROUP_CHAT), + 'is_disturb' => (int)$service->isDisturb($user_id, $group_id, TalkModeConstant::GROUP_CHAT), 'notice' => $notice ? $notice->toArray() : [] ]); } diff --git a/app/Controller/Api/V1/TalkController.php b/app/Controller/Api/V1/TalkController.php index 674e4da..c6ca1ca 100644 --- a/app/Controller/Api/V1/TalkController.php +++ b/app/Controller/Api/V1/TalkController.php @@ -14,7 +14,7 @@ use App\Cache\LastMessage; use App\Cache\Repository\LockRedis; use App\Cache\UnreadTalk; use App\Constants\TalkMessageType; -use App\Constants\TalkMode; +use App\Constants\TalkModeConstant; use App\Model\Talk\TalkList; use App\Service\UserFriendService; use App\Support\UserRelation; @@ -63,7 +63,7 @@ class TalkController extends CController // 读取用户的未读消息列表 if ($list = UnreadTalk::getInstance()->reads($user_id)) { foreach ($list as $friend_id => $num) { - $this->talkListService->create($user_id, $friend_id, TalkMode::PRIVATE_CHAT); + $this->talkListService->create($user_id, $friend_id, TalkModeConstant::PRIVATE_CHAT); } } @@ -107,13 +107,13 @@ class TalkController extends CController 'receiver_id' => $result['receiver_id'], ]); - if ($result['talk_type'] == TalkMode::PRIVATE_CHAT) { + if ($result['talk_type'] == TalkModeConstant::PRIVATE_CHAT) { $userInfo = User::where('id', $data['receiver_id'])->first(['nickname', 'avatar']); $data['avatar'] = $userInfo->avatar; $data['name'] = $userInfo->nickname; $data['unread_num'] = UnreadTalk::getInstance()->read($data['receiver_id'], $user_id); $data['remark_name'] = $service->getFriendRemark($user_id, (int)$data['receiver_id']); - } else if ($result['talk_type'] == TalkMode::GROUP_CHAT) { + } else if ($result['talk_type'] == TalkModeConstant::GROUP_CHAT) { $groupInfo = Group::where('id', $data['receiver_id'])->first(['group_name', 'avatar']); $data['name'] = $groupInfo->group_name; $data['avatar'] = $groupInfo->avatar; @@ -200,7 +200,7 @@ class TalkController extends CController ]); // 设置好友消息未读数 - if ($params['talk_type'] == TalkMode::PRIVATE_CHAT) { + if ($params['talk_type'] == TalkModeConstant::PRIVATE_CHAT) { UnreadTalk::getInstance()->reset((int)$params['receiver_id'], $this->uid()); } @@ -223,7 +223,7 @@ class TalkController extends CController ]); $user_id = $this->uid(); - if ($params['talk_type'] == TalkMode::GROUP_CHAT && !Group::isMember((int)$params['receiver_id'], $user_id)) { + if ($params['talk_type'] == TalkModeConstant::GROUP_CHAT && !Group::isMember((int)$params['receiver_id'], $user_id)) { return $this->response->fail('暂不属于好友关系或群聊成员,无法查看聊天记录!'); } @@ -278,7 +278,7 @@ class TalkController extends CController ]); $user_id = $this->uid(); - if ($params['talk_type'] == TalkMode::GROUP_CHAT && !Group::isMember((int)$params['receiver_id'], $user_id)) { + if ($params['talk_type'] == TalkModeConstant::GROUP_CHAT && !Group::isMember((int)$params['receiver_id'], $user_id)) { return $this->response->fail('暂不属于好友关系或群聊成员,无法查看聊天记录!'); } diff --git a/app/Controller/Api/V1/TalkMessageController.php b/app/Controller/Api/V1/TalkMessageController.php index f58b1f8..8548358 100644 --- a/app/Controller/Api/V1/TalkMessageController.php +++ b/app/Controller/Api/V1/TalkMessageController.php @@ -3,12 +3,12 @@ namespace App\Controller\Api\V1; use App\Cache\UnreadTalk; -use App\Constants\TalkMessageEvent; -use App\Constants\TalkMode; +use App\Constants\TalkEventConstant; +use App\Constants\TalkModeConstant; +use App\Event\TalkEvent; use App\Model\EmoticonItem; use App\Model\FileSplitUpload; use App\Service\TalkMessageService; -use App\Support\MessageProducer; use App\Support\UserRelation; use App\Service\EmoticonService; use App\Service\TalkService; @@ -189,12 +189,12 @@ class TalkMessageController extends CController ]); $user_id = $this->uid(); - if (!UserRelation::isFriendOrGroupMember($user_id, $params['receiver_id'], TalkMode::GROUP_CHAT)) { + if (!UserRelation::isFriendOrGroupMember($user_id, $params['receiver_id'], TalkModeConstant::GROUP_CHAT)) { return $this->response->fail('暂不属于好友关系或群聊成员,无法发送聊天消息!'); } $isTrue = $this->talkMessageService->insertVoteMessage([ - 'talk_type' => TalkMode::GROUP_CHAT, + 'talk_type' => TalkModeConstant::GROUP_CHAT, 'user_id' => $user_id, 'receiver_id' => $params['receiver_id'], ], [ @@ -289,13 +289,13 @@ class TalkMessageController extends CController $receive_user_ids = $receive_group_ids = []; if (isset($params['receive_user_ids']) && !empty($params['receive_user_ids'])) { $receive_user_ids = array_map(function ($friend_id) { - return ['talk_type' => TalkMode::PRIVATE_CHAT, 'id' => (int)$friend_id]; + return ['talk_type' => TalkModeConstant::PRIVATE_CHAT, 'id' => (int)$friend_id]; }, $params['receive_user_ids']); } if (isset($params['receive_group_ids']) && !empty($params['receive_group_ids'])) { $receive_group_ids = array_map(function ($group_id) { - return ['talk_type' => TalkMode::GROUP_CHAT, 'id' => (int)$group_id]; + return ['talk_type' => TalkModeConstant::GROUP_CHAT, 'id' => (int)$group_id]; }, $params['receive_group_ids']); } @@ -318,7 +318,7 @@ class TalkMessageController extends CController // 消息推送队列 foreach ($ids as $value) { - MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ + event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [ 'sender_id' => $user_id, 'receiver_id' => $value['receiver_id'], 'talk_type' => $value['talk_type'], diff --git a/app/Controller/WebSocketController.php b/app/Controller/WebSocketController.php index 7d5daea..da0199a 100644 --- a/app/Controller/WebSocketController.php +++ b/app/Controller/WebSocketController.php @@ -13,9 +13,8 @@ namespace App\Controller; use App\Cache\SocketRoom; use App\Service\Message\ReceiveHandleService; -use App\Support\MessageProducer; use Hyperf\Di\Annotation\Inject; -use App\Constants\TalkMessageEvent; +use App\Constants\TalkEventConstant; use Hyperf\Contract\OnCloseInterface; use Hyperf\Contract\OnMessageInterface; use Hyperf\Contract\OnOpenInterface; @@ -25,6 +24,8 @@ use Swoole\Http\Response; use Swoole\WebSocket\Server; use App\Service\SocketClientService; use App\Model\Group\GroupMember; +use App\Event\TalkEvent; + /** * Class WebSocketController @@ -76,7 +77,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos } if (!$isOnline) { - MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_ONLINE_STATUS, [ + event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_ONLINE_STATUS, [ 'user_id' => $user_id, 'status' => 1, ])); @@ -123,7 +124,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos $isOnline = $this->client->isOnlineAll($user_id); if ($isOnline) return; - MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_ONLINE_STATUS, [ + event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_ONLINE_STATUS, [ 'user_id' => $user_id, 'status' => 0, ])); diff --git a/app/Event/TalkEvent.php b/app/Event/TalkEvent.php new file mode 100644 index 0000000..512d46e --- /dev/null +++ b/app/Event/TalkEvent.php @@ -0,0 +1,30 @@ +event_name = $event_name; + $this->data = $data; + } +} diff --git a/app/Listener/TalkMessageListener.php b/app/Listener/TalkMessageListener.php new file mode 100644 index 0000000..77d458a --- /dev/null +++ b/app/Listener/TalkMessageListener.php @@ -0,0 +1,33 @@ +event_name, $event->data)); + } +} diff --git a/app/Service/ContactApplyService.php b/app/Service/ContactApplyService.php index 56fe7ce..938ebd8 100644 --- a/app/Service/ContactApplyService.php +++ b/app/Service/ContactApplyService.php @@ -3,11 +3,11 @@ namespace App\Service; use App\Cache\FriendApply; -use App\Constants\TalkMessageEvent; +use App\Constants\TalkEventConstant; +use App\Event\TalkEvent; use App\Model\User; use App\Model\UsersFriend; use App\Model\UsersFriendApply; -use App\Support\MessageProducer; use App\Traits\PagingTrait; use Hyperf\DbConnection\Db; @@ -49,7 +49,7 @@ class ContactApplyService // 判断对方是否在线。如果在线发送消息通知 $isOnline = container()->get(SocketClientService::class)->isOnlineAll($friend_id); if ($isOnline) { - MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_FRIEND_APPLY, [ + event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_FRIEND_APPLY, [ 'apply_id' => $result->id, 'type' => 1, ])); @@ -100,7 +100,7 @@ class ContactApplyService // 判断对方是否在线。如果在线发送消息通知 $isOnline = container()->get(SocketClientService::class)->isOnlineAll($info->user_id); if ($isOnline) { - MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_FRIEND_APPLY, [ + event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_FRIEND_APPLY, [ 'apply_id' => $apply_id, 'type' => 2, ])); diff --git a/app/Service/EmoticonService.php b/app/Service/EmoticonService.php index d89ab82..1ea8a84 100644 --- a/app/Service/EmoticonService.php +++ b/app/Service/EmoticonService.php @@ -3,7 +3,7 @@ namespace App\Service; use App\Constants\TalkMessageType; -use App\Constants\TalkMode; +use App\Constants\TalkModeConstant; use App\Model\Talk\TalkRecords; use App\Model\Talk\TalkRecordsFile; use App\Model\EmoticonItem; @@ -101,7 +101,7 @@ class EmoticonService extends BaseService if (!$result) return [false, []]; - if ($result->talk_type == TalkMode::PRIVATE_CHAT) { + if ($result->talk_type == TalkModeConstant::PRIVATE_CHAT) { if ($result->user_id != $user_id && $result->receiver_id != $user_id) { return [false, []]; } diff --git a/app/Service/GroupService.php b/app/Service/GroupService.php index 171911c..004c5b5 100644 --- a/app/Service/GroupService.php +++ b/app/Service/GroupService.php @@ -5,15 +5,15 @@ namespace App\Service; use App\Cache\LastMessage; use App\Cache\SocketRoom; -use App\Constants\TalkMessageEvent; +use App\Constants\TalkEventConstant; use App\Constants\TalkMessageType; -use App\Constants\TalkMode; +use App\Constants\TalkModeConstant; +use App\Event\TalkEvent; use App\Model\Talk\TalkRecords; use App\Model\Talk\TalkRecordsInvite; use App\Model\Group\Group; use App\Model\Group\GroupMember; use App\Model\Talk\TalkList; -use App\Support\MessageProducer; use Hyperf\DbConnection\Db; use Exception; @@ -62,7 +62,7 @@ class GroupService extends BaseService ]; $chatList[] = [ - 'talk_type' => TalkMode::GROUP_CHAT, + 'talk_type' => TalkModeConstant::GROUP_CHAT, 'user_id' => $friend_id, 'receiver_id' => $group->id, 'created_at' => date('Y-m-d H:i:s'), @@ -79,7 +79,7 @@ class GroupService extends BaseService } $result = TalkRecords::create([ - 'talk_type' => TalkMode::GROUP_CHAT, + 'talk_type' => TalkModeConstant::GROUP_CHAT, 'user_id' => 0, 'receiver_id' => $group->id, 'msg_type' => TalkMessageType::GROUP_INVITE_MESSAGE, @@ -100,7 +100,7 @@ class GroupService extends BaseService return [false, null]; } - LastMessage::getInstance()->save(TalkMode::GROUP_CHAT, $user_id, $group->id, [ + LastMessage::getInstance()->save(TalkModeConstant::GROUP_CHAT, $user_id, $group->id, [ 'text' => '[入群通知]', 'created_at' => date('Y-m-d H:i:s') ]); @@ -110,10 +110,10 @@ class GroupService extends BaseService SocketRoom::getInstance()->addRoomMember(strval($group->id), strval($value)); } - MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ + event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [ 'sender_id' => $user_id, 'receiver_id' => $group->id, - 'talk_type' => TalkMode::GROUP_CHAT, + 'talk_type' => TalkModeConstant::GROUP_CHAT, 'record_id' => $result->id ])); @@ -194,7 +194,7 @@ class GroupService extends BaseService $updateArr = $insertArr = $updateArr1 = $insertArr1 = []; $members = GroupMember::where('group_id', $group_id)->whereIn('user_id', $friend_ids)->get(['id', 'user_id', 'is_quit'])->keyBy('user_id')->toArray(); - $chatArr = TalkList::where('talk_type', TalkMode::GROUP_CHAT) + $chatArr = TalkList::where('talk_type', TalkModeConstant::GROUP_CHAT) ->where('receiver_id', $group_id) ->whereIn('user_id', $friend_ids) ->get(['id', 'user_id', 'is_delete']) @@ -213,7 +213,7 @@ class GroupService extends BaseService if (!isset($chatArr[$uid])) { $insertArr1[] = [ - 'talk_type' => TalkMode::GROUP_CHAT, + 'talk_type' => TalkModeConstant::GROUP_CHAT, 'user_id' => $uid, 'receiver_id' => $group_id, 'created_at' => date('Y-m-d H:i:s'), @@ -252,7 +252,7 @@ class GroupService extends BaseService } $result = TalkRecords::create([ - 'talk_type' => TalkMode::GROUP_CHAT, + 'talk_type' => TalkModeConstant::GROUP_CHAT, 'user_id' => 0, 'receiver_id' => $group_id, 'msg_type' => TalkMessageType::GROUP_INVITE_MESSAGE, @@ -273,7 +273,7 @@ class GroupService extends BaseService return false; } - LastMessage::getInstance()->save(TalkMode::GROUP_CHAT, $user_id, $group_id, [ + LastMessage::getInstance()->save(TalkModeConstant::GROUP_CHAT, $user_id, $group_id, [ 'text' => '[入群通知]', 'created_at' => date('Y-m-d H:i:s') ]); @@ -283,10 +283,10 @@ class GroupService extends BaseService SocketRoom::getInstance()->addRoomMember(strval($group_id), strval($value)); } - MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ + event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [ 'sender_id' => $user_id, 'receiver_id' => $group_id, - 'talk_type' => TalkMode::GROUP_CHAT, + 'talk_type' => TalkModeConstant::GROUP_CHAT, 'record_id' => $result->id ])); @@ -317,7 +317,7 @@ class GroupService extends BaseService if ($count == 0) throw new Exception('更新记录失败...'); $result = TalkRecords::create([ - 'talk_type' => TalkMode::GROUP_CHAT, + 'talk_type' => TalkModeConstant::GROUP_CHAT, 'user_id' => 0, 'receiver_id' => $group_id, 'msg_type' => TalkMessageType::GROUP_INVITE_MESSAGE, @@ -334,7 +334,7 @@ class GroupService extends BaseService ]); TalkList::where([ - ['talk_type', '=', TalkMode::GROUP_CHAT], + ['talk_type', '=', TalkModeConstant::GROUP_CHAT], ['user_id', '=', $user_id], ['receiver_id', '=', $group_id], ])->update(['is_delete' => 1]); @@ -348,10 +348,10 @@ class GroupService extends BaseService // 移出聊天室 SocketRoom::getInstance()->delRoomMember(strval($group_id), strval($user_id)); - MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ + event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [ 'sender_id' => $user_id, 'receiver_id' => (int)$params['group_id'], - 'talk_type' => TalkMode::GROUP_CHAT, + 'talk_type' => TalkModeConstant::GROUP_CHAT, 'record_id' => $result->id ])); @@ -382,7 +382,7 @@ class GroupService extends BaseService if ($count == 0) throw new Exception('更新记录失败...'); $result = TalkRecords::create([ - 'talk_type' => TalkMode::GROUP_CHAT, + 'talk_type' => TalkModeConstant::GROUP_CHAT, 'user_id' => 0, 'receiver_id' => $group_id, 'msg_type' => TalkMessageType::GROUP_INVITE_MESSAGE, @@ -397,7 +397,7 @@ class GroupService extends BaseService 'user_ids' => implode(',', $member_ids) ]); - TalkList::whereIn('user_id', $member_ids)->where('receiver_id', $group_id)->where('talk_type', TalkMode::GROUP_CHAT)->update([ + TalkList::whereIn('user_id', $member_ids)->where('receiver_id', $group_id)->where('talk_type', TalkModeConstant::GROUP_CHAT)->update([ 'is_delete' => 1, 'updated_at' => date('Y-m-d H:i:s') ]); @@ -413,10 +413,10 @@ class GroupService extends BaseService SocketRoom::getInstance()->delRoomMember(strval($group_id), strval($uid)); } - MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ + event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [ 'sender_id' => $user_id, 'receiver_id' => $group_id, - 'talk_type' => TalkMode::GROUP_CHAT, + 'talk_type' => TalkModeConstant::GROUP_CHAT, 'record_id' => $result->id ])); @@ -461,7 +461,7 @@ class GroupService extends BaseService $list = []; if ($items) { $list = TalkList::query()->where('user_id', $user_id) - ->where('talk_type', TalkMode::GROUP_CHAT) + ->where('talk_type', TalkModeConstant::GROUP_CHAT) ->whereIn('receiver_id', array_column($items, 'id')) ->get(['receiver_id', 'is_disturb'])->keyBy('receiver_id')->toArray(); } diff --git a/app/Service/Message/FormatMessageService.php b/app/Service/Message/FormatMessageService.php index 3adaaac..40c2508 100644 --- a/app/Service/Message/FormatMessageService.php +++ b/app/Service/Message/FormatMessageService.php @@ -1,4 +1,5 @@ 'onTalk', - TalkMessageEvent::EVENT_KEYBOARD => 'onKeyboard', + TalkEventConstant::EVENT_TALK => 'onTalk', + TalkEventConstant::EVENT_KEYBOARD => 'onKeyboard', ]; /** @@ -55,7 +53,7 @@ class ReceiveHandleService if ($user_id != $data['sender_id']) return; // 验证消息类型 - if (!in_array($data['talk_type'], TalkMode::getTypes())) return; + if (!in_array($data['talk_type'], TalkModeConstant::getTypes())) return; // 验证发送消息用户与接受消息用户之间是否存在好友或群聊关系 $isTrue = UserRelation::isFriendOrGroupMember($user_id, (int)$data['receiver_id'], (int)$data['talk_type']); @@ -77,7 +75,7 @@ class ReceiveHandleService ]); // 判断是否私信 - if ($result->talk_type == TalkMode::PRIVATE_CHAT) { + if ($result->talk_type == TalkModeConstant::PRIVATE_CHAT) { UnreadTalk::getInstance()->increment($result->user_id, $result->receiver_id); } @@ -87,7 +85,7 @@ class ReceiveHandleService 'created_at' => date('Y-m-d H:i:s') ]); - MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ + event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [ 'sender_id' => $result->user_id, 'receiver_id' => $result->receiver_id, 'talk_type' => $result->talk_type, @@ -105,7 +103,7 @@ class ReceiveHandleService */ public function onKeyboard($server, Frame $frame, $data) { - MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_KEYBOARD, [ + event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_KEYBOARD, [ 'sender_id' => (int)$data['sender_id'], 'receiver_id' => (int)$data['receiver_id'], ])); diff --git a/app/Service/Message/SubscribeHandleService.php b/app/Service/Message/SubscribeHandleService.php index b29d370..f321345 100644 --- a/app/Service/Message/SubscribeHandleService.php +++ b/app/Service/Message/SubscribeHandleService.php @@ -1,10 +1,11 @@ 'onConsumeTalk', + TalkEventConstant::EVENT_TALK => 'onConsumeTalk', // 键盘输入事件 - TalkMessageEvent::EVENT_KEYBOARD => 'onConsumeKeyboard', + TalkEventConstant::EVENT_KEYBOARD => 'onConsumeKeyboard', // 用户在线状态事件 - TalkMessageEvent::EVENT_ONLINE_STATUS => 'onConsumeOnlineStatus', + TalkEventConstant::EVENT_ONLINE_STATUS => 'onConsumeOnlineStatus', // 聊天消息推送事件 - TalkMessageEvent::EVENT_REVOKE_TALK => 'onConsumeRevokeTalk', + TalkEventConstant::EVENT_REVOKE_TALK => 'onConsumeRevokeTalk', // 好友申请相关事件 - TalkMessageEvent::EVENT_FRIEND_APPLY => 'onConsumeFriendApply' + TalkEventConstant::EVENT_FRIEND_APPLY => 'onConsumeFriendApply' ]; /** @@ -83,12 +84,12 @@ class SubscribeHandleService $fds = []; $groupInfo = null; - if ($talk_type == TalkMode::PRIVATE_CHAT) { + if ($talk_type == TalkModeConstant::PRIVATE_CHAT) { $fds = array_merge( $this->clientService->findUserFds($sender_id), $this->clientService->findUserFds($receiver_id) ); - } else if ($talk_type == TalkMode::GROUP_CHAT) { + } else if ($talk_type == TalkModeConstant::GROUP_CHAT) { foreach (SocketRoom::getInstance()->getRoomMembers(strval($receiver_id)) as $uid) { $fds = array_merge($fds, $this->clientService->findUserFds(intval($uid))); } @@ -129,7 +130,7 @@ class SubscribeHandleService ]) ]; - $this->socketPushNotify($fds, json_encode([TalkMessageEvent::EVENT_TALK, $notify])); + $this->socketPushNotify($fds, json_encode([TalkEventConstant::EVENT_TALK, $notify])); } /** @@ -141,7 +142,7 @@ class SubscribeHandleService { $fds = $this->clientService->findUserFds($data['data']['receiver_id']); - $this->socketPushNotify($fds, json_encode([TalkMessageEvent::EVENT_KEYBOARD, $data['data']])); + $this->socketPushNotify($fds, json_encode([TalkEventConstant::EVENT_KEYBOARD, $data['data']])); } /** @@ -162,7 +163,7 @@ class SubscribeHandleService } $this->socketPushNotify(array_unique($fds), json_encode([ - TalkMessageEvent::EVENT_ONLINE_STATUS, [ + TalkEventConstant::EVENT_ONLINE_STATUS, [ 'user_id' => $user_id, 'status' => $status ] @@ -179,10 +180,10 @@ class SubscribeHandleService $record = TalkRecords::where('id', $data['data']['record_id'])->first(['id', 'talk_type', 'user_id', 'receiver_id']); $fds = []; - if ($record->talk_type == TalkMode::PRIVATE_CHAT) { + if ($record->talk_type == TalkModeConstant::PRIVATE_CHAT) { $fds = array_merge($fds, $this->clientService->findUserFds($record->user_id)); $fds = array_merge($fds, $this->clientService->findUserFds($record->receiver_id)); - } else if ($record->talk_type == TalkMode::GROUP_CHAT) { + } else if ($record->talk_type == TalkModeConstant::GROUP_CHAT) { $userIds = SocketRoom::getInstance()->getRoomMembers(strval($record->receiver_id)); foreach ($userIds as $uid) { $fds = array_merge($fds, $this->clientService->findUserFds((int)$uid)); @@ -190,7 +191,7 @@ class SubscribeHandleService } $fds = array_unique($fds); - $this->socketPushNotify($fds, json_encode([TalkMessageEvent::EVENT_REVOKE_TALK, [ + $this->socketPushNotify($fds, json_encode([TalkEventConstant::EVENT_REVOKE_TALK, [ 'talk_type' => $record->talk_type, 'sender_id' => $record->user_id, 'receiver_id' => $record->receiver_id, @@ -236,7 +237,7 @@ class SubscribeHandleService 'mobile' => $friendInfo->mobile, ]; - $this->socketPushNotify(array_unique($fds), json_encode([TalkMessageEvent::EVENT_FRIEND_APPLY, $msg])); + $this->socketPushNotify(array_unique($fds), json_encode([TalkEventConstant::EVENT_FRIEND_APPLY, $msg])); } /** diff --git a/app/Service/TalkListService.php b/app/Service/TalkListService.php index 5cda408..a4b209a 100644 --- a/app/Service/TalkListService.php +++ b/app/Service/TalkListService.php @@ -5,7 +5,7 @@ namespace App\Service; use App\Cache\LastMessage; use App\Cache\ServerRunID; use App\Cache\UnreadTalk; -use App\Constants\TalkMode; +use App\Constants\TalkModeConstant; use App\Model\Talk\TalkList; use Carbon\Carbon; @@ -113,10 +113,10 @@ class TalkListService $rows = TalkList::from('talk_list as list') ->leftJoin('users', function ($join) { - $join->on('users.id', '=', 'list.receiver_id')->where('list.talk_type', '=', TalkMode::PRIVATE_CHAT); + $join->on('users.id', '=', 'list.receiver_id')->where('list.talk_type', '=', TalkModeConstant::PRIVATE_CHAT); }) ->leftJoin('group', function ($join) { - $join->on('group.id', '=', 'list.receiver_id')->where('list.talk_type', '=', TalkMode::GROUP_CHAT); + $join->on('group.id', '=', 'list.receiver_id')->where('list.talk_type', '=', TalkModeConstant::GROUP_CHAT); }) ->where('list.user_id', $user_id) ->where('list.is_delete', 0) @@ -137,13 +137,13 @@ class TalkListService 'updated_at' => Carbon::parse($item['updated_at'])->toDateTimeString(), ]); - if ($item['talk_type'] == TalkMode::PRIVATE_CHAT) { + if ($item['talk_type'] == TalkModeConstant::PRIVATE_CHAT) { $data['name'] = $item['nickname']; $data['avatar'] = $item['user_avatar']; $data['unread_num'] = UnreadTalk::getInstance()->read($item['receiver_id'], $user_id); $data['is_online'] = container()->get(SocketClientService::class)->isOnlineAll($item['receiver_id'], $runIdAll); $data['remark_name'] = container()->get(UserFriendService::class)->getFriendRemark($user_id, $item['receiver_id']); - } else if (TalkMode::GROUP_CHAT) { + } else if (TalkModeConstant::GROUP_CHAT) { $data['name'] = strval($item['group_name']); $data['avatar'] = $item['group_avatar']; } diff --git a/app/Service/TalkMessageService.php b/app/Service/TalkMessageService.php index a5ab8f2..d9ef802 100644 --- a/app/Service/TalkMessageService.php +++ b/app/Service/TalkMessageService.php @@ -5,16 +5,16 @@ namespace App\Service; use App\Cache\LastMessage; use App\Cache\VoteCache; use App\Cache\VoteStatisticsCache; -use App\Constants\TalkMessageEvent; +use App\Constants\TalkEventConstant; use App\Constants\TalkMessageType; +use App\Event\TalkEvent; use App\Model\Group\GroupMember; use App\Model\Talk\TalkRecordsCode; use App\Model\Talk\TalkRecordsVote; use App\Model\Talk\TalkRecordsVoteAnswer; -use App\Support\MessageProducer; use App\Support\UserRelation; use Exception; -use App\Constants\MediaFileType; +use App\Constants\MediaTypeConstant; use App\Model\Talk\TalkRecords; use App\Model\Talk\TalkRecordsFile; use Hyperf\DbConnection\Db; @@ -53,18 +53,18 @@ class TalkMessageService return false; } - MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ + LastMessage::getInstance()->save($insert->talk_type, $insert->user_id, $insert->receiver_id, [ + 'text' => '[代码消息]', + 'created_at' => date('Y-m-d H:i:s') + ]); + + event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [ 'sender_id' => $insert->user_id, 'receiver_id' => $insert->receiver_id, 'talk_type' => $insert->talk_type, 'record_id' => $insert->id ])); - LastMessage::getInstance()->save($insert->talk_type, $insert->user_id, $insert->receiver_id, [ - 'text' => '[代码消息]', - 'created_at' => date('Y-m-d H:i:s') - ]); - return true; } @@ -89,7 +89,7 @@ class TalkMessageService } $file['record_id'] = $insert->id; - $file['file_type'] = MediaFileType::getMediaType($file['file_suffix']); + $file['file_type'] = MediaTypeConstant::getMediaType($file['file_suffix']); $file['created_at'] = date('Y-m-d H:i:s'); if (!TalkRecordsFile::create($file)) { throw new Exception('插入聊天记录(代码消息)失败...'); @@ -101,18 +101,18 @@ class TalkMessageService return false; } - MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ + LastMessage::getInstance()->save($insert->talk_type, $insert->user_id, $insert->receiver_id, [ + 'text' => '[图片消息]', + 'created_at' => date('Y-m-d H:i:s') + ]); + + event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [ 'sender_id' => $insert->user_id, 'receiver_id' => $insert->receiver_id, 'talk_type' => $insert->talk_type, 'record_id' => $insert->id ])); - LastMessage::getInstance()->save($insert->talk_type, $insert->user_id, $insert->receiver_id, [ - 'text' => '[图片消息]', - 'created_at' => date('Y-m-d H:i:s') - ]); - return true; } @@ -154,19 +154,18 @@ class TalkMessageService return false; } - // 推送消息通知 - MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ + LastMessage::getInstance()->save($insert->talk_type, $insert->user_id, $insert->receiver_id, [ + 'text' => '[投票消息]', + 'created_at' => date('Y-m-d H:i:s') + ]); + + event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [ 'sender_id' => $insert->user_id, 'receiver_id' => $insert->receiver_id, 'talk_type' => $insert->talk_type, 'record_id' => $insert->id ])); - LastMessage::getInstance()->save($insert->talk_type, $insert->user_id, $insert->receiver_id, [ - 'text' => '[投票消息]', - 'created_at' => date('Y-m-d H:i:s') - ]); - return true; } diff --git a/app/Service/TalkService.php b/app/Service/TalkService.php index 2370507..74cc864 100644 --- a/app/Service/TalkService.php +++ b/app/Service/TalkService.php @@ -2,11 +2,11 @@ namespace App\Service; -use App\Constants\TalkMessageEvent; +use App\Constants\TalkEventConstant; use App\Constants\TalkMessageType; -use App\Constants\TalkMode; +use App\Constants\TalkModeConstant; +use App\Event\TalkEvent; use App\Service\Message\FormatMessageService; -use App\Support\MessageProducer; use Exception; use App\Model\Group\Group; use App\Model\Talk\TalkRecords; @@ -53,7 +53,7 @@ class TalkService extends BaseService $rowsSqlObj->where('talk_records.id', '<', $record_id); } - if ($talk_type == TalkMode::PRIVATE_CHAT) { + if ($talk_type == TalkModeConstant::PRIVATE_CHAT) { $rowsSqlObj->where(function ($query) use ($user_id, $receiver_id) { $query->where([ ['talk_records.user_id', '=', $user_id], @@ -98,9 +98,9 @@ class TalkService extends BaseService ]); // 判断是否有权限查看 - if ($result->talk_type == TalkMode::PRIVATE_CHAT && ($result->user_id != $user_id && $result->receiver_id != $user_id)) { + if ($result->talk_type == TalkModeConstant::PRIVATE_CHAT && ($result->user_id != $user_id && $result->receiver_id != $user_id)) { return []; - } else if ($result->talk_type == TalkMode::GROUP_CHAT && !Group::isMember($result->receiver_id, $user_id)) { + } else if ($result->talk_type == TalkModeConstant::GROUP_CHAT && !Group::isMember($result->receiver_id, $user_id)) { return []; } @@ -138,13 +138,13 @@ class TalkService extends BaseService */ public function removeRecords(int $user_id, int $talk_type, int $receiver_id, array $record_ids) { - if ($talk_type == TalkMode::PRIVATE_CHAT) {// 私聊信息 + if ($talk_type == TalkModeConstant::PRIVATE_CHAT) {// 私聊信息 $ids = TalkRecords::whereIn('id', $record_ids)->where(function ($query) use ($user_id, $receiver_id) { $query->where([['user_id', '=', $user_id], ['receiver_id', '=', $receiver_id]]) ->orWhere([['user_id', '=', $receiver_id], ['receiver_id', '=', $user_id]]); })->where('talk_type', $talk_type)->pluck('id'); } else {// 群聊信息 - $ids = TalkRecords::whereIn('id', $record_ids)->where('talk_type', TalkMode::GROUP_CHAT)->pluck('id'); + $ids = TalkRecords::whereIn('id', $record_ids)->where('talk_type', TalkModeConstant::GROUP_CHAT)->pluck('id'); } // 判断要删除的消息在数据库中是否存在 @@ -153,7 +153,7 @@ class TalkService extends BaseService } // 判读是否属于群消息并且判断是否是群成员 - if ($talk_type == TalkMode::GROUP_CHAT && !Group::isMember($receiver_id, $user_id)) { + if ($talk_type == TalkModeConstant::GROUP_CHAT && !Group::isMember($receiver_id, $user_id)) { return false; } @@ -185,11 +185,11 @@ class TalkService extends BaseService return [false, '已超过有效的撤回时间', []]; } - if ($result->talk_type == TalkMode::PRIVATE_CHAT) { + if ($result->talk_type == TalkModeConstant::PRIVATE_CHAT) { if ($result->user_id != $user_id && $result->receiver_id != $user_id) { return [false, '非法操作', []]; } - } else if ($result->talk_type == TalkMode::GROUP_CHAT) { + } else if ($result->talk_type == TalkModeConstant::GROUP_CHAT) { if (!Group::isMember($result->receiver_id, $user_id)) { return [false, '非法操作', []]; } @@ -198,7 +198,7 @@ class TalkService extends BaseService $result->is_revoke = 1; $result->save(); - MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_REVOKE_TALK, [ + event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_REVOKE_TALK, [ 'record_id' => $result->id ])); @@ -225,11 +225,11 @@ class TalkService extends BaseService if (!$result) return []; // 根据消息类型判断用户是否有转发权限 - if ($result->talk_type == TalkMode::PRIVATE_CHAT) { + if ($result->talk_type == TalkModeConstant::PRIVATE_CHAT) { if ($result->user_id != $user_id && $result->receiver_id != $user_id) { return []; } - } else if ($result->talk_type == TalkMode::GROUP_CHAT) { + } else if ($result->talk_type == TalkModeConstant::GROUP_CHAT) { if (!Group::isMember($result->receiver_id, $user_id)) { return []; } @@ -324,7 +324,7 @@ class TalkService extends BaseService $sqlObj = TalkRecords::whereIn('id', $records_ids); - if ($talk_type == TalkMode::PRIVATE_CHAT) { + if ($talk_type == TalkModeConstant::PRIVATE_CHAT) { if (!container()->get(UserFriendService::class)->isFriend($user_id, $receiver_id, true)) return []; $sqlObj = $sqlObj->where(function ($query) use ($user_id, $receiver_id) { @@ -339,7 +339,7 @@ class TalkService extends BaseService } else { if (!Group::isMember($receiver_id, $user_id)) return []; - $sqlObj = $sqlObj->where('receiver_id', $receiver_id)->whereIn('msg_type', $msg_type)->where('talk_type', TalkMode::GROUP_CHAT)->where('is_revoke', 0); + $sqlObj = $sqlObj->where('receiver_id', $receiver_id)->whereIn('msg_type', $msg_type)->where('talk_type', TalkModeConstant::GROUP_CHAT)->where('is_revoke', 0); } $result = $sqlObj->get(); diff --git a/app/Support/UserRelation.php b/app/Support/UserRelation.php index be5f2b0..b2e60ee 100644 --- a/app/Support/UserRelation.php +++ b/app/Support/UserRelation.php @@ -2,7 +2,7 @@ namespace App\Support; -use App\Constants\TalkMode; +use App\Constants\TalkModeConstant; use App\Model\Group\Group; use App\Service\UserFriendService; @@ -18,9 +18,9 @@ class UserRelation */ public static function isFriendOrGroupMember(int $user_id, int $receiver_id, int $talk_type) { - if ($talk_type == TalkMode::PRIVATE_CHAT) { + if ($talk_type == TalkModeConstant::PRIVATE_CHAT) { return container()->get(UserFriendService::class)->isFriend($user_id, $receiver_id, true); - } else if ($talk_type == TalkMode::GROUP_CHAT) { + } else if ($talk_type == TalkModeConstant::GROUP_CHAT) { return Group::isMember($receiver_id, $user_id); } diff --git a/更新计划.md b/更新计划.md index 35a38cf..e69de29 100644 --- a/更新计划.md +++ b/更新计划.md @@ -1,11 +0,0 @@ --- 更新计划 -- - -1. 好友相关逻辑待开发 - -2. 投票消息功能待开发 - -3. 发送名片功能待开发 - -4. 推送登录通知待开发 - -5. 静态文件上传第三方待开发