初始化

main
gzydong 2020-11-21 22:47:21 +08:00
parent 2a955a841a
commit b05d25e1de
7 changed files with 151 additions and 44 deletions

View File

@ -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) {
$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']);
}
} else if ($result->msg_type == 5) {
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
])
];

View File

@ -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([], '已成功退出群组...');
}

View File

@ -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([], '转发成功...');
}

View File

@ -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)]);
}
/**
* 获取拆分文件信息
*

View File

@ -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'
];
}

View File

@ -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];
}

View File

@ -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,