优化代码

main
gzydong 2021-07-20 23:12:18 +08:00
parent c9728f1a20
commit 93131bde18
21 changed files with 194 additions and 142 deletions

View File

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

View File

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

View File

@ -7,7 +7,7 @@ namespace App\Constants;
* *
* @package App\Constants * @package App\Constants
*/ */
class TalkMode class TalkModeConstant
{ {
/** /**
* 私信 * 私信

View File

@ -10,7 +10,7 @@
namespace App\Controller\Api\V1; namespace App\Controller\Api\V1;
use App\Constants\TalkMode; use App\Constants\TalkModeConstant;
use App\Service\TalkListService; use App\Service\TalkListService;
use App\Service\UserService; use App\Service\UserService;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
@ -77,7 +77,7 @@ class ContactsController extends CController
return $this->response->fail('好友关系解除失败!'); 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 推送消息(待完善) // TODO 推送消息(待完善)

View File

@ -10,7 +10,7 @@
namespace App\Controller\Api\V1; namespace App\Controller\Api\V1;
use App\Constants\TalkMode; use App\Constants\TalkModeConstant;
use App\Service\GroupNoticeService; use App\Service\GroupNoticeService;
use App\Service\TalkListService; use App\Service\TalkListService;
use App\Service\UserService; use App\Service\UserService;
@ -215,7 +215,7 @@ class GroupController extends CController
'is_manager' => $groupInfo->creator_id == $user_id, 'is_manager' => $groupInfo->creator_id == $user_id,
'manager_nickname' => $groupInfo->nickname, 'manager_nickname' => $groupInfo->nickname,
'visit_card' => GroupMember::visitCard($user_id, $group_id), '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() : [] 'notice' => $notice ? $notice->toArray() : []
]); ]);
} }

View File

@ -14,7 +14,7 @@ use App\Cache\LastMessage;
use App\Cache\Repository\LockRedis; use App\Cache\Repository\LockRedis;
use App\Cache\UnreadTalk; use App\Cache\UnreadTalk;
use App\Constants\TalkMessageType; use App\Constants\TalkMessageType;
use App\Constants\TalkMode; use App\Constants\TalkModeConstant;
use App\Model\Talk\TalkList; use App\Model\Talk\TalkList;
use App\Service\UserFriendService; use App\Service\UserFriendService;
use App\Support\UserRelation; use App\Support\UserRelation;
@ -63,7 +63,7 @@ class TalkController extends CController
// 读取用户的未读消息列表 // 读取用户的未读消息列表
if ($list = UnreadTalk::getInstance()->reads($user_id)) { if ($list = UnreadTalk::getInstance()->reads($user_id)) {
foreach ($list as $friend_id => $num) { 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'], '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']); $userInfo = User::where('id', $data['receiver_id'])->first(['nickname', 'avatar']);
$data['avatar'] = $userInfo->avatar; $data['avatar'] = $userInfo->avatar;
$data['name'] = $userInfo->nickname; $data['name'] = $userInfo->nickname;
$data['unread_num'] = UnreadTalk::getInstance()->read($data['receiver_id'], $user_id); $data['unread_num'] = UnreadTalk::getInstance()->read($data['receiver_id'], $user_id);
$data['remark_name'] = $service->getFriendRemark($user_id, (int)$data['receiver_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']); $groupInfo = Group::where('id', $data['receiver_id'])->first(['group_name', 'avatar']);
$data['name'] = $groupInfo->group_name; $data['name'] = $groupInfo->group_name;
$data['avatar'] = $groupInfo->avatar; $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()); UnreadTalk::getInstance()->reset((int)$params['receiver_id'], $this->uid());
} }
@ -223,7 +223,7 @@ class TalkController extends CController
]); ]);
$user_id = $this->uid(); $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('暂不属于好友关系或群聊成员,无法查看聊天记录!'); return $this->response->fail('暂不属于好友关系或群聊成员,无法查看聊天记录!');
} }
@ -278,7 +278,7 @@ class TalkController extends CController
]); ]);
$user_id = $this->uid(); $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('暂不属于好友关系或群聊成员,无法查看聊天记录!'); return $this->response->fail('暂不属于好友关系或群聊成员,无法查看聊天记录!');
} }

View File

@ -3,12 +3,12 @@
namespace App\Controller\Api\V1; namespace App\Controller\Api\V1;
use App\Cache\UnreadTalk; use App\Cache\UnreadTalk;
use App\Constants\TalkMessageEvent; use App\Constants\TalkEventConstant;
use App\Constants\TalkMode; use App\Constants\TalkModeConstant;
use App\Event\TalkEvent;
use App\Model\EmoticonItem; use App\Model\EmoticonItem;
use App\Model\FileSplitUpload; use App\Model\FileSplitUpload;
use App\Service\TalkMessageService; use App\Service\TalkMessageService;
use App\Support\MessageProducer;
use App\Support\UserRelation; use App\Support\UserRelation;
use App\Service\EmoticonService; use App\Service\EmoticonService;
use App\Service\TalkService; use App\Service\TalkService;
@ -189,12 +189,12 @@ class TalkMessageController extends CController
]); ]);
$user_id = $this->uid(); $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('暂不属于好友关系或群聊成员,无法发送聊天消息!'); return $this->response->fail('暂不属于好友关系或群聊成员,无法发送聊天消息!');
} }
$isTrue = $this->talkMessageService->insertVoteMessage([ $isTrue = $this->talkMessageService->insertVoteMessage([
'talk_type' => TalkMode::GROUP_CHAT, 'talk_type' => TalkModeConstant::GROUP_CHAT,
'user_id' => $user_id, 'user_id' => $user_id,
'receiver_id' => $params['receiver_id'], 'receiver_id' => $params['receiver_id'],
], [ ], [
@ -289,13 +289,13 @@ class TalkMessageController extends CController
$receive_user_ids = $receive_group_ids = []; $receive_user_ids = $receive_group_ids = [];
if (isset($params['receive_user_ids']) && !empty($params['receive_user_ids'])) { if (isset($params['receive_user_ids']) && !empty($params['receive_user_ids'])) {
$receive_user_ids = array_map(function ($friend_id) { $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']); }, $params['receive_user_ids']);
} }
if (isset($params['receive_group_ids']) && !empty($params['receive_group_ids'])) { if (isset($params['receive_group_ids']) && !empty($params['receive_group_ids'])) {
$receive_group_ids = array_map(function ($group_id) { $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']); }, $params['receive_group_ids']);
} }
@ -318,7 +318,7 @@ class TalkMessageController extends CController
// 消息推送队列 // 消息推送队列
foreach ($ids as $value) { foreach ($ids as $value) {
MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [
'sender_id' => $user_id, 'sender_id' => $user_id,
'receiver_id' => $value['receiver_id'], 'receiver_id' => $value['receiver_id'],
'talk_type' => $value['talk_type'], 'talk_type' => $value['talk_type'],

View File

@ -13,9 +13,8 @@ namespace App\Controller;
use App\Cache\SocketRoom; use App\Cache\SocketRoom;
use App\Service\Message\ReceiveHandleService; use App\Service\Message\ReceiveHandleService;
use App\Support\MessageProducer;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use App\Constants\TalkMessageEvent; use App\Constants\TalkEventConstant;
use Hyperf\Contract\OnCloseInterface; use Hyperf\Contract\OnCloseInterface;
use Hyperf\Contract\OnMessageInterface; use Hyperf\Contract\OnMessageInterface;
use Hyperf\Contract\OnOpenInterface; use Hyperf\Contract\OnOpenInterface;
@ -25,6 +24,8 @@ use Swoole\Http\Response;
use Swoole\WebSocket\Server; use Swoole\WebSocket\Server;
use App\Service\SocketClientService; use App\Service\SocketClientService;
use App\Model\Group\GroupMember; use App\Model\Group\GroupMember;
use App\Event\TalkEvent;
/** /**
* Class WebSocketController * Class WebSocketController
@ -76,7 +77,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
} }
if (!$isOnline) { if (!$isOnline) {
MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_ONLINE_STATUS, [ event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_ONLINE_STATUS, [
'user_id' => $user_id, 'user_id' => $user_id,
'status' => 1, 'status' => 1,
])); ]));
@ -123,7 +124,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
$isOnline = $this->client->isOnlineAll($user_id); $isOnline = $this->client->isOnlineAll($user_id);
if ($isOnline) return; if ($isOnline) return;
MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_ONLINE_STATUS, [ event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_ONLINE_STATUS, [
'user_id' => $user_id, 'user_id' => $user_id,
'status' => 0, 'status' => 0,
])); ]));

30
app/Event/TalkEvent.php Normal file
View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace App\Event;
class TalkEvent
{
/**
* @var string 消息事件名
*/
public $event_name;
/**
* @var array 消息数据
*/
public $data;
/**
* TalkMessageEvent constructor.
*
* @param string $event_name
* @param array $data
*/
public function __construct(string $event_name, array $data = [])
{
$this->event_name = $event_name;
$this->data = $data;
}
}

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace App\Listener;
use App\Event\TalkEvent;
use App\Support\MessageProducer;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Event\Annotation\Listener;
/**
* Websocket 消息监听器
*
* @Listener
*/
class TalkMessageListener implements ListenerInterface
{
public function listen(): array
{
// 返回一个该监听器要监听的事件数组,可以同时监听多个事件
return [
TalkEvent::class,
];
}
/**
* @param object|TalkEvent $event
*/
public function process(object $event)
{
MessageProducer::publish(MessageProducer::create($event->event_name, $event->data));
}
}

View File

@ -3,11 +3,11 @@
namespace App\Service; namespace App\Service;
use App\Cache\FriendApply; use App\Cache\FriendApply;
use App\Constants\TalkMessageEvent; use App\Constants\TalkEventConstant;
use App\Event\TalkEvent;
use App\Model\User; use App\Model\User;
use App\Model\UsersFriend; use App\Model\UsersFriend;
use App\Model\UsersFriendApply; use App\Model\UsersFriendApply;
use App\Support\MessageProducer;
use App\Traits\PagingTrait; use App\Traits\PagingTrait;
use Hyperf\DbConnection\Db; use Hyperf\DbConnection\Db;
@ -49,7 +49,7 @@ class ContactApplyService
// 判断对方是否在线。如果在线发送消息通知 // 判断对方是否在线。如果在线发送消息通知
$isOnline = container()->get(SocketClientService::class)->isOnlineAll($friend_id); $isOnline = container()->get(SocketClientService::class)->isOnlineAll($friend_id);
if ($isOnline) { if ($isOnline) {
MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_FRIEND_APPLY, [ event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_FRIEND_APPLY, [
'apply_id' => $result->id, 'apply_id' => $result->id,
'type' => 1, 'type' => 1,
])); ]));
@ -100,7 +100,7 @@ class ContactApplyService
// 判断对方是否在线。如果在线发送消息通知 // 判断对方是否在线。如果在线发送消息通知
$isOnline = container()->get(SocketClientService::class)->isOnlineAll($info->user_id); $isOnline = container()->get(SocketClientService::class)->isOnlineAll($info->user_id);
if ($isOnline) { if ($isOnline) {
MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_FRIEND_APPLY, [ event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_FRIEND_APPLY, [
'apply_id' => $apply_id, 'apply_id' => $apply_id,
'type' => 2, 'type' => 2,
])); ]));

View File

@ -3,7 +3,7 @@
namespace App\Service; namespace App\Service;
use App\Constants\TalkMessageType; use App\Constants\TalkMessageType;
use App\Constants\TalkMode; use App\Constants\TalkModeConstant;
use App\Model\Talk\TalkRecords; use App\Model\Talk\TalkRecords;
use App\Model\Talk\TalkRecordsFile; use App\Model\Talk\TalkRecordsFile;
use App\Model\EmoticonItem; use App\Model\EmoticonItem;
@ -101,7 +101,7 @@ class EmoticonService extends BaseService
if (!$result) return [false, []]; 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) { if ($result->user_id != $user_id && $result->receiver_id != $user_id) {
return [false, []]; return [false, []];
} }

View File

@ -5,15 +5,15 @@ namespace App\Service;
use App\Cache\LastMessage; use App\Cache\LastMessage;
use App\Cache\SocketRoom; use App\Cache\SocketRoom;
use App\Constants\TalkMessageEvent; use App\Constants\TalkEventConstant;
use App\Constants\TalkMessageType; 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\TalkRecords;
use App\Model\Talk\TalkRecordsInvite; use App\Model\Talk\TalkRecordsInvite;
use App\Model\Group\Group; use App\Model\Group\Group;
use App\Model\Group\GroupMember; use App\Model\Group\GroupMember;
use App\Model\Talk\TalkList; use App\Model\Talk\TalkList;
use App\Support\MessageProducer;
use Hyperf\DbConnection\Db; use Hyperf\DbConnection\Db;
use Exception; use Exception;
@ -62,7 +62,7 @@ class GroupService extends BaseService
]; ];
$chatList[] = [ $chatList[] = [
'talk_type' => TalkMode::GROUP_CHAT, 'talk_type' => TalkModeConstant::GROUP_CHAT,
'user_id' => $friend_id, 'user_id' => $friend_id,
'receiver_id' => $group->id, 'receiver_id' => $group->id,
'created_at' => date('Y-m-d H:i:s'), 'created_at' => date('Y-m-d H:i:s'),
@ -79,7 +79,7 @@ class GroupService extends BaseService
} }
$result = TalkRecords::create([ $result = TalkRecords::create([
'talk_type' => TalkMode::GROUP_CHAT, 'talk_type' => TalkModeConstant::GROUP_CHAT,
'user_id' => 0, 'user_id' => 0,
'receiver_id' => $group->id, 'receiver_id' => $group->id,
'msg_type' => TalkMessageType::GROUP_INVITE_MESSAGE, 'msg_type' => TalkMessageType::GROUP_INVITE_MESSAGE,
@ -100,7 +100,7 @@ class GroupService extends BaseService
return [false, null]; return [false, null];
} }
LastMessage::getInstance()->save(TalkMode::GROUP_CHAT, $user_id, $group->id, [ LastMessage::getInstance()->save(TalkModeConstant::GROUP_CHAT, $user_id, $group->id, [
'text' => '[入群通知]', 'text' => '[入群通知]',
'created_at' => date('Y-m-d H:i:s') '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)); 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, 'sender_id' => $user_id,
'receiver_id' => $group->id, 'receiver_id' => $group->id,
'talk_type' => TalkMode::GROUP_CHAT, 'talk_type' => TalkModeConstant::GROUP_CHAT,
'record_id' => $result->id 'record_id' => $result->id
])); ]));
@ -194,7 +194,7 @@ class GroupService extends BaseService
$updateArr = $insertArr = $updateArr1 = $insertArr1 = []; $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(); $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) ->where('receiver_id', $group_id)
->whereIn('user_id', $friend_ids) ->whereIn('user_id', $friend_ids)
->get(['id', 'user_id', 'is_delete']) ->get(['id', 'user_id', 'is_delete'])
@ -213,7 +213,7 @@ class GroupService extends BaseService
if (!isset($chatArr[$uid])) { if (!isset($chatArr[$uid])) {
$insertArr1[] = [ $insertArr1[] = [
'talk_type' => TalkMode::GROUP_CHAT, 'talk_type' => TalkModeConstant::GROUP_CHAT,
'user_id' => $uid, 'user_id' => $uid,
'receiver_id' => $group_id, 'receiver_id' => $group_id,
'created_at' => date('Y-m-d H:i:s'), 'created_at' => date('Y-m-d H:i:s'),
@ -252,7 +252,7 @@ class GroupService extends BaseService
} }
$result = TalkRecords::create([ $result = TalkRecords::create([
'talk_type' => TalkMode::GROUP_CHAT, 'talk_type' => TalkModeConstant::GROUP_CHAT,
'user_id' => 0, 'user_id' => 0,
'receiver_id' => $group_id, 'receiver_id' => $group_id,
'msg_type' => TalkMessageType::GROUP_INVITE_MESSAGE, 'msg_type' => TalkMessageType::GROUP_INVITE_MESSAGE,
@ -273,7 +273,7 @@ class GroupService extends BaseService
return false; return false;
} }
LastMessage::getInstance()->save(TalkMode::GROUP_CHAT, $user_id, $group_id, [ LastMessage::getInstance()->save(TalkModeConstant::GROUP_CHAT, $user_id, $group_id, [
'text' => '[入群通知]', 'text' => '[入群通知]',
'created_at' => date('Y-m-d H:i:s') '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)); 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, 'sender_id' => $user_id,
'receiver_id' => $group_id, 'receiver_id' => $group_id,
'talk_type' => TalkMode::GROUP_CHAT, 'talk_type' => TalkModeConstant::GROUP_CHAT,
'record_id' => $result->id 'record_id' => $result->id
])); ]));
@ -317,7 +317,7 @@ class GroupService extends BaseService
if ($count == 0) throw new Exception('更新记录失败...'); if ($count == 0) throw new Exception('更新记录失败...');
$result = TalkRecords::create([ $result = TalkRecords::create([
'talk_type' => TalkMode::GROUP_CHAT, 'talk_type' => TalkModeConstant::GROUP_CHAT,
'user_id' => 0, 'user_id' => 0,
'receiver_id' => $group_id, 'receiver_id' => $group_id,
'msg_type' => TalkMessageType::GROUP_INVITE_MESSAGE, 'msg_type' => TalkMessageType::GROUP_INVITE_MESSAGE,
@ -334,7 +334,7 @@ class GroupService extends BaseService
]); ]);
TalkList::where([ TalkList::where([
['talk_type', '=', TalkMode::GROUP_CHAT], ['talk_type', '=', TalkModeConstant::GROUP_CHAT],
['user_id', '=', $user_id], ['user_id', '=', $user_id],
['receiver_id', '=', $group_id], ['receiver_id', '=', $group_id],
])->update(['is_delete' => 1]); ])->update(['is_delete' => 1]);
@ -348,10 +348,10 @@ class GroupService extends BaseService
// 移出聊天室 // 移出聊天室
SocketRoom::getInstance()->delRoomMember(strval($group_id), strval($user_id)); 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, 'sender_id' => $user_id,
'receiver_id' => (int)$params['group_id'], 'receiver_id' => (int)$params['group_id'],
'talk_type' => TalkMode::GROUP_CHAT, 'talk_type' => TalkModeConstant::GROUP_CHAT,
'record_id' => $result->id 'record_id' => $result->id
])); ]));
@ -382,7 +382,7 @@ class GroupService extends BaseService
if ($count == 0) throw new Exception('更新记录失败...'); if ($count == 0) throw new Exception('更新记录失败...');
$result = TalkRecords::create([ $result = TalkRecords::create([
'talk_type' => TalkMode::GROUP_CHAT, 'talk_type' => TalkModeConstant::GROUP_CHAT,
'user_id' => 0, 'user_id' => 0,
'receiver_id' => $group_id, 'receiver_id' => $group_id,
'msg_type' => TalkMessageType::GROUP_INVITE_MESSAGE, 'msg_type' => TalkMessageType::GROUP_INVITE_MESSAGE,
@ -397,7 +397,7 @@ class GroupService extends BaseService
'user_ids' => implode(',', $member_ids) '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, 'is_delete' => 1,
'updated_at' => date('Y-m-d H:i:s') '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)); 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, 'sender_id' => $user_id,
'receiver_id' => $group_id, 'receiver_id' => $group_id,
'talk_type' => TalkMode::GROUP_CHAT, 'talk_type' => TalkModeConstant::GROUP_CHAT,
'record_id' => $result->id 'record_id' => $result->id
])); ]));
@ -461,7 +461,7 @@ class GroupService extends BaseService
$list = []; $list = [];
if ($items) { if ($items) {
$list = TalkList::query()->where('user_id', $user_id) $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')) ->whereIn('receiver_id', array_column($items, 'id'))
->get(['receiver_id', 'is_disturb'])->keyBy('receiver_id')->toArray(); ->get(['receiver_id', 'is_disturb'])->keyBy('receiver_id')->toArray();
} }

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
namespace App\Service\Message; namespace App\Service\Message;

View File

@ -1,18 +1,16 @@
<?php <?php
declare(strict_types=1);
namespace App\Service\Message; namespace App\Service\Message;
use App\Cache\LastMessage; use App\Cache\LastMessage;
use App\Cache\UnreadTalk; use App\Cache\UnreadTalk;
use App\Constants\TalkMessageEvent; use App\Constants\TalkEventConstant;
use App\Constants\TalkMessageType; 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\TalkRecords;
use App\Model\Group\Group;
use App\Model\UsersFriend;
use App\Service\SocketClientService; use App\Service\SocketClientService;
use App\Service\UserFriendService;
use App\Support\MessageProducer;
use App\Support\UserRelation; use App\Support\UserRelation;
use Swoole\Http\Response; use Swoole\Http\Response;
use Swoole\WebSocket\Frame; use Swoole\WebSocket\Frame;
@ -27,8 +25,8 @@ class ReceiveHandleService
// 消息事件绑定 // 消息事件绑定
const EVENTS = [ const EVENTS = [
TalkMessageEvent::EVENT_TALK => 'onTalk', TalkEventConstant::EVENT_TALK => 'onTalk',
TalkMessageEvent::EVENT_KEYBOARD => 'onKeyboard', TalkEventConstant::EVENT_KEYBOARD => 'onKeyboard',
]; ];
/** /**
@ -55,7 +53,7 @@ class ReceiveHandleService
if ($user_id != $data['sender_id']) return; 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']); $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); UnreadTalk::getInstance()->increment($result->user_id, $result->receiver_id);
} }
@ -87,7 +85,7 @@ class ReceiveHandleService
'created_at' => date('Y-m-d H:i:s') '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, 'sender_id' => $result->user_id,
'receiver_id' => $result->receiver_id, 'receiver_id' => $result->receiver_id,
'talk_type' => $result->talk_type, 'talk_type' => $result->talk_type,
@ -105,7 +103,7 @@ class ReceiveHandleService
*/ */
public function onKeyboard($server, Frame $frame, $data) 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'], 'sender_id' => (int)$data['sender_id'],
'receiver_id' => (int)$data['receiver_id'], 'receiver_id' => (int)$data['receiver_id'],
])); ]));

View File

@ -1,10 +1,11 @@
<?php <?php
declare(strict_types=1);
namespace App\Service\Message; namespace App\Service\Message;
use App\Cache\SocketRoom; use App\Cache\SocketRoom;
use App\Constants\TalkMessageEvent; use App\Constants\TalkEventConstant;
use App\Constants\TalkMode; use App\Constants\TalkModeConstant;
use App\Model\Talk\TalkRecords; use App\Model\Talk\TalkRecords;
use App\Model\Group\Group; use App\Model\Group\Group;
use App\Model\User; use App\Model\User;
@ -21,19 +22,19 @@ class SubscribeHandleService
*/ */
const EVENTS = [ const EVENTS = [
// 聊天消息事件 // 聊天消息事件
TalkMessageEvent::EVENT_TALK => '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 = []; $fds = [];
$groupInfo = null; $groupInfo = null;
if ($talk_type == TalkMode::PRIVATE_CHAT) { if ($talk_type == TalkModeConstant::PRIVATE_CHAT) {
$fds = array_merge( $fds = array_merge(
$this->clientService->findUserFds($sender_id), $this->clientService->findUserFds($sender_id),
$this->clientService->findUserFds($receiver_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) { foreach (SocketRoom::getInstance()->getRoomMembers(strval($receiver_id)) as $uid) {
$fds = array_merge($fds, $this->clientService->findUserFds(intval($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']); $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([ $this->socketPushNotify(array_unique($fds), json_encode([
TalkMessageEvent::EVENT_ONLINE_STATUS, [ TalkEventConstant::EVENT_ONLINE_STATUS, [
'user_id' => $user_id, 'user_id' => $user_id,
'status' => $status 'status' => $status
] ]
@ -179,10 +180,10 @@ class SubscribeHandleService
$record = TalkRecords::where('id', $data['data']['record_id'])->first(['id', 'talk_type', 'user_id', 'receiver_id']); $record = TalkRecords::where('id', $data['data']['record_id'])->first(['id', 'talk_type', 'user_id', 'receiver_id']);
$fds = []; $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->user_id));
$fds = array_merge($fds, $this->clientService->findUserFds($record->receiver_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)); $userIds = SocketRoom::getInstance()->getRoomMembers(strval($record->receiver_id));
foreach ($userIds as $uid) { foreach ($userIds as $uid) {
$fds = array_merge($fds, $this->clientService->findUserFds((int)$uid)); $fds = array_merge($fds, $this->clientService->findUserFds((int)$uid));
@ -190,7 +191,7 @@ class SubscribeHandleService
} }
$fds = array_unique($fds); $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, 'talk_type' => $record->talk_type,
'sender_id' => $record->user_id, 'sender_id' => $record->user_id,
'receiver_id' => $record->receiver_id, 'receiver_id' => $record->receiver_id,
@ -236,7 +237,7 @@ class SubscribeHandleService
'mobile' => $friendInfo->mobile, '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]));
} }
/** /**

View File

@ -5,7 +5,7 @@ namespace App\Service;
use App\Cache\LastMessage; use App\Cache\LastMessage;
use App\Cache\ServerRunID; use App\Cache\ServerRunID;
use App\Cache\UnreadTalk; use App\Cache\UnreadTalk;
use App\Constants\TalkMode; use App\Constants\TalkModeConstant;
use App\Model\Talk\TalkList; use App\Model\Talk\TalkList;
use Carbon\Carbon; use Carbon\Carbon;
@ -113,10 +113,10 @@ class TalkListService
$rows = TalkList::from('talk_list as list') $rows = TalkList::from('talk_list as list')
->leftJoin('users', function ($join) { ->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) { ->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.user_id', $user_id)
->where('list.is_delete', 0) ->where('list.is_delete', 0)
@ -137,13 +137,13 @@ class TalkListService
'updated_at' => Carbon::parse($item['updated_at'])->toDateTimeString(), '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['name'] = $item['nickname'];
$data['avatar'] = $item['user_avatar']; $data['avatar'] = $item['user_avatar'];
$data['unread_num'] = UnreadTalk::getInstance()->read($item['receiver_id'], $user_id); $data['unread_num'] = UnreadTalk::getInstance()->read($item['receiver_id'], $user_id);
$data['is_online'] = container()->get(SocketClientService::class)->isOnlineAll($item['receiver_id'], $runIdAll); $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']); $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['name'] = strval($item['group_name']);
$data['avatar'] = $item['group_avatar']; $data['avatar'] = $item['group_avatar'];
} }

View File

@ -5,16 +5,16 @@ namespace App\Service;
use App\Cache\LastMessage; use App\Cache\LastMessage;
use App\Cache\VoteCache; use App\Cache\VoteCache;
use App\Cache\VoteStatisticsCache; use App\Cache\VoteStatisticsCache;
use App\Constants\TalkMessageEvent; use App\Constants\TalkEventConstant;
use App\Constants\TalkMessageType; use App\Constants\TalkMessageType;
use App\Event\TalkEvent;
use App\Model\Group\GroupMember; use App\Model\Group\GroupMember;
use App\Model\Talk\TalkRecordsCode; use App\Model\Talk\TalkRecordsCode;
use App\Model\Talk\TalkRecordsVote; use App\Model\Talk\TalkRecordsVote;
use App\Model\Talk\TalkRecordsVoteAnswer; use App\Model\Talk\TalkRecordsVoteAnswer;
use App\Support\MessageProducer;
use App\Support\UserRelation; use App\Support\UserRelation;
use Exception; use Exception;
use App\Constants\MediaFileType; use App\Constants\MediaTypeConstant;
use App\Model\Talk\TalkRecords; use App\Model\Talk\TalkRecords;
use App\Model\Talk\TalkRecordsFile; use App\Model\Talk\TalkRecordsFile;
use Hyperf\DbConnection\Db; use Hyperf\DbConnection\Db;
@ -53,18 +53,18 @@ class TalkMessageService
return false; 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, 'sender_id' => $insert->user_id,
'receiver_id' => $insert->receiver_id, 'receiver_id' => $insert->receiver_id,
'talk_type' => $insert->talk_type, 'talk_type' => $insert->talk_type,
'record_id' => $insert->id '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; return true;
} }
@ -89,7 +89,7 @@ class TalkMessageService
} }
$file['record_id'] = $insert->id; $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'); $file['created_at'] = date('Y-m-d H:i:s');
if (!TalkRecordsFile::create($file)) { if (!TalkRecordsFile::create($file)) {
throw new Exception('插入聊天记录(代码消息)失败...'); throw new Exception('插入聊天记录(代码消息)失败...');
@ -101,18 +101,18 @@ class TalkMessageService
return false; 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, 'sender_id' => $insert->user_id,
'receiver_id' => $insert->receiver_id, 'receiver_id' => $insert->receiver_id,
'talk_type' => $insert->talk_type, 'talk_type' => $insert->talk_type,
'record_id' => $insert->id '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; return true;
} }
@ -154,19 +154,18 @@ class TalkMessageService
return false; return false;
} }
// 推送消息通知 LastMessage::getInstance()->save($insert->talk_type, $insert->user_id, $insert->receiver_id, [
MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ 'text' => '[投票消息]',
'created_at' => date('Y-m-d H:i:s')
]);
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [
'sender_id' => $insert->user_id, 'sender_id' => $insert->user_id,
'receiver_id' => $insert->receiver_id, 'receiver_id' => $insert->receiver_id,
'talk_type' => $insert->talk_type, 'talk_type' => $insert->talk_type,
'record_id' => $insert->id '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; return true;
} }

View File

@ -2,11 +2,11 @@
namespace App\Service; namespace App\Service;
use App\Constants\TalkMessageEvent; use App\Constants\TalkEventConstant;
use App\Constants\TalkMessageType; use App\Constants\TalkMessageType;
use App\Constants\TalkMode; use App\Constants\TalkModeConstant;
use App\Event\TalkEvent;
use App\Service\Message\FormatMessageService; use App\Service\Message\FormatMessageService;
use App\Support\MessageProducer;
use Exception; use Exception;
use App\Model\Group\Group; use App\Model\Group\Group;
use App\Model\Talk\TalkRecords; use App\Model\Talk\TalkRecords;
@ -53,7 +53,7 @@ class TalkService extends BaseService
$rowsSqlObj->where('talk_records.id', '<', $record_id); $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) { $rowsSqlObj->where(function ($query) use ($user_id, $receiver_id) {
$query->where([ $query->where([
['talk_records.user_id', '=', $user_id], ['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 []; 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 []; return [];
} }
@ -138,13 +138,13 @@ class TalkService extends BaseService
*/ */
public function removeRecords(int $user_id, int $talk_type, int $receiver_id, array $record_ids) 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) { $ids = TalkRecords::whereIn('id', $record_ids)->where(function ($query) use ($user_id, $receiver_id) {
$query->where([['user_id', '=', $user_id], ['receiver_id', '=', $receiver_id]]) $query->where([['user_id', '=', $user_id], ['receiver_id', '=', $receiver_id]])
->orWhere([['user_id', '=', $receiver_id], ['receiver_id', '=', $user_id]]); ->orWhere([['user_id', '=', $receiver_id], ['receiver_id', '=', $user_id]]);
})->where('talk_type', $talk_type)->pluck('id'); })->where('talk_type', $talk_type)->pluck('id');
} else {// 群聊信息 } 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; return false;
} }
@ -185,11 +185,11 @@ class TalkService extends BaseService
return [false, '已超过有效的撤回时间', []]; 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) { if ($result->user_id != $user_id && $result->receiver_id != $user_id) {
return [false, '非法操作', []]; 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)) { if (!Group::isMember($result->receiver_id, $user_id)) {
return [false, '非法操作', []]; return [false, '非法操作', []];
} }
@ -198,7 +198,7 @@ class TalkService extends BaseService
$result->is_revoke = 1; $result->is_revoke = 1;
$result->save(); $result->save();
MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_REVOKE_TALK, [ event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_REVOKE_TALK, [
'record_id' => $result->id 'record_id' => $result->id
])); ]));
@ -225,11 +225,11 @@ class TalkService extends BaseService
if (!$result) return []; 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) { if ($result->user_id != $user_id && $result->receiver_id != $user_id) {
return []; 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)) { if (!Group::isMember($result->receiver_id, $user_id)) {
return []; return [];
} }
@ -324,7 +324,7 @@ class TalkService extends BaseService
$sqlObj = TalkRecords::whereIn('id', $records_ids); $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 []; if (!container()->get(UserFriendService::class)->isFriend($user_id, $receiver_id, true)) return [];
$sqlObj = $sqlObj->where(function ($query) use ($user_id, $receiver_id) { $sqlObj = $sqlObj->where(function ($query) use ($user_id, $receiver_id) {
@ -339,7 +339,7 @@ class TalkService extends BaseService
} else { } else {
if (!Group::isMember($receiver_id, $user_id)) return []; 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(); $result = $sqlObj->get();

View File

@ -2,7 +2,7 @@
namespace App\Support; namespace App\Support;
use App\Constants\TalkMode; use App\Constants\TalkModeConstant;
use App\Model\Group\Group; use App\Model\Group\Group;
use App\Service\UserFriendService; use App\Service\UserFriendService;
@ -18,9 +18,9 @@ class UserRelation
*/ */
public static function isFriendOrGroupMember(int $user_id, int $receiver_id, int $talk_type) 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); 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); return Group::isMember($receiver_id, $user_id);
} }

View File

@ -1,11 +0,0 @@
-- 更新计划 --
1. 好友相关逻辑待开发
2. 投票消息功能待开发
3. 发送名片功能待开发
4. 推送登录通知待开发
5. 静态文件上传第三方待开发