优化代码

main
gzydong 2021-07-08 23:44:43 +08:00
parent 6eb881d679
commit d40d06091e
10 changed files with 313 additions and 366 deletions

View File

@ -10,7 +10,8 @@
namespace App\Controller\Api\V1; namespace App\Controller\Api\V1;
use App\Model\UsersFriend; use App\Constants\TalkMode;
use App\Service\TalkListService;
use App\Service\UserService; use App\Service\UserService;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
@ -20,7 +21,6 @@ use App\Middleware\JWTAuthMiddleware;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use App\Service\ContactsService; use App\Service\ContactsService;
use App\Service\SocketClientService; use App\Service\SocketClientService;
use App\Model\TalkList;
use App\Cache\FriendApply; use App\Cache\FriendApply;
use App\Cache\FriendRemark; use App\Cache\FriendRemark;
use App\Cache\ServerRunID; use App\Cache\ServerRunID;
@ -77,9 +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);
TalkList::delItem($user_id, $params['friend_id'], 2);
TalkList::delItem($params['friend_id'], $user_id, 2);
// TODO 推送消息(待完善) // TODO 推送消息(待完善)

View File

@ -19,12 +19,10 @@ use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware; use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\JWTAuthMiddleware; use App\Middleware\JWTAuthMiddleware;
use App\Model\UsersFriend;
use App\Model\TalkList; use App\Model\TalkList;
use App\Model\Group\Group; use App\Model\Group\Group;
use App\Model\Group\GroupMember; use App\Model\Group\GroupMember;
use App\Model\Group\GroupNotice; use App\Model\Group\GroupNotice;
use App\Amqp\Producer\ChatMessageProducer;
use App\Service\GroupService; use App\Service\GroupService;
use App\Constants\TalkMessageEvent; use App\Constants\TalkMessageEvent;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;

View File

@ -22,9 +22,9 @@ use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\JWTAuthMiddleware; use App\Middleware\JWTAuthMiddleware;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use App\Model\User; use App\Model\User;
use App\Model\TalkList;
use App\Model\Group\Group; use App\Model\Group\Group;
use App\Service\TalkService; use App\Service\TalkService;
use App\Service\TalkListService;
/** /**
* Class TalkController * Class TalkController
@ -41,6 +41,12 @@ class TalkController extends CController
*/ */
public $talkService; public $talkService;
/**
* @Inject
* @var TalkListService
*/
public $talkListService;
/** /**
* 获取用户对话列表 * 获取用户对话列表
* @RequestMapping(path="list", methods="get") * @RequestMapping(path="list", methods="get")
@ -53,10 +59,12 @@ class TalkController extends CController
// 读取用户的未读消息列表 // 读取用户的未读消息列表
if ($list = UnreadTalk::getInstance()->reads($user_id)) { if ($list = UnreadTalk::getInstance()->reads($user_id)) {
$this->talkService->updateUnreadTalkList($user_id, $list); foreach ($list as $friend_id => $num) {
$this->talkListService->create($user_id, $friend_id, TalkMode::PRIVATE_CHAT);
}
} }
return $this->response->success($this->talkService->talks($user_id)); return $this->response->success($this->talkListService->getTalkList($user_id));
} }
/** /**
@ -78,7 +86,7 @@ class TalkController extends CController
return $this->response->fail('暂不属于好友关系或群聊成员,无法进行聊天!'); return $this->response->fail('暂不属于好友关系或群聊成员,无法进行聊天!');
} }
$result = TalkList::addItem($user_id, $params['receiver_id'], $params['talk_type']); $result = $this->talkListService->create($user_id, $params['receiver_id'], $params['talk_type']);
if (!$result) return $this->response->fail('创建失败!'); if (!$result) return $this->response->fail('创建失败!');
$data = [ $data = [
@ -129,7 +137,7 @@ class TalkController extends CController
'list_id' => 'required|integer|min:0' 'list_id' => 'required|integer|min:0'
]); ]);
return TalkList::delItem($this->uid(), $params['list_id']) return $this->talkListService->delete($this->uid(), $params['list_id'])
? $this->response->success([], '对话列表删除成功...') ? $this->response->success([], '对话列表删除成功...')
: $this->response->fail('对话列表删除失败!'); : $this->response->fail('对话列表删除失败!');
} }
@ -148,7 +156,7 @@ class TalkController extends CController
'type' => 'required|in:1,2', 'type' => 'required|in:1,2',
]); ]);
return TalkList::topItem($this->uid(), $params['list_id'], $params['type'] == 1) return $this->talkListService->top($this->uid(), $params['list_id'], $params['type'] == 1)
? $this->response->success([], '对话列表置顶(或取消置顶)成功...') ? $this->response->success([], '对话列表置顶(或取消置顶)成功...')
: $this->response->fail('对话列表置顶(或取消置顶)失败!'); : $this->response->fail('对话列表置顶(或取消置顶)失败!');
} }
@ -168,9 +176,7 @@ class TalkController extends CController
'is_disturb' => 'required|in:0,1', 'is_disturb' => 'required|in:0,1',
]); ]);
$isTrue = TalkList::setNotDisturb($this->uid(), $params['receiver_id'], $params['talk_type'], $params['is_disturb']); return $this->talkListService->disturb($this->uid(), $params['receiver_id'], $params['talk_type'], $params['is_disturb'])
return $isTrue
? $this->response->success([], '免打扰设置成功...') ? $this->response->success([], '免打扰设置成功...')
: $this->response->fail('免打扰设置失败!'); : $this->response->fail('免打扰设置失败!');
} }

View File

@ -2,14 +2,13 @@
namespace App\Controller\Api\V1; namespace App\Controller\Api\V1;
use App\Amqp\Producer\ChatMessageProducer;
use App\Cache\LastMessage; use App\Cache\LastMessage;
use App\Cache\UnreadTalk; use App\Cache\UnreadTalk;
use App\Constants\TalkMessageEvent; use App\Constants\TalkMessageEvent;
use App\Constants\TalkMessageType;
use App\Constants\TalkMode; use App\Constants\TalkMode;
use App\Model\EmoticonItem; use App\Model\EmoticonItem;
use App\Model\FileSplitUpload; use App\Model\FileSplitUpload;
use App\Service\TalkMessageService;
use App\Support\MessageProducer; use App\Support\MessageProducer;
use App\Support\UserRelation; use App\Support\UserRelation;
use App\Service\EmoticonService; use App\Service\EmoticonService;
@ -36,6 +35,12 @@ class TalkMessageController extends CController
*/ */
public $talkService; public $talkService;
/**
* @Inject
* @var TalkMessageService
*/
public $talkMessageService;
/** /**
* 发送代码块消息 * 发送代码块消息
* @RequestMapping(path="code", methods="post") * @RequestMapping(path="code", methods="post")
@ -55,9 +60,8 @@ class TalkMessageController extends CController
return $this->response->fail('暂不属于好友关系或群聊成员,无法发送聊天消息!'); return $this->response->fail('暂不属于好友关系或群聊成员,无法发送聊天消息!');
} }
$record_id = $this->talkService->createCodeMessage([ $record_id = $this->talkMessageService->insertCodeMessage([
'talk_type' => $params['talk_type'], 'talk_type' => $params['talk_type'],
'msg_type' => TalkMessageType::CODE_MESSAGE,
'user_id' => $user_id, 'user_id' => $user_id,
'receiver_id' => $params['receiver_id'], 'receiver_id' => $params['receiver_id'],
], [ ], [
@ -120,9 +124,8 @@ class TalkMessageController extends CController
} }
// 创建图片消息记录 // 创建图片消息记录
$record_id = $this->talkService->createImgMessage([ $record_id = $this->talkMessageService->insertFileMessage([
'talk_type' => $params['talk_type'], 'talk_type' => $params['talk_type'],
'msg_type' => TalkMessageType::FILE_MESSAGE,
'user_id' => $user_id, 'user_id' => $user_id,
'receiver_id' => $params['receiver_id'], 'receiver_id' => $params['receiver_id'],
], [ ], [
@ -182,9 +185,8 @@ class TalkMessageController extends CController
return $this->response->fail('文件不存在...'); return $this->response->fail('文件不存在...');
} }
$record_id = $this->talkService->createFileMessage([ $record_id = $this->talkMessageService->insertFileMessage([
'talk_type' => $params['talk_type'], 'talk_type' => $params['talk_type'],
'msg_type' => TalkMessageType::FILE_MESSAGE,
'user_id' => $user_id, 'user_id' => $user_id,
'receiver_id' => $params['receiver_id'] 'receiver_id' => $params['receiver_id']
], [ ], [
@ -248,9 +250,8 @@ class TalkMessageController extends CController
if (!$emoticon) return $this->response->fail('表情不存在!'); if (!$emoticon) return $this->response->fail('表情不存在!');
$record_id = $this->talkService->createEmoticonMessage([ $record_id = $this->talkMessageService->insertFileMessage([
'talk_type' => $params['talk_type'], 'talk_type' => $params['talk_type'],
'msg_type' => TalkMessageType::FILE_MESSAGE,
'user_id' => $user_id, 'user_id' => $user_id,
'receiver_id' => $params['receiver_id'], 'receiver_id' => $params['receiver_id'],
], [ ], [

View File

@ -49,118 +49,4 @@ class TalkList extends BaseModel
'created_at' => 'datetime', 'created_at' => 'datetime',
'updated_at' => 'datetime' 'updated_at' => 'datetime'
]; ];
/**
* 创建聊天列表记录
*
* @param int $user_id 用户ID
* @param int $receiver_id 接收者ID
* @param int $talk_type 创建类型[1:私聊;2:群聊;]
* @return array
*/
public static function addItem(int $user_id, int $receiver_id, int $talk_type)
{
$result = self::query()->where([
['user_id', '=', $user_id],
['talk_type', '=', $talk_type],
['receiver_id', '=', $receiver_id],
])->first();
if (!$result) {
$result = self::query()->create([
'talk_type' => $talk_type,
'user_id' => $user_id,
'receiver_id' => $receiver_id,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
]);
}
$result->is_top = 0;
$result->is_delete = 0;
$result->is_disturb = 0;
$result->updated_at = date('Y-m-d H:i:s');
$result->save();
return [
'id' => $result->id,
'talk_type' => $result->talk_type,
'receiver_id' => $result->receiver_id,
];
}
/**
* 聊天对话列表置顶操作
*
* @param int $user_id 用户ID
* @param int $list_id 对话列表ID
* @param bool $is_top 是否置顶true: false:否)
* @return bool
*/
public static function topItem(int $user_id, int $list_id, $is_top = true)
{
return (bool)self::query()->where([
['id', '=', $list_id],
['user_id', '=', $user_id],
])->update([
'is_top' => $is_top ? 1 : 0,
'updated_at' => date('Y-m-d H:i:s')
]);
}
/**
* 删除聊天列表
*
* @param int $user_id 用户ID
* @param int $id 聊天列表ID、好友ID或群聊ID
* @param int $type ID类型[1:聊天列表ID;2:好友ID;3:群聊ID;]
* @return bool
*/
public static function delItem(int $user_id, int $id, $type = 1)
{
$model = self::query();
if ($type == 1) {
$model->where('id', $id)->where('user_id', $user_id);
} else {
$model->where([
['talk_type', '=', $type == 2 ? 1 : 2],
['user_id', '=', $user_id],
['receiver_id', '=', $id],
]);
}
return (bool)$model->update([
'is_delete' => 0,
'updated_at' => date('Y-m-d H:i:s')
]);
}
/**
* 设置消息免打扰
*
* @param int $user_id 用户ID
* @param int $receiver_id 接收者ID
* @param int $talk_type 接收者类型[1:好友;2:群组;]
* @param int $is_disturb 是否免打扰
* @return boolean
*/
public static function setNotDisturb(int $user_id, int $receiver_id, int $talk_type, int $is_disturb)
{
$result = self::query()
->where([
['user_id', '=', $user_id],
['talk_type', '=', $talk_type],
['receiver_id', '=', $receiver_id],
])
->first(['id', 'is_disturb']);
if (!$result || $is_disturb == $result->is_disturb) {
return false;
}
return (bool)self::query()->where('id', $result->id)->update([
'is_disturb' => $is_disturb,
'updated_at' => date('Y-m-d H:i:s')
]);
}
} }

View File

@ -40,6 +40,8 @@ class ContactsService extends BaseService
if ($res) redis()->del("good_friends:{$user_id}_{$friend_id}"); if ($res) redis()->del("good_friends:{$user_id}_{$friend_id}");
return $res; return $res;
} }

View File

@ -0,0 +1,13 @@
<?php
namespace App\Service;
class PushMessageService
{
public function loginMessage(int $user_id, array $params)
{
}
}

View File

@ -0,0 +1,193 @@
<?php
namespace App\Service;
use App\Cache\LastMessage;
use App\Cache\ServerRunID;
use App\Cache\UnreadTalk;
use App\Constants\TalkMode;
use App\Model\TalkList;
use App\Model\UsersFriend;
class TalkListService
{
/**
* 创建聊天列表记录
*
* @param int $user_id 用户ID
* @param int $receiver_id 接收者ID
* @param int $talk_type 创建类型[1:私聊;2:群聊;]
* @return array
*/
public function create(int $user_id, int $receiver_id, int $talk_type)
{
$result = TalkList::updateOrCreate([
'talk_type' => $talk_type,
'user_id' => $user_id,
'receiver_id' => $receiver_id,
], [
'is_top' => 0,
'is_delete' => 0,
'is_disturb' => 0,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
]);
return [
'id' => $result->id,
'talk_type' => $result->talk_type,
'receiver_id' => $result->receiver_id,
];
}
/**
* 聊天对话列表置顶操作
*
* @param int $user_id 用户ID
* @param int $list_id 会话列表ID
* @param bool $is_top 是否置顶true: false:否)
* @return bool
*/
public function top(int $user_id, int $list_id, $is_top = true)
{
return (bool)TalkList::query()->where([
['id', '=', $list_id],
['user_id', '=', $user_id],
])->update([
'is_top' => $is_top ? 1 : 0,
'updated_at' => date('Y-m-d H:i:s')
]);
}
/**
* 删除会话列表
*
* @param int $user_id 用户ID
* @param int $list_id 会话列表ID
* @return bool
*/
public function delete(int $user_id, int $list_id)
{
return (bool)TalkList::query()->where([
['id', '=', $list_id],
['user_id', '=', $user_id],
])->update([
'is_delete' => 1,
'updated_at' => date('Y-m-d H:i:s')
]);
}
/**
* 删除会话列表
*
* @param int $user_id 用户ID
* @param int $receiver_id 接受者ID
* @param int $talk_type 对话类型
* @return bool
*/
public function deleteByType(int $user_id, int $receiver_id, int $talk_type)
{
return (bool)TalkList::query()->where([
['user_id', '=', $user_id],
['talk_type', '=', $talk_type],
['receiver_id', '=', $receiver_id],
])->update([
'is_delete' => 1,
'updated_at' => date('Y-m-d H:i:s')
]);
}
/**
* 获取用户的聊天列表
*
* @param int $user_id 用户ID
* @return array
*/
public function getTalkList(int $user_id)
{
$filed = [
'list.id', 'list.talk_type', 'list.receiver_id', 'list.updated_at', 'list.is_disturb', 'list.is_top',
'users.avatar as user_avatar', 'users.nickname',
'group.group_name', 'group.avatar as group_avatar'
];
$rows = TalkList::from('talk_list as list')
->leftJoin('users', function ($join) {
$join->on('users.id', '=', 'list.receiver_id')->where('list.talk_type', '=', TalkMode::PRIVATE_CHAT);
})
->leftJoin('group', function ($join) {
$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)
->orderBy('list.updated_at', 'desc')
->get($filed)
->toArray();
if (!$rows) return [];
$socketFDService = make(SocketClientService::class);
$runIdAll = ServerRunID::getInstance()->getServerRunIdAll();
return array_map(function ($item) use ($user_id, $socketFDService, $runIdAll) {
$data['id'] = $item['id'];
$data['talk_type'] = $item['talk_type'];
$data['receiver_id'] = $item['receiver_id'];
$data['avatar'] = ''; // 默认头像
$data['name'] = ''; // 对方昵称/群名称
$data['remark_name'] = ''; // 好友备注
$data['unread_num'] = 0; // 未读消息
$data['is_online'] = false; // 是否在线
$data['is_top'] = $item['is_top'];
$data['is_disturb'] = $item['is_disturb'];
$data['msg_text'] = '......';
$data['updated_at'] = $item['updated_at'] ?: '2020-01-01 00:00:00';
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);
$data['is_online'] = $socketFDService->isOnlineAll($item['receiver_id'], $runIdAll);
$data['remark_name'] = UsersFriend::getFriendRemark($user_id, (int)$item['receiver_id']);
} else {
$data['name'] = strval($item['group_name']);
$data['avatar'] = $item['group_avatar'];
}
$records = LastMessage::getInstance()->read($data['talk_type'], $user_id, $data['receiver_id']);
if ($records) {
$data['msg_text'] = $records['text'];
$data['updated_at'] = $records['created_at'];
}
return $data;
}, $rows);
}
/**
* 设置消息免打扰
*
* @param int $user_id 用户ID
* @param int $receiver_id 接收者ID
* @param int $talk_type 接收者类型[1:好友;2:群组;]
* @param int $is_disturb 是否免打扰[0:;1:;]
* @return boolean
*/
public function disturb(int $user_id, int $receiver_id, int $talk_type, int $is_disturb)
{
$result = TalkList::query()->where([
['user_id', '=', $user_id],
['talk_type', '=', $talk_type],
['receiver_id', '=', $receiver_id],
])->first(['id', 'is_disturb']);
if (!$result || $is_disturb == $result->is_disturb) {
return false;
}
return (bool)TalkList::query()->where('id', $result->id)->update([
'is_disturb' => $is_disturb,
'updated_at' => date('Y-m-d H:i:s')
]);
}
}

View File

@ -2,8 +2,84 @@
namespace App\Service; namespace App\Service;
use App\Constants\TalkMessageType;
use App\Model\Chat\TalkRecordsCode;
use Exception;
use App\Constants\MediaFileType;
use App\Model\Chat\TalkRecords;
use App\Model\Chat\TalkRecordsFile;
use Hyperf\DbConnection\Db;
class TalkMessageService class TalkMessageService
{ {
/**
* 创建代码块消息
*
* @param array $message
* @param array $code
* @return bool|int
*/
public function insertCodeMessage(array $message, array $code)
{
Db::beginTransaction();
try {
$message['msg_type'] = TalkMessageType::CODE_MESSAGE;
$message['created_at'] = date('Y-m-d H:i:s');
$message['updated_at'] = date('Y-m-d H:i:s');
$insert = TalkRecords::create($message);
if (!$insert) {
throw new Exception('插入聊天记录失败...');
}
$code['record_id'] = $insert->id;
$code['created_at'] = date('Y-m-d H:i:s');
if (!TalkRecordsCode::create($code)) {
throw new Exception('插入聊天记录(代码消息)失败...');
}
Db::commit();
} catch (Exception $e) {
Db::rollBack();
return false;
}
return $insert->id;
}
/**
* 创建文件类消息
*
* @param array $message
* @param array $file
* @return bool|int
*/
public function insertFileMessage(array $message, array $file)
{
Db::beginTransaction();
try {
$message['msg_type'] = TalkMessageType::FILE_MESSAGE;
$message['created_at'] = date('Y-m-d H:i:s');
$message['updated_at'] = date('Y-m-d H:i:s');
$insert = TalkRecords::create($message);
if (!$insert) {
throw new Exception('插入聊天记录失败...');
}
$file['record_id'] = $insert->id;
$file['file_type'] = MediaFileType::getMediaType($file['file_suffix']);
$file['created_at'] = date('Y-m-d H:i:s');
if (!TalkRecordsFile::create($file)) {
throw new Exception('插入聊天记录(代码消息)失败...');
}
Db::commit();
} catch (Exception $e) {
Db::rollBack();
return false;
}
return $insert->id;
}
} }

View File

@ -2,13 +2,10 @@
namespace App\Service; namespace App\Service;
use App\Cache\ServerRunID;
use App\Constants\MediaFileType;
use App\Constants\TalkMessageType; use App\Constants\TalkMessageType;
use App\Constants\TalkMode; use App\Constants\TalkMode;
use Exception; use Exception;
use App\Model\User; use App\Model\User;
use App\Model\TalkList;
use App\Model\UsersFriend; use App\Model\UsersFriend;
use App\Model\Group\Group; use App\Model\Group\Group;
use App\Model\Chat\TalkRecords; use App\Model\Chat\TalkRecords;
@ -18,102 +15,11 @@ use App\Model\Chat\TalkRecordsForward;
use App\Model\Chat\TalkRecordsInvite; use App\Model\Chat\TalkRecordsInvite;
use App\Traits\PagingTrait; use App\Traits\PagingTrait;
use Hyperf\DbConnection\Db; use Hyperf\DbConnection\Db;
use App\Cache\FriendRemark;
use App\Cache\LastMessage;
use App\Cache\UnreadTalk;
class TalkService extends BaseService class TalkService extends BaseService
{ {
use PagingTrait; use PagingTrait;
/**
* 获取用户的聊天列表
*
* @param int $user_id 用户ID
* @return array
*/
public function talks(int $user_id)
{
$filed = [
'list.id', 'list.talk_type', 'list.receiver_id', 'list.updated_at', 'list.is_disturb', 'list.is_top',
'users.avatar as user_avatar', 'users.nickname',
'group.group_name', 'group.avatar as group_avatar'
];
$rows = TalkList::from('talk_list as list')
->leftJoin('users', function ($join) {
$join->on('users.id', '=', 'list.receiver_id')->where('list.talk_type', '=', TalkMode::PRIVATE_CHAT);
})
->leftJoin('group', function ($join) {
$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)
->orderBy('list.updated_at', 'desc')
->get($filed)
->toArray();
if (!$rows) return [];
$socketFDService = make(SocketClientService::class);
$runIdAll = ServerRunID::getInstance()->getServerRunIdAll();
return array_map(function ($item) use ($user_id, $socketFDService, $runIdAll) {
$data['id'] = $item['id'];
$data['talk_type'] = $item['talk_type'];
$data['receiver_id'] = $item['receiver_id'];
$data['avatar'] = ''; // 默认头像
$data['name'] = ''; // 对方昵称/群名称
$data['remark_name'] = ''; // 好友备注
$data['unread_num'] = 0; // 未读消息
$data['is_online'] = false; // 是否在线
$data['is_top'] = $item['is_top'];
$data['is_disturb'] = $item['is_disturb'];
$data['msg_text'] = '......';
$data['updated_at'] = $item['updated_at'] ?: '2020-01-01 00:00:00';
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);
$data['is_online'] = $socketFDService->isOnlineAll($item['receiver_id'], $runIdAll);
$data['remark_name'] = UsersFriend::getFriendRemark($user_id, (int)$item['receiver_id']);
} else {
$data['name'] = strval($item['group_name']);
$data['avatar'] = $item['group_avatar'];
}
$records = LastMessage::getInstance()->read($data['talk_type'], $user_id, $data['receiver_id']);
if ($records) {
$data['msg_text'] = $records['text'];
$data['updated_at'] = $records['created_at'];
}
return $data;
}, $rows);
}
/**
* 同步未读的消息到数据库中
*
* @param int $user_id 用户ID
* @param $data
*/
public function updateUnreadTalkList(int $user_id, $data)
{
foreach ($data as $friend_id => $num) {
TalkList::updateOrCreate([
'talk_type' => TalkMode::PRIVATE_CHAT,
'user_id' => $user_id,
'receiver_id' => $friend_id,
], [
'is_delete' => 0,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
]);
}
}
/** /**
* 处理聊天记录信息 * 处理聊天记录信息
* *
@ -672,136 +578,4 @@ class TalkService extends BaseService
$rows = $rowsSqlObj->orderBy('talk_records.id', 'desc')->forPage($page, $page_size)->get()->toArray(); $rows = $rowsSqlObj->orderBy('talk_records.id', 'desc')->forPage($page, $page_size)->get()->toArray();
return $this->getPagingRows($this->handleChatRecords($rows), $count, $page, $page_size); return $this->getPagingRows($this->handleChatRecords($rows), $count, $page, $page_size);
} }
/**
* 创建图片消息
*
* @param array $message
* @param array $fileInfo
* @return bool|int
*/
public function createImgMessage(array $message, array $fileInfo)
{
Db::beginTransaction();
try {
$message['created_at'] = date('Y-m-d H:i:s');
$insert = TalkRecords::create($message);
if (!$insert) {
throw new Exception('插入聊天记录失败...');
}
$fileInfo['record_id'] = $insert->id;
$fileInfo['file_type'] = MediaFileType::getMediaType($fileInfo['file_suffix']);
$fileInfo['created_at'] = date('Y-m-d H:i:s');
if (!TalkRecordsFile::create($fileInfo)) {
throw new Exception('插入聊天记录(文件消息)失败...');
}
Db::commit();
} catch (Exception $e) {
Db::rollBack();
return false;
}
return $insert->id;
}
/**
* 创建代码块消息
*
* @param array $message
* @param array $codeBlock
* @return bool|int
*/
public function createCodeMessage(array $message, array $codeBlock)
{
Db::beginTransaction();
try {
$message['created_at'] = date('Y-m-d H:i:s');
$insert = TalkRecords::create($message);
if (!$insert) {
throw new Exception('插入聊天记录失败...');
}
$codeBlock['record_id'] = $insert->id;
$codeBlock['created_at'] = date('Y-m-d H:i:s');
if (!TalkRecordsCode::create($codeBlock)) {
throw new Exception('插入聊天记录(代码消息)失败...');
}
Db::commit();
} catch (Exception $e) {
Db::rollBack();
return false;
}
return $insert->id;
}
/**
* 创建代码块消息
*
* @param array $message
* @param array $emoticon
* @return bool|int
*/
public function createEmoticonMessage(array $message, array $emoticon)
{
Db::beginTransaction();
try {
$message['created_at'] = date('Y-m-d H:i:s');
$insert = TalkRecords::create($message);
if (!$insert) {
throw new Exception('插入聊天记录失败...');
}
$emoticon['record_id'] = $insert->id;
$emoticon['created_at'] = date('Y-m-d H:i:s');
if (!TalkRecordsFile::create($emoticon)) {
throw new Exception('插入聊天记录(代码消息)失败...');
}
Db::commit();
} catch (Exception $e) {
Db::rollBack();
return false;
}
return $insert->id;
}
/**
* 创建文件消息
*
* @param array $message
* @param array $emoticon
* @return bool|int
*/
public function createFileMessage(array $message, array $emoticon)
{
Db::beginTransaction();
try {
$message['created_at'] = date('Y-m-d H:i:s');
$insert = TalkRecords::create($message);
if (!$insert) {
throw new Exception('插入聊天记录失败...');
}
$emoticon['record_id'] = $insert->id;
$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('插入聊天记录(代码消息)失败...');
}
Db::commit();
} catch (Exception $e) {
Db::rollBack();
return false;
}
return $insert->id;
}
} }