From b05d25e1defe39e8877076921a68a080bb3b8268 Mon Sep 17 00:00:00 2001 From: gzydong Date: Sat, 21 Nov 2020 22:47:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Amqp/Consumer/ChatMessageConsumer.php | 51 ++++++++++++---- app/Controller/Api/V1/GroupController.php | 69 +++++++++++++++++----- app/Controller/Api/V1/TalkController.php | 42 ++++++++----- app/Controller/Api/V1/UploadController.php | 17 ++++++ app/Model/Chat/ChatRecordsForward.php | 3 +- app/Service/GroupService.php | 5 +- app/Service/TalkService.php | 8 ++- 7 files changed, 151 insertions(+), 44 deletions(-) diff --git a/app/Amqp/Consumer/ChatMessageConsumer.php b/app/Amqp/Consumer/ChatMessageConsumer.php index c88aaf0..b4d7d92 100644 --- a/app/Amqp/Consumer/ChatMessageConsumer.php +++ b/app/Amqp/Consumer/ChatMessageConsumer.php @@ -8,6 +8,8 @@ use App\Helper\PushMessageHelper; use App\Model\Chat\ChatRecord; use App\Model\Chat\ChatRecordsCode; use App\Model\Chat\ChatRecordsFile; +use App\Model\User; +use App\Model\Chat\ChatRecordsInvite; use App\Service\SocketFDService; use App\Service\SocketRoomService; use Hyperf\Amqp\Result; @@ -112,6 +114,9 @@ class ChatMessageConsumer extends ConsumerMessage return Result::ACK; } + /** + * @var ChatRecord + */ $result = ChatRecord::leftJoin('users', 'users.id', '=', 'chat_records.user_id') ->where('chat_records.id', $data['record_id']) ->first([ @@ -130,15 +135,39 @@ class ChatMessageConsumer extends ConsumerMessage $file = []; $code_block = []; - if ($result->msg_type == 2) { - $file = ChatRecordsFile::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']); - $file = $file ? $file->toArray() : []; - if ($file) { - $file['file_url'] = get_media_url($file['save_dir']); - } - } else if ($result->msg_type == 5) { - $code_block = ChatRecordsCode::where('record_id', $result->id)->first(['record_id', 'code_lang', 'code']); - $code_block = $code_block ? $code_block->toArray() : []; + $forward = []; + $invite = []; + switch ($result->msg_type) { + case 2://文件消息 + $file = ChatRecordsFile::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']); + $file = $file ? $file->toArray() : []; + if ($file) { + $file['file_url'] = get_media_url($file['save_dir']); + } + break; + case 3://入群消息/退群消息 + $notifyInfo = ChatRecordsInvite::where('record_id', $result->id)->first([ + 'record_id', 'type', 'operate_user_id', 'user_ids' + ]); + + $userInfo = User::where('id', $notifyInfo->operate_user_id)->first(['nickname', 'id']); + $membersIds = explode(',', $notifyInfo->user_ids); + + $invite = [ + 'type' => $notifyInfo->type, + 'operate_user' => ['id' => $userInfo->id, 'nickname' => $userInfo->nickname], + 'users' => User::select('id', 'nickname')->whereIn('id', $membersIds)->get()->toArray() + ]; + + unset($notifyInfo, $userInfo, $membersIds); + break; + case 4://会话记录消息 + + break; + case 5://代码块消息 + $code_block = ChatRecordsCode::where('record_id', $result->id)->first(['record_id', 'code_lang', 'code']); + $code_block = $code_block ? $code_block->toArray() : []; + break; } $msg = [ @@ -156,7 +185,9 @@ class ChatMessageConsumer extends ConsumerMessage "created_at" => $result->created_at, "content" => $result->content, "file" => $file, - "code_block" => $code_block + "code_block" => $code_block, + 'forward' => $forward, + 'invite' => $invite ]) ]; diff --git a/app/Controller/Api/V1/GroupController.php b/app/Controller/Api/V1/GroupController.php index d03f72f..64674e8 100644 --- a/app/Controller/Api/V1/GroupController.php +++ b/app/Controller/Api/V1/GroupController.php @@ -3,6 +3,8 @@ namespace App\Controller\Api\V1; use App\Model\UsersFriend; +use App\Service\SocketRoomService; +use Hyperf\Amqp\Producer; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; @@ -13,6 +15,7 @@ use App\Model\UsersChatList; use App\Model\Group\UsersGroup; use App\Model\Group\UsersGroupMember; use App\Model\Group\UsersGroupNotice; +use App\Amqp\Producer\ChatMessageProducer; /** * Class GroupController @@ -30,6 +33,18 @@ class GroupController extends CController */ public $groupService; + /** + * @Inject + * @var Producer + */ + private $producer; + + /** + * @Inject + * @var SocketRoomService + */ + private $socketRoomService; + /** * 创建群组 * @@ -47,7 +62,8 @@ class GroupController extends CController $friend_ids = array_filter(explode(',', $params['uids'])); - [$isTrue, $data] = $this->groupService->create($this->uid(), [ + $user_id = $this->uid(); + [$isTrue, $data] = $this->groupService->create($user_id, [ 'name' => $params['group_name'], 'avatar' => $params['avatar'] ?? '', 'profile' => $params['group_profile'] ?? '' @@ -57,8 +73,10 @@ class GroupController extends CController return $this->response->fail('创建群聊失败,请稍后再试...'); } - //群聊创建成功后需要创建聊天室并发送消息通知 - // ... 包装消息推送到队列 + // ...消息推送队列 + $this->producer->produce( + new ChatMessageProducer($user_id, $data['group_id'], 2, $data['record_id']) + ); return $this->response->success([ 'group_id' => $data['group_id'] @@ -101,14 +119,23 @@ class GroupController extends CController ]); $uids = array_filter(explode(',', $params['uids'])); + $uids = array_unique($uids); - [$isTrue, $record_id] = $this->groupService->invite($this->uid(), $params['group_id'], array_unique($uids)); + $user_id = $this->uid(); + [$isTrue, $record_id] = $this->groupService->invite($user_id, $params['group_id'], $uids); if (!$isTrue) { return $this->response->fail('邀请好友加入群聊失败...'); } - // 推送入群消息 - // ... + // 移出聊天室 + foreach ($uids as $uid) { + $this->socketRoomService->addRoomMember($uid, $params['group_id']); + } + + // ...消息推送队列 + $this->producer->produce( + new ChatMessageProducer($user_id, $params['group_id'], 2, $record_id) + ); return $this->response->success([], '好友已成功加入群聊...'); } @@ -125,13 +152,19 @@ class GroupController extends CController 'group_id' => 'required|integer' ]); - [$isTrue, $record_id] = $this->groupService->quit($this->uid(), $params['group_id']); + $user_id = $this->uid(); + [$isTrue, $record_id] = $this->groupService->quit($user_id, $params['group_id']); if (!$isTrue) { return $this->response->fail('退出群组失败...'); } - // 推送消息通知 - // ... + // 移出聊天室 + $this->socketRoomService->delRoomMember($params['group_id'], $user_id); + + // ...消息推送队列 + $this->producer->produce( + new ChatMessageProducer($user_id, $params['group_id'], 2, $record_id) + ); return $this->response->success([], '已成功退出群组...'); } @@ -178,13 +211,21 @@ class GroupController extends CController 'members_ids' => 'required|array' ]); - [$isTrue, $record_id] = $this->groupService->removeMember($params['group_id'], $this->uid(), $params['members_ids']); + $user_id = $this->uid(); + [$isTrue, $record_id] = $this->groupService->removeMember($params['group_id'], $user_id, $params['members_ids']); if (!$isTrue) { return $this->response->fail('群聊用户移除失败...'); } - // 推送消息通知 - // ... + // 移出聊天室 + foreach ($params['members_ids'] as $uid) { + $this->socketRoomService->delRoomMember($params['group_id'], $uid); + } + + // ...消息推送队列 + $this->producer->produce( + new ChatMessageProducer($user_id, $params['group_id'], 2, $record_id) + ); return $this->response->success([], '已成功退出群组...'); } @@ -387,7 +428,7 @@ class GroupController extends CController ]); return $result - ? $this->response->success([],'修改群公告信息成功...') + ? $this->response->success([], '修改群公告信息成功...') : $this->response->fail('修改群公告信息成功...'); } @@ -419,7 +460,7 @@ class GroupController extends CController ]); return $result - ? $this->response->success([],'公告删除成功...') + ? $this->response->success([], '公告删除成功...') : $this->response->fail('公告删除失败...'); } } diff --git a/app/Controller/Api/V1/TalkController.php b/app/Controller/Api/V1/TalkController.php index fc9de91..150e836 100644 --- a/app/Controller/Api/V1/TalkController.php +++ b/app/Controller/Api/V1/TalkController.php @@ -189,8 +189,8 @@ class TalkController extends CController $isTrue = UsersChatList::notDisturbItem($this->uid(), $params['receive_id'], $params['type'], $params['not_disturb']); return $isTrue - ? $this->response->success([], '对话列表置顶成功...') - : $this->response->fail('对话列表置顶失败...'); + ? $this->response->success([], '免打扰设置成功...') + : $this->response->fail('免打扰设置失败...'); } /** @@ -279,19 +279,29 @@ class TalkController extends CController //转发方方式[1:逐条转发;2:合并转发] 'forward_mode' => 'required|in:1,2', //转发的好友的ID - 'receive_user_ids' => 'required|array', +// 'receive_user_ids' => 'array', //转发的群聊ID - 'receive_group_ids' => 'required|array', +// 'receive_group_ids' => 'array', ]); $user_id = $this->uid(); - $items = array_merge( - array_map(function ($friend_id) { + + $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 ['source' => 1, 'id' => $friend_id]; - }, $params['receive_user_ids']), - array_map(function ($group_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 ['source' => 2, 'id' => $group_id]; - }, $params['receive_group_ids']) + }, $params['receive_group_ids']); + } + + $items = array_merge( + $receive_user_ids, + $receive_group_ids ); if ($params['forward_mode'] == 1) {//单条转发 @@ -304,14 +314,18 @@ class TalkController extends CController return $this->response->fail('转发失败...'); } - if ($params['receive_user_ids']) { - foreach ($params['receive_user_ids'] as $v) { - $this->unreadTalkCache->setInc($v, $user_id); + if ($receive_user_ids) { + foreach ($receive_user_ids as $v) { + $this->unreadTalkCache->setInc($v['id'], $user_id); } } - //这里需要调用WebSocket推送接口 - // ... + // ...消息推送队列 + foreach ($ids as $value) { + $this->producer->produce( + new ChatMessageProducer($user_id, $value['receive_id'], $value['source'], $value['record_id']) + ); + } return $this->response->success([], '转发成功...'); } diff --git a/app/Controller/Api/V1/UploadController.php b/app/Controller/Api/V1/UploadController.php index 799d3f9..47d3a8a 100644 --- a/app/Controller/Api/V1/UploadController.php +++ b/app/Controller/Api/V1/UploadController.php @@ -35,6 +35,23 @@ class UploadController extends CController */ private $splitUploadService; + /** + * 图片文件流上传接口 + * + * @RequestMapping(path="file-stream", methods="post") + * + */ + public function fileStream() + { + $fileStream = $this->request->post('fileStream', ''); + $data = base64_decode(str_replace(['data:image/png;base64,', ' '], ['', '+'], $fileStream)); + + $path = '/media/images/avatar/' . date('Ymd') . '/' . uniqid() . date('His') . '.png'; + $this->uploadService->makeDirectory($this->uploadService->driver('/media/images/avatar/' . date('Ymd') . '/')); + @file_put_contents($this->uploadService->driver($path), $data); + return $this->response->success(['avatar' => get_media_url($path)]); + } + /** * 获取拆分文件信息 * diff --git a/app/Model/Chat/ChatRecordsForward.php b/app/Model/Chat/ChatRecordsForward.php index febd8c7..e2cd4b4 100644 --- a/app/Model/Chat/ChatRecordsForward.php +++ b/app/Model/Chat/ChatRecordsForward.php @@ -33,7 +33,7 @@ class ChatRecordsForward extends BaseModel * @var array */ protected $fillable = [ - 'record_id', 'user_id', 'created_at' + 'record_id', 'user_id', 'records_id','text','created_at' ]; /** @@ -45,6 +45,5 @@ class ChatRecordsForward extends BaseModel 'id' => 'integer', 'record_id' => 'integer', 'user_id' => 'integer', - 'created_at' => 'datetime' ]; } diff --git a/app/Service/GroupService.php b/app/Service/GroupService.php index 57fe6eb..9cdbdbd 100644 --- a/app/Service/GroupService.php +++ b/app/Service/GroupService.php @@ -2,6 +2,7 @@ namespace App\Service; +use App\Cache\LastMsgCache; use App\Model\Chat\ChatRecord; use App\Model\Chat\ChatRecordsInvite; use App\Model\Group\UsersGroup; @@ -100,7 +101,7 @@ class GroupService extends BaseService } // 设置群聊消息缓存 - //LastMsgCache::set(['created_at' => date('Y-m-d H:i:s'), 'text' => '入群通知'], $insRes->id, 0); + LastMsgCache::set(['created_at' => date('Y-m-d H:i:s'), 'text' => '入群通知'], $insRes->id, 0); return [true, ['record_id' => $result->id, 'group_id' => $insRes->id]]; } @@ -221,7 +222,7 @@ class GroupService extends BaseService return [false, 0]; } - //LastMsgCache::set(['created_at' => date('Y-m-d H:i:s'), 'text' => '入群通知'], $group_id, 0); + LastMsgCache::set(['created_at' => date('Y-m-d H:i:s'), 'text' => '入群通知'], $group_id, 0); return [true, $result->id]; } diff --git a/app/Service/TalkService.php b/app/Service/TalkService.php index e16bd50..2570148 100644 --- a/app/Service/TalkService.php +++ b/app/Service/TalkService.php @@ -521,7 +521,7 @@ class TalkService extends BaseService if ($source == 2) {//群聊消息 //判断是否是群聊成员 if (!UsersGroup::isMember($receive_id, $user_id)) { - return []; + return false; } $sqlObj = $sqlObj->where('receive_id', $receive_id)->whereIn('msg_type', $msg_type)->where('source', 2)->where('is_revoke', 0); @@ -591,7 +591,11 @@ class TalkService extends BaseService throw new Exception('插入消息失败'); } - $insRecordIds[] = $res->id; + $insRecordIds[] = [ + 'record_id' => $res->id, + 'receive_id' => $item['id'], + 'source' => $item['source'] + ]; if (!ChatRecordsForward::create([ 'record_id' => $res->id,