优化代码

main
gzydong 2021-07-08 19:30:03 +08:00
parent a3b5033cab
commit 7c1b33b52b
17 changed files with 156 additions and 149 deletions

View File

@ -19,7 +19,7 @@ use App\Cache\SocketFdBindUser;
use App\Cache\SocketRoom;
use App\Cache\SocketUserBindFds;
use App\Cache\UnreadTalk;
use App\Constants\FileMediaType;
use App\Constants\MediaFileType;
use App\Model\Group\Group;
use App\Model\Group\GroupMember;
use App\Model\TalkList;

View File

@ -7,7 +7,7 @@ namespace App\Constants;
*
* @package App\Constants
*/
class FileMediaType
class MediaFileType
{
const FILE_IMAGE = 1; //图片文件
const FILE_VIDEO = 2; //视频文件

View File

@ -4,5 +4,8 @@ namespace App\Constants;
class RedisSubscribeChan
{
/**
* Websocket 消息推送渠道
*/
const WEBSOCKET_CHAN = 'websocket';
}

View File

@ -7,7 +7,7 @@ namespace App\Constants;
*
* @package App\Constants
*/
class SocketConstants
class TalkMessageEvent
{
/**
* 对话消息通知 - 事件名

View File

@ -7,14 +7,14 @@ namespace App\Constants;
*
* @package App\Constants
*/
class TalkMsgType
class TalkMessageType
{
const TEXT_MESSAGE = 1;//文本消息
const FILE_MESSAGE = 2;//文件消息
const FORWARD_MESSAGE = 3;//会话消息
const CODE_MESSAGE = 4;//代码消息
const VOTE_MESSAGE = 5;//投票消息
const GROUP_NOTICE_MESSAGE = 6;//群公告
const GROUP_NOTICE_MESSAGE = 6;//群公告
const FRIEND_APPLY_MESSAGE = 7;//好友申请
const USER_LOGIN_MESSAGE = 8;//登录通知
const GROUP_INVITE_MESSAGE = 9;//入群退群消息

View File

@ -2,7 +2,12 @@
namespace App\Constants;
class TalkType
/**
* 聊天对话模式
*
* @package App\Constants
*/
class TalkMode
{
/**
* 私聊
@ -14,7 +19,6 @@ class TalkType
*/
const GROUP_CHAT = 2;
public static function getTypes()
{
return [

View File

@ -5,7 +5,7 @@ namespace App\Controller\Api\V1;
use App\Amqp\Producer\ChatMessageProducer;
use App\Cache\FriendApply;
use App\Constants\SocketConstants;
use App\Constants\TalkMessageEvent;
use App\Model\UsersFriendsApply;
use App\Service\SocketClientService;
use App\Service\UserService;
@ -72,7 +72,7 @@ class ContactsApplyController extends CController
// 判断对方是否在线。如果在线发送消息通知
if ($this->socketClientService->isOnlineAll($params['friend_id'])) {
MessageProducer::publish(
MessageProducer::create(SocketConstants::EVENT_FRIEND_APPLY, [
MessageProducer::create(TalkMessageEvent::EVENT_FRIEND_APPLY, [
'apply_id' => $result->id,
'type' => 1,
])
@ -105,7 +105,7 @@ class ContactsApplyController extends CController
// 判断对方是否在线。如果在线发送消息通知
if ($this->socketClientService->isOnlineAll($friend_id)) {
MessageProducer::publish(
MessageProducer::create(SocketConstants::EVENT_FRIEND_APPLY, [
MessageProducer::create(TalkMessageEvent::EVENT_FRIEND_APPLY, [
'apply_id' => (int)$params['apply_id'],
'type' => 2,
])

View File

@ -11,7 +11,7 @@
namespace App\Controller\Api\V1;
use App\Cache\SocketRoom;
use App\Constants\TalkType;
use App\Constants\TalkMode;
use App\Service\UserService;
use App\Support\MessageProducer;
use Hyperf\Di\Annotation\Inject;
@ -26,7 +26,7 @@ use App\Model\Group\GroupMember;
use App\Model\Group\GroupNotice;
use App\Amqp\Producer\ChatMessageProducer;
use App\Service\GroupService;
use App\Constants\SocketConstants;
use App\Constants\TalkMessageEvent;
use Psr\Http\Message\ResponseInterface;
/**
@ -78,10 +78,10 @@ class GroupController extends CController
}
MessageProducer::publish(
MessageProducer::create(SocketConstants::EVENT_TALK, [
MessageProducer::create(TalkMessageEvent::EVENT_TALK, [
'sender_id' => $user_id,
'receiver_id' => (int)$data['group_id'],
'talk_type' => TalkType::GROUP_CHAT,
'talk_type' => TalkMode::GROUP_CHAT,
'record_id' => (int)$data['record_id']
])
);
@ -144,10 +144,10 @@ class GroupController extends CController
}
MessageProducer::publish(
MessageProducer::create(SocketConstants::EVENT_TALK, [
MessageProducer::create(TalkMessageEvent::EVENT_TALK, [
'sender_id' => $user_id,
'receiver_id' => (int)$params['group_id'],
'talk_type' => TalkType::GROUP_CHAT,
'talk_type' => TalkMode::GROUP_CHAT,
'record_id' => $record_id
])
);
@ -178,10 +178,10 @@ class GroupController extends CController
SocketRoom::getInstance()->delRoomMember($params['group_id'], $user_id);
MessageProducer::publish(
MessageProducer::create(SocketConstants::EVENT_TALK, [
MessageProducer::create(TalkMessageEvent::EVENT_TALK, [
'sender_id' => $user_id,
'receiver_id' => (int)$params['group_id'],
'talk_type' => TalkType::GROUP_CHAT,
'talk_type' => TalkMode::GROUP_CHAT,
'record_id' => $record_id
])
);
@ -250,10 +250,10 @@ class GroupController extends CController
}
MessageProducer::publish(
MessageProducer::create(SocketConstants::EVENT_TALK, [
MessageProducer::create(TalkMessageEvent::EVENT_TALK, [
'sender_id' => $user_id,
'receiver_id' => (int)$params['group_id'],
'talk_type' => TalkType::GROUP_CHAT,
'talk_type' => TalkMode::GROUP_CHAT,
'record_id' => $record_id
])
);
@ -301,7 +301,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)TalkList::where('user_id', $user_id)->where('receiver_id', $group_id)->where('talk_type', TalkType::GROUP_CHAT)->value('is_disturb'),
'is_disturb' => (int)TalkList::where('user_id', $user_id)->where('receiver_id', $group_id)->where('talk_type', TalkMode::GROUP_CHAT)->value('is_disturb'),
'notice' => $notice ? $notice->toArray() : []
]);
}

View File

@ -12,8 +12,8 @@ namespace App\Controller\Api\V1;
use App\Cache\LastMessage;
use App\Cache\UnreadTalk;
use App\Constants\TalkMsgType;
use App\Constants\TalkType;
use App\Constants\TalkMessageType;
use App\Constants\TalkMode;
use App\Support\UserRelation;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
@ -96,12 +96,12 @@ class TalkController extends CController
'updated_at' => date('Y-m-d H:i:s')
];
if ($result['talk_type'] == TalkType::PRIVATE_CHAT) {
if ($result['talk_type'] == TalkMode::PRIVATE_CHAT) {
$userInfo = User::where('id', $user_id)->first(['nickname', 'avatar']);
$data['avatar'] = $userInfo->avatar;
$data['name'] = $userInfo->nickname;
$data['unread_num'] = UnreadTalk::getInstance()->read($data['receiver_id'], $user_id);
} else if ($result['talk_type'] == TalkType::GROUP_CHAT) {
} else if ($result['talk_type'] == TalkMode::GROUP_CHAT) {
$groupInfo = Group::where('id', $data['receiver_id'])->first(['group_name', 'avatar']);
$data['name'] = $groupInfo->group_name;
$data['avatar'] = $groupInfo->avatar;
@ -190,7 +190,7 @@ class TalkController extends CController
]);
// 设置好友消息未读数
if ($params['talk_type'] == TalkType::PRIVATE_CHAT) {
if ($params['talk_type'] == TalkMode::PRIVATE_CHAT) {
UnreadTalk::getInstance()->reset((int)$params['receiver_id'], $this->uid());
}
@ -273,10 +273,10 @@ class TalkController extends CController
}
$types = [
TalkMsgType::TEXT_MESSAGE,
TalkMsgType::FILE_MESSAGE,
TalkMsgType::FORWARD_MESSAGE,
TalkMsgType::CODE_MESSAGE
TalkMessageType::TEXT_MESSAGE,
TalkMessageType::FILE_MESSAGE,
TalkMessageType::FORWARD_MESSAGE,
TalkMessageType::CODE_MESSAGE
];
if (in_array($params['msg_type'], $types)) {

View File

@ -5,9 +5,9 @@ namespace App\Controller\Api\V1;
use App\Amqp\Producer\ChatMessageProducer;
use App\Cache\LastMessage;
use App\Cache\UnreadTalk;
use App\Constants\SocketConstants;
use App\Constants\TalkMsgType;
use App\Constants\TalkType;
use App\Constants\TalkMessageEvent;
use App\Constants\TalkMessageType;
use App\Constants\TalkMode;
use App\Model\EmoticonItem;
use App\Model\FileSplitUpload;
use App\Support\MessageProducer;
@ -57,7 +57,7 @@ class TalkMessageController extends CController
$record_id = $this->talkService->createCodeMessage([
'talk_type' => $params['talk_type'],
'msg_type' => TalkMsgType::CODE_MESSAGE,
'msg_type' => TalkMessageType::CODE_MESSAGE,
'user_id' => $user_id,
'receiver_id' => $params['receiver_id'],
], [
@ -69,7 +69,7 @@ class TalkMessageController extends CController
if (!$record_id) return $this->response->fail('消息发送失败!');
MessageProducer::publish(
MessageProducer::create(SocketConstants::EVENT_TALK, [
MessageProducer::create(TalkMessageEvent::EVENT_TALK, [
'sender_id' => $user_id,
'receiver_id' => (int)$params['receiver_id'],
'talk_type' => (int)$params['talk_type'],
@ -122,7 +122,7 @@ class TalkMessageController extends CController
// 创建图片消息记录
$record_id = $this->talkService->createImgMessage([
'talk_type' => $params['talk_type'],
'msg_type' => TalkMsgType::FILE_MESSAGE,
'msg_type' => TalkMessageType::FILE_MESSAGE,
'user_id' => $user_id,
'receiver_id' => $params['receiver_id'],
], [
@ -136,7 +136,7 @@ class TalkMessageController extends CController
if (!$record_id) return $this->response->fail('图片上传失败!');
MessageProducer::publish(
MessageProducer::create(SocketConstants::EVENT_TALK, [
MessageProducer::create(TalkMessageEvent::EVENT_TALK, [
'sender_id' => $user_id,
'receiver_id' => (int)$params['receiver_id'],
'talk_type' => (int)$params['talk_type'],
@ -184,7 +184,7 @@ class TalkMessageController extends CController
$record_id = $this->talkService->createFileMessage([
'talk_type' => $params['talk_type'],
'msg_type' => TalkMsgType::FILE_MESSAGE,
'msg_type' => TalkMessageType::FILE_MESSAGE,
'user_id' => $user_id,
'receiver_id' => $params['receiver_id']
], [
@ -199,7 +199,7 @@ class TalkMessageController extends CController
if (!$record_id) return $this->response->fail('表情发送失败!');
MessageProducer::publish(
MessageProducer::create(SocketConstants::EVENT_TALK, [
MessageProducer::create(TalkMessageEvent::EVENT_TALK, [
'sender_id' => $user_id,
'receiver_id' => (int)$params['receiver_id'],
'talk_type' => (int)$params['talk_type'],
@ -250,7 +250,7 @@ class TalkMessageController extends CController
$record_id = $this->talkService->createEmoticonMessage([
'talk_type' => $params['talk_type'],
'msg_type' => TalkMsgType::FILE_MESSAGE,
'msg_type' => TalkMessageType::FILE_MESSAGE,
'user_id' => $user_id,
'receiver_id' => $params['receiver_id'],
], [
@ -264,7 +264,7 @@ class TalkMessageController extends CController
if (!$record_id) return $this->response->fail('表情发送失败!');
MessageProducer::publish(
MessageProducer::create(SocketConstants::EVENT_TALK, [
MessageProducer::create(TalkMessageEvent::EVENT_TALK, [
'sender_id' => $user_id,
'receiver_id' => (int)$params['receiver_id'],
'talk_type' => (int)$params['talk_type'],
@ -297,13 +297,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' => TalkType::PRIVATE_CHAT, 'id' => (int)$friend_id];
return ['talk_type' => TalkMode::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' => TalkType::GROUP_CHAT, 'id' => (int)$group_id];
return ['talk_type' => TalkMode::GROUP_CHAT, 'id' => (int)$group_id];
}, $params['receive_group_ids']);
}
@ -327,7 +327,7 @@ class TalkMessageController extends CController
// 消息推送队列
foreach ($ids as $value) {
MessageProducer::publish(
MessageProducer::create(SocketConstants::EVENT_TALK, [
MessageProducer::create(TalkMessageEvent::EVENT_TALK, [
'sender_id' => $user_id,
'receiver_id' => $value['receiver_id'],
'talk_type' => $value['talk_type'],
@ -374,7 +374,7 @@ class TalkMessageController extends CController
if (!$isTrue) return $this->response->fail($message);
MessageProducer::publish(
MessageProducer::create(SocketConstants::EVENT_REVOKE_TALK, [
MessageProducer::create(TalkMessageEvent::EVENT_REVOKE_TALK, [
'record_id' => $params['record_id']
])
);

View File

@ -15,7 +15,7 @@ use App\Cache\SocketRoom;
use App\Service\Message\ReceiveHandleService;
use App\Support\MessageProducer;
use Hyperf\Di\Annotation\Inject;
use App\Constants\SocketConstants;
use App\Constants\TalkMessageEvent;
use Hyperf\Contract\OnCloseInterface;
use Hyperf\Contract\OnMessageInterface;
use Hyperf\Contract\OnOpenInterface;
@ -49,8 +49,8 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
* 消息事件绑定
*/
const EVENTS = [
SocketConstants::EVENT_TALK => 'onTalk',
SocketConstants::EVENT_KEYBOARD => 'onKeyboard',
TalkMessageEvent::EVENT_TALK => 'onTalk',
TalkMessageEvent::EVENT_KEYBOARD => 'onKeyboard',
];
/**
@ -85,7 +85,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
if (!$isOnline) {
MessageProducer::publish(
MessageProducer::create(SocketConstants::EVENT_ONLINE_STATUS, [
MessageProducer::create(TalkMessageEvent::EVENT_ONLINE_STATUS, [
'user_id' => $user_id,
'status' => 1,
])
@ -134,7 +134,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
$isOnline = $this->socketClientService->isOnlineAll($user_id);
if (!$isOnline) {
MessageProducer::publish(
MessageProducer::create(SocketConstants::EVENT_ONLINE_STATUS, [
MessageProducer::create(TalkMessageEvent::EVENT_ONLINE_STATUS, [
'user_id' => $user_id,
'status' => 0,
])

View File

@ -2,8 +2,8 @@
namespace App\Service;
use App\Constants\TalkMsgType;
use App\Constants\TalkType;
use App\Constants\TalkMessageType;
use App\Constants\TalkMode;
use App\Model\Chat\TalkRecords;
use App\Model\Chat\TalkRecordsFile;
use App\Model\EmoticonItem;
@ -95,13 +95,13 @@ class EmoticonService extends BaseService
{
$result = TalkRecords::where([
['id', '=', $record_id],
['msg_type', '=', TalkMsgType::FILE_MESSAGE],
['msg_type', '=', TalkMessageType::FILE_MESSAGE],
['is_revoke', '=', 0],
])->first(['id', 'talk_type', 'receiver_id', 'msg_type', 'user_id', 'is_revoke']);
if (!$result) return [false, []];
if ($result->talk_type == TalkType::PRIVATE_CHAT) {
if ($result->talk_type == TalkMode::PRIVATE_CHAT) {
if ($result->user_id != $user_id && $result->receiver_id != $user_id) {
return [false, []];
}

View File

@ -4,8 +4,8 @@ declare(strict_types=1);
namespace App\Service;
use App\Cache\LastMessage;
use App\Constants\TalkMsgType;
use App\Constants\TalkType;
use App\Constants\TalkMessageType;
use App\Constants\TalkMode;
use App\Model\Chat\TalkRecords;
use App\Model\Chat\TalkRecordsInvite;
use App\Model\Group\Group;
@ -46,7 +46,7 @@ class GroupService extends BaseService
$list = [];
if ($items) {
$list = TalkList::query()->where('user_id', $user_id)
->where('talk_type', TalkType::GROUP_CHAT)
->where('talk_type', TalkMode::GROUP_CHAT)
->whereIn('receiver_id', array_column($items, 'id'))
->get(['receiver_id', 'is_disturb'])->keyBy('receiver_id')->toArray();
}
@ -96,7 +96,7 @@ class GroupService extends BaseService
];
$chatList[] = [
'talk_type' => TalkType::GROUP_CHAT,
'talk_type' => TalkMode::GROUP_CHAT,
'user_id' => $friend_id,
'receiver_id' => $insRes->id,
'created_at' => date('Y-m-d H:i:s'),
@ -113,10 +113,10 @@ class GroupService extends BaseService
}
$result = TalkRecords::create([
'talk_type' => TalkType::GROUP_CHAT,
'talk_type' => TalkMode::GROUP_CHAT,
'user_id' => 0,
'receiver_id' => $insRes->id,
'msg_type' => TalkMsgType::GROUP_INVITE_MESSAGE,
'msg_type' => TalkMessageType::GROUP_INVITE_MESSAGE,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
]);
@ -134,7 +134,7 @@ class GroupService extends BaseService
return [false, 0];
}
LastMessage::getInstance()->save(TalkType::GROUP_CHAT, $user_id, $insRes->id, [
LastMessage::getInstance()->save(TalkMode::GROUP_CHAT, $user_id, $insRes->id, [
'text' => '[入群通知]',
'created_at' => date('Y-m-d H:i:s')
]);
@ -193,7 +193,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', TalkType::GROUP_CHAT)
$chatArr = TalkList::where('talk_type', TalkMode::GROUP_CHAT)
->where('receiver_id', $group_id)
->whereIn('user_id', $friend_ids)
->get(['id', 'user_id', 'is_delete'])
@ -212,7 +212,7 @@ class GroupService extends BaseService
if (!isset($chatArr[$uid])) {
$insertArr1[] = [
'talk_type' => TalkType::GROUP_CHAT,
'talk_type' => TalkMode::GROUP_CHAT,
'user_id' => $uid,
'receiver_id' => $group_id,
'created_at' => date('Y-m-d H:i:s'),
@ -251,10 +251,10 @@ class GroupService extends BaseService
}
$result = TalkRecords::create([
'talk_type' => TalkType::GROUP_CHAT,
'talk_type' => TalkMode::GROUP_CHAT,
'user_id' => 0,
'receiver_id' => $group_id,
'msg_type' => TalkMsgType::GROUP_INVITE_MESSAGE,
'msg_type' => TalkMessageType::GROUP_INVITE_MESSAGE,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
]);
@ -272,7 +272,7 @@ class GroupService extends BaseService
return [false, 0];
}
LastMessage::getInstance()->save(TalkType::GROUP_CHAT, $user_id, $group_id, [
LastMessage::getInstance()->save(TalkMode::GROUP_CHAT, $user_id, $group_id, [
'text' => '[入群通知]',
'created_at' => date('Y-m-d H:i:s')
]);
@ -306,10 +306,10 @@ class GroupService extends BaseService
}
$result = TalkRecords::create([
'talk_type' => TalkType::GROUP_CHAT,
'talk_type' => TalkMode::GROUP_CHAT,
'user_id' => 0,
'receiver_id' => $group_id,
'msg_type' => TalkMsgType::GROUP_INVITE_MESSAGE,
'msg_type' => TalkMessageType::GROUP_INVITE_MESSAGE,
'content' => $user_id,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
@ -325,7 +325,7 @@ class GroupService extends BaseService
]);
TalkList::where([
['talk_type', '=', TalkType::GROUP_CHAT],
['talk_type', '=', TalkMode::GROUP_CHAT],
['user_id', '=', $user_id],
['receiver_id', '=', $group_id],
])->update([
@ -367,10 +367,10 @@ class GroupService extends BaseService
}
$result = TalkRecords::create([
'talk_type' => TalkType::GROUP_CHAT,
'talk_type' => TalkMode::GROUP_CHAT,
'user_id' => 0,
'receiver_id' => $group_id,
'msg_type' => TalkMsgType::GROUP_INVITE_MESSAGE,
'msg_type' => TalkMessageType::GROUP_INVITE_MESSAGE,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
]);
@ -382,7 +382,7 @@ class GroupService extends BaseService
'user_ids' => implode(',', $member_ids)
]);
TalkList::whereIn('user_id', $member_ids)->where('receiver_id', $group_id)->where('talk_type', TalkType::GROUP_CHAT)->update([
TalkList::whereIn('user_id', $member_ids)->where('receiver_id', $group_id)->where('talk_type', TalkMode::GROUP_CHAT)->update([
'is_delete' => 1,
'updated_at' => date('Y-m-d H:i:s')
]);

View File

@ -4,9 +4,9 @@ namespace App\Service\Message;
use App\Cache\LastMessage;
use App\Cache\UnreadTalk;
use App\Constants\SocketConstants;
use App\Constants\TalkMsgType;
use App\Constants\TalkType;
use App\Constants\TalkMessageEvent;
use App\Constants\TalkMessageType;
use App\Constants\TalkMode;
use App\Model\Chat\TalkRecords;
use App\Model\Group\Group;
use App\Model\UsersFriend;
@ -44,17 +44,17 @@ class ReceiveHandleService
}
// 验证消息类型 私聊|群聊
if (!in_array($data['talk_type'], TalkType::getTypes())) {
if (!in_array($data['talk_type'], TalkMode::getTypes())) {
return;
}
// 验证发送消息用户与接受消息用户之间是否存在好友或群聊关系(后期走缓存)
if ($data['talk_type'] == TalkType::PRIVATE_CHAT) {
if ($data['talk_type'] == TalkMode::PRIVATE_CHAT) {
// 判断发送者和接受者是否是好友关系
if (!UsersFriend::isFriend((int)$data['sender_id'], (int)$data['receiver_id'], true)) {
return;
}
} else if ($data['talk_type'] == TalkType::GROUP_CHAT) {
} else if ($data['talk_type'] == TalkMode::GROUP_CHAT) {
// 判断是否属于群成员
if (!Group::isMember((int)$data['receiver_id'], (int)$data['sender_id'])) {
return;
@ -65,7 +65,7 @@ class ReceiveHandleService
'talk_type' => $data['talk_type'],
'user_id' => $data['sender_id'],
'receiver_id' => $data['receiver_id'],
'msg_type' => TalkMsgType::TEXT_MESSAGE,
'msg_type' => TalkMessageType::TEXT_MESSAGE,
'content' => htmlspecialchars($data['text_message']),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
@ -74,7 +74,7 @@ class ReceiveHandleService
if (!$result) return;
// 判断是否私聊
if ($result->talk_type == TalkType::PRIVATE_CHAT) {
if ($result->talk_type == TalkMode::PRIVATE_CHAT) {
// 设置好友消息未读数
UnreadTalk::getInstance()->increment($result->user_id, $result->receiver_id);
}
@ -86,7 +86,7 @@ class ReceiveHandleService
]);
MessageProducer::publish(
MessageProducer::create(SocketConstants::EVENT_TALK, [
MessageProducer::create(TalkMessageEvent::EVENT_TALK, [
'sender_id' => $result->user_id,
'receiver_id' => $result->receiver_id,
'talk_type' => $result->talk_type,
@ -106,7 +106,7 @@ class ReceiveHandleService
public function onKeyboard($server, Frame $frame, $data)
{
MessageProducer::publish(
MessageProducer::create(SocketConstants::EVENT_KEYBOARD, [
MessageProducer::create(TalkMessageEvent::EVENT_KEYBOARD, [
'sender_id' => intval($data['sender_id']),
'receiver_id' => intval($data['receiver_id']),
])

View File

@ -3,9 +3,9 @@
namespace App\Service\Message;
use App\Cache\SocketRoom;
use App\Constants\SocketConstants;
use App\Constants\TalkMsgType;
use App\Constants\TalkType;
use App\Constants\TalkMessageEvent;
use App\Constants\TalkMessageType;
use App\Constants\TalkMode;
use App\Model\Chat\TalkRecords;
use App\Model\Chat\TalkRecordsCode;
use App\Model\Chat\TalkRecordsFile;
@ -27,19 +27,19 @@ class SubscribeHandleService
*/
const EVENTS = [
// 聊天消息事件
SocketConstants::EVENT_TALK => 'onConsumeTalk',
TalkMessageEvent::EVENT_TALK => 'onConsumeTalk',
// 键盘输入事件
SocketConstants::EVENT_KEYBOARD => 'onConsumeKeyboard',
TalkMessageEvent::EVENT_KEYBOARD => 'onConsumeKeyboard',
// 用户在线状态事件
SocketConstants::EVENT_ONLINE_STATUS => 'onConsumeOnlineStatus',
TalkMessageEvent::EVENT_ONLINE_STATUS => 'onConsumeOnlineStatus',
// 聊天消息推送事件
SocketConstants::EVENT_REVOKE_TALK => 'onConsumeRevokeTalk',
TalkMessageEvent::EVENT_REVOKE_TALK => 'onConsumeRevokeTalk',
// 好友申请相关事件
SocketConstants::EVENT_FRIEND_APPLY => 'onConsumeFriendApply'
TalkMessageEvent::EVENT_FRIEND_APPLY => 'onConsumeFriendApply'
];
/**
@ -68,12 +68,12 @@ class SubscribeHandleService
$fds = [];
$groupInfo = null;
if ($talk_type == TalkType::PRIVATE_CHAT) {
if ($talk_type == TalkMode::PRIVATE_CHAT) {
$fds = array_merge(
$this->clientService->findUserFds($sender_id),
$this->clientService->findUserFds($receiver_id)
);
} else if ($talk_type == TalkType::GROUP_CHAT) {
} else if ($talk_type == TalkMode::GROUP_CHAT) {
foreach (SocketRoom::getInstance()->getRoomMembers(strval($receiver_id)) as $uid) {
$fds = array_merge($fds, $this->clientService->findUserFds(intval($uid)));
}
@ -106,7 +106,7 @@ class SubscribeHandleService
$file = $code_block = $forward = $invite = [];
switch ($result->msg_type) {
case TalkMsgType::FILE_MESSAGE:
case TalkMessageType::FILE_MESSAGE:
$file = TalkRecordsFile::where('record_id', $result->id)->first([
'id', 'record_id', 'user_id', 'file_source', 'file_type',
'save_type', 'original_name', 'file_suffix', 'file_size', 'save_dir'
@ -116,7 +116,7 @@ class SubscribeHandleService
$file && $file['file_url'] = get_media_url($file['save_dir']);
break;
case TalkMsgType::FORWARD_MESSAGE:
case TalkMessageType::FORWARD_MESSAGE:
$forward = ['num' => 0, 'list' => []];
$forwardInfo = TalkRecordsForward::where('record_id', $result->id)->first(['records_id', 'text']);
if ($forwardInfo) {
@ -127,12 +127,12 @@ class SubscribeHandleService
}
break;
case TalkMsgType::CODE_MESSAGE:
case TalkMessageType::CODE_MESSAGE:
$code_block = TalkRecordsCode::where('record_id', $result->id)->first(['record_id', 'code_lang', 'code']);
$code_block = $code_block ? $code_block->toArray() : [];
break;
case TalkMsgType::GROUP_INVITE_MESSAGE:
case TalkMessageType::GROUP_INVITE_MESSAGE:
$notifyInfo = TalkRecordsInvite::where('record_id', $result->id)->first([
'record_id', 'type', 'operate_user_id', 'user_ids'
]);
@ -171,7 +171,7 @@ class SubscribeHandleService
])
];
$this->socketPushNotify($fds, json_encode([SocketConstants::EVENT_TALK, $notify]));
$this->socketPushNotify($fds, json_encode([TalkMessageEvent::EVENT_TALK, $notify]));
return true;
}
@ -186,7 +186,7 @@ class SubscribeHandleService
{
$fds = $this->clientService->findUserFds($data['data']['receiver_id']);
$this->socketPushNotify($fds, json_encode([SocketConstants::EVENT_KEYBOARD, $data['data']]));
$this->socketPushNotify($fds, json_encode([TalkMessageEvent::EVENT_KEYBOARD, $data['data']]));
return true;
}
@ -210,7 +210,7 @@ class SubscribeHandleService
}
$this->socketPushNotify(array_unique($fds), json_encode([
SocketConstants::EVENT_ONLINE_STATUS, [
TalkMessageEvent::EVENT_ONLINE_STATUS, [
'user_id' => $user_id,
'status' => $status
]
@ -230,10 +230,10 @@ class SubscribeHandleService
$record = TalkRecords::where('id', $data['data']['record_id'])->first(['id', 'talk_type', 'user_id', 'receiver_id']);
$fds = [];
if ($record->talk_type == TalkType::PRIVATE_CHAT) {
if ($record->talk_type == TalkMode::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 == TalkType::GROUP_CHAT) {
} else if ($record->talk_type == TalkMode::GROUP_CHAT) {
$userIds = SocketRoom::getInstance()->getRoomMembers(strval($record->receiver_id));
foreach ($userIds as $uid) {
$fds = array_merge($fds, $this->clientService->findUserFds((int)$uid));
@ -241,7 +241,7 @@ class SubscribeHandleService
}
$fds = array_unique($fds);
$this->socketPushNotify($fds, json_encode([SocketConstants::EVENT_REVOKE_TALK, [
$this->socketPushNotify($fds, json_encode([TalkMessageEvent::EVENT_REVOKE_TALK, [
'talk_type' => $record->talk_type,
'sender_id' => $record->user_id,
'receiver_id' => $record->receiver_id,
@ -290,7 +290,7 @@ class SubscribeHandleService
'mobile' => $friendInfo->mobile,
];
$this->socketPushNotify(array_unique($fds), json_encode([SocketConstants::EVENT_FRIEND_APPLY, $msg]));
$this->socketPushNotify(array_unique($fds), json_encode([TalkMessageEvent::EVENT_FRIEND_APPLY, $msg]));
return true;
}

View File

@ -3,9 +3,9 @@
namespace App\Service;
use App\Cache\ServerRunID;
use App\Constants\FileMediaType;
use App\Constants\TalkMsgType;
use App\Constants\TalkType;
use App\Constants\MediaFileType;
use App\Constants\TalkMessageType;
use App\Constants\TalkMode;
use Exception;
use App\Model\User;
use App\Model\TalkList;
@ -42,10 +42,10 @@ class TalkService extends BaseService
$rows = TalkList::from('talk_list as list')
->leftJoin('users', function ($join) {
$join->on('users.id', '=', 'list.receiver_id')->where('list.talk_type', '=', TalkType::PRIVATE_CHAT);
$join->on('users.id', '=', 'list.receiver_id')->where('list.talk_type', '=', TalkMode::PRIVATE_CHAT);
})
->leftJoin('group', function ($join) {
$join->on('group.id', '=', 'list.receiver_id')->where('list.talk_type', '=', TalkType::GROUP_CHAT);
$join->on('group.id', '=', 'list.receiver_id')->where('list.talk_type', '=', TalkMode::GROUP_CHAT);
})
->where('list.user_id', $user_id)
->where('list.is_delete', 0)
@ -72,7 +72,7 @@ class TalkService extends BaseService
$data['msg_text'] = '......';
$data['updated_at'] = $item['updated_at'] ?: '2020-01-01 00:00:00';
if ($item['talk_type'] == TalkType::PRIVATE_CHAT) {
if ($item['talk_type'] == TalkMode::PRIVATE_CHAT) {
$data['name'] = $item['nickname'];
$data['avatar'] = $item['user_avatar'];
$data['unread_num'] = UnreadTalk::getInstance()->read($item['receiver_id'], $user_id);
@ -103,7 +103,7 @@ class TalkService extends BaseService
{
foreach ($data as $friend_id => $num) {
TalkList::updateOrCreate([
'talk_type' => TalkType::PRIVATE_CHAT,
'talk_type' => TalkMode::PRIVATE_CHAT,
'user_id' => $user_id,
'receiver_id' => $friend_id,
], [
@ -127,16 +127,16 @@ class TalkService extends BaseService
$files = $codes = $forwards = $invites = [];
foreach ($rows as $value) {
switch ($value['msg_type']) {
case TalkMsgType::FILE_MESSAGE:
case TalkMessageType::FILE_MESSAGE:
$files[] = $value['id'];
break;
case TalkMsgType::GROUP_INVITE_MESSAGE:
case TalkMessageType::GROUP_INVITE_MESSAGE:
$invites[] = $value['id'];
break;
case TalkMsgType::FORWARD_MESSAGE:
case TalkMessageType::FORWARD_MESSAGE:
$forwards[] = $value['id'];
break;
case TalkMsgType::CODE_MESSAGE:
case TalkMessageType::CODE_MESSAGE:
$codes[] = $value['id'];
break;
}
@ -169,14 +169,14 @@ class TalkService extends BaseService
$rows[$k]['invite'] = [];
switch ($row['msg_type']) {
case TalkMsgType::FILE_MESSAGE:// 文件消息
case TalkMessageType::FILE_MESSAGE:// 文件消息
$rows[$k]['file'] = $files[$row['id']] ?? [];
if ($rows[$k]['file']) {
$rows[$k]['file']['file_url'] = get_media_url($rows[$k]['file']['save_dir']);
}
break;
case TalkMsgType::FORWARD_MESSAGE:// 会话记录消息
case TalkMessageType::FORWARD_MESSAGE:// 会话记录消息
if (isset($forwards[$row['id']])) {
$rows[$k]['forward'] = [
'num' => substr_count($forwards[$row['id']]['records_id'], ',') + 1,
@ -185,7 +185,7 @@ class TalkService extends BaseService
}
break;
case TalkMsgType::CODE_MESSAGE:// 代码块消息
case TalkMessageType::CODE_MESSAGE:// 代码块消息
$rows[$k]['code_block'] = $codes[$row['id']] ?? [];
if ($rows[$k]['code_block']) {
$rows[$k]['code_block']['code'] = htmlspecialchars_decode($rows[$k]['code_block']['code']);
@ -193,7 +193,7 @@ class TalkService extends BaseService
}
break;
case TalkMsgType::GROUP_INVITE_MESSAGE:// 入群消息/退群消息
case TalkMessageType::GROUP_INVITE_MESSAGE:// 入群消息/退群消息
if (isset($invites[$row['id']])) {
$rows[$k]['invite'] = [
'type' => $invites[$row['id']]['type'],
@ -251,7 +251,7 @@ class TalkService extends BaseService
$rowsSqlObj->where('talk_records.id', '<', $record_id);
}
if ($talk_type == TalkType::PRIVATE_CHAT) {
if ($talk_type == TalkMode::PRIVATE_CHAT) {
$rowsSqlObj->where(function ($query) use ($user_id, $receiver_id) {
$query->where([
['talk_records.user_id', '=', $user_id],
@ -297,9 +297,9 @@ class TalkService extends BaseService
]);
// 判断是否有权限查看
if ($result->talk_type == TalkType::PRIVATE_CHAT && ($result->user_id != $user_id && $result->receiver_id != $user_id)) {
if ($result->talk_type == TalkMode::PRIVATE_CHAT && ($result->user_id != $user_id && $result->receiver_id != $user_id)) {
return [];
} else if ($result->talk_type == TalkType::GROUP_CHAT && !Group::isMember($result->receiver_id, $user_id)) {
} else if ($result->talk_type == TalkMode::GROUP_CHAT && !Group::isMember($result->receiver_id, $user_id)) {
return [];
}
@ -336,13 +336,13 @@ class TalkService extends BaseService
*/
public function removeRecords(int $user_id, int $talk_type, int $receiver_id, array $record_ids)
{
if ($talk_type == TalkType::PRIVATE_CHAT) {// 私聊信息
if ($talk_type == TalkMode::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', TalkType::GROUP_CHAT)->pluck('id');
$ids = TalkRecords::whereIn('id', $record_ids)->where('talk_type', TalkMode::GROUP_CHAT)->pluck('id');
}
// 判断要删除的消息在数据库中是否存在
@ -351,7 +351,7 @@ class TalkService extends BaseService
}
// 判读是否属于群消息并且判断是否是群成员
if ($talk_type == TalkType::GROUP_CHAT && !Group::isMember($receiver_id, $user_id)) {
if ($talk_type == TalkMode::GROUP_CHAT && !Group::isMember($receiver_id, $user_id)) {
return false;
}
@ -383,11 +383,11 @@ class TalkService extends BaseService
return [false, '已超过有效的撤回时间', []];
}
if ($result->talk_type == TalkType::PRIVATE_CHAT) {
if ($result->talk_type == TalkMode::PRIVATE_CHAT) {
if ($result->user_id != $user_id && $result->receiver_id != $user_id) {
return [false, '非法操作', []];
}
} else if ($result->talk_type == TalkType::GROUP_CHAT) {
} else if ($result->talk_type == TalkMode::GROUP_CHAT) {
if (!Group::isMember($result->receiver_id, $user_id)) {
return [false, '非法操作', []];
}
@ -410,29 +410,29 @@ class TalkService extends BaseService
public function forwardRecords(int $user_id, int $record_id, array $receiver_ids)
{
$msgTypeArray = [
TalkMsgType::TEXT_MESSAGE,
TalkMsgType::FILE_MESSAGE,
TalkMsgType::CODE_MESSAGE
TalkMessageType::TEXT_MESSAGE,
TalkMessageType::FILE_MESSAGE,
TalkMessageType::CODE_MESSAGE
];
$result = TalkRecords::where('id', $record_id)->whereIn('msg_type', $msgTypeArray)->first();
if (!$result) return [];
// 根据消息类型判断用户是否有转发权限
if ($result->talk_type == TalkType::PRIVATE_CHAT) {
if ($result->talk_type == TalkMode::PRIVATE_CHAT) {
if ($result->user_id != $user_id && $result->receiver_id != $user_id) {
return [];
}
} else if ($result->talk_type == TalkType::GROUP_CHAT) {
} else if ($result->talk_type == TalkMode::GROUP_CHAT) {
if (!Group::isMember($result->receiver_id, $user_id)) {
return [];
}
}
$fileInfo = $codeBlock = null;
if ($result->msg_type == TalkMsgType::FILE_MESSAGE) {
if ($result->msg_type == TalkMessageType::FILE_MESSAGE) {
$fileInfo = TalkRecordsFile::where('record_id', $record_id)->first();
} else if ($result->msg_type == TalkMsgType::CODE_MESSAGE) {
} else if ($result->msg_type == TalkMessageType::CODE_MESSAGE) {
$codeBlock = TalkRecordsCode::where('record_id', $record_id)->first();
}
@ -460,7 +460,7 @@ class TalkService extends BaseService
'talk_type' => $res->talk_type
];
if ($result->msg_type == TalkMsgType::FILE_MESSAGE) {
if ($result->msg_type == TalkMessageType::FILE_MESSAGE) {
if (!TalkRecordsFile::create([
'record_id' => $res->id,
'user_id' => $fileInfo->user_id,
@ -475,7 +475,7 @@ class TalkService extends BaseService
])) {
throw new Exception('插入文件消息记录失败');
}
} else if ($result->msg_type == TalkMsgType::CODE_MESSAGE) {
} else if ($result->msg_type == TalkMessageType::CODE_MESSAGE) {
if (!TalkRecordsCode::create([
'record_id' => $res->id,
'user_id' => $user_id,
@ -511,14 +511,14 @@ class TalkService extends BaseService
{
// 支持转发的消息类型
$msg_type = [
TalkMsgType::TEXT_MESSAGE,
TalkMsgType::FILE_MESSAGE,
TalkMsgType::CODE_MESSAGE
TalkMessageType::TEXT_MESSAGE,
TalkMessageType::FILE_MESSAGE,
TalkMessageType::CODE_MESSAGE
];
$sqlObj = TalkRecords::whereIn('id', $records_ids);
if ($talk_type == TalkType::PRIVATE_CHAT) {
if ($talk_type == TalkMode::PRIVATE_CHAT) {
if (!UsersFriend::isFriend($user_id, $receiver_id)) return [];
$sqlObj = $sqlObj->where(function ($query) use ($user_id, $receiver_id) {
@ -533,7 +533,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', TalkType::GROUP_CHAT)->where('is_revoke', 0);
$sqlObj = $sqlObj->where('receiver_id', $receiver_id)->whereIn('msg_type', $msg_type)->where('talk_type', TalkMode::GROUP_CHAT)->where('is_revoke', 0);
}
$result = $sqlObj->get();
@ -550,19 +550,19 @@ class TalkService extends BaseService
$jsonText = [];
foreach ($rows as $row) {
switch ($row->msg_type) {
case TalkMsgType::TEXT_MESSAGE:
case TalkMessageType::TEXT_MESSAGE:
$jsonText[] = [
'nickname' => $row->nickname,
'text' => mb_substr(str_replace(PHP_EOL, "", $row->content), 0, 30)
];
break;
case TalkMsgType::FILE_MESSAGE:
case TalkMessageType::FILE_MESSAGE:
$jsonText[] = [
'nickname' => $row->nickname,
'text' => '【文件消息】'
];
break;
case TalkMsgType::CODE_MESSAGE:
case TalkMessageType::CODE_MESSAGE:
$jsonText[] = [
'nickname' => $row->nickname,
'text' => '【代码消息】'
@ -579,7 +579,7 @@ class TalkService extends BaseService
'talk_type' => $item['talk_type'],
'user_id' => $user_id,
'receiver_id' => $item['id'],
'msg_type' => TalkMsgType::FORWARD_MESSAGE,
'msg_type' => TalkMessageType::FORWARD_MESSAGE,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
]);
@ -692,7 +692,7 @@ class TalkService extends BaseService
}
$fileInfo['record_id'] = $insert->id;
$fileInfo['file_type'] = FileMediaType::getMediaType($fileInfo['file_suffix']);
$fileInfo['file_type'] = MediaFileType::getMediaType($fileInfo['file_suffix']);
$fileInfo['created_at'] = date('Y-m-d H:i:s');
if (!TalkRecordsFile::create($fileInfo)) {
@ -790,7 +790,7 @@ class TalkService extends BaseService
}
$emoticon['record_id'] = $insert->id;
$emoticon['file_type'] = FileMediaType::getMediaType($emoticon['file_suffix']);
$emoticon['file_type'] = MediaFileType::getMediaType($emoticon['file_suffix']);
$emoticon['created_at'] = date('Y-m-d H:i:s');
if (!TalkRecordsFile::create($emoticon)) {
throw new Exception('插入聊天记录(代码消息)失败...');

View File

@ -2,7 +2,7 @@
namespace App\Support;
use App\Constants\TalkType;
use App\Constants\TalkMode;
use App\Model\Group\Group;
use App\Model\UsersFriend;
@ -18,9 +18,9 @@ class UserRelation
*/
public static function isFriendOrGroupMember(int $user_id, int $receiver_id, int $talk_type)
{
if ($talk_type == TalkType::PRIVATE_CHAT) {
if ($talk_type == TalkMode::PRIVATE_CHAT) {
return UsersFriend::isFriend($user_id, $receiver_id, true);
} else if ($talk_type == TalkType::GROUP_CHAT) {
} else if ($talk_type == TalkMode::GROUP_CHAT) {
return Group::isMember($receiver_id, $user_id);
}