优化代码

main
gzydong 2021-07-21 23:31:41 +08:00
parent d2e58be344
commit 3d4d715471
8 changed files with 49 additions and 39 deletions

View File

@ -9,7 +9,7 @@ use App\Cache\Repository\HashRedis;
*
* @package App\Cache
*/
class UnreadTalk extends HashRedis
class UnreadTalkCache extends HashRedis
{
public $name = 'unread-talk';

View File

@ -12,7 +12,7 @@ namespace App\Controller\Api\V1;
use App\Cache\LastMessage;
use App\Cache\Repository\LockRedis;
use App\Cache\UnreadTalk;
use App\Cache\UnreadTalkCache;
use App\Constants\TalkMessageType;
use App\Constants\TalkModeConstant;
use App\Model\Talk\TalkList;
@ -61,7 +61,7 @@ class TalkController extends CController
$user_id = $this->uid();
// 读取用户的未读消息列表
if ($list = UnreadTalk::getInstance()->reads($user_id)) {
if ($list = UnreadTalkCache::getInstance()->reads($user_id)) {
foreach ($list as $friend_id => $num) {
$this->talkListService->create($user_id, $friend_id, TalkModeConstant::PRIVATE_CHAT);
}
@ -85,8 +85,11 @@ class TalkController extends CController
]);
$user_id = $this->uid();
$lock = 'talk:list:' . $user_id . '-' . $params['receiver_id'] . '-' . $params['talk_type'];
if (!LockRedis::getInstance()->lock($lock, 10)) {
$string = join('-', [$user_id, $params['receiver_id'], $params['talk_type'], md5($request->getHeaderLine('user-agent'))]);
$lock = 'talk:list:' . $string;
// 防止前端并发请求
if (!LockRedis::getInstance()->lock($lock, 60)) {
return $this->response->fail('创建失败!');
}
@ -111,7 +114,7 @@ class TalkController extends CController
$userInfo = User::where('id', $data['receiver_id'])->first(['nickname', 'avatar']);
$data['avatar'] = $userInfo->avatar;
$data['name'] = $userInfo->nickname;
$data['unread_num'] = UnreadTalk::getInstance()->read($data['receiver_id'], $user_id);
$data['unread_num'] = UnreadTalkCache::getInstance()->read($data['receiver_id'], $user_id);
$data['remark_name'] = $service->getFriendRemark($user_id, (int)$data['receiver_id']);
} else if ($result['talk_type'] == TalkModeConstant::GROUP_CHAT) {
$groupInfo = Group::where('id', $data['receiver_id'])->first(['group_name', 'avatar']);
@ -201,7 +204,7 @@ class TalkController extends CController
// 设置好友消息未读数
if ($params['talk_type'] == TalkModeConstant::PRIVATE_CHAT) {
UnreadTalk::getInstance()->reset((int)$params['receiver_id'], $this->uid());
UnreadTalkCache::getInstance()->reset((int)$params['receiver_id'], $this->uid());
}
return $this->response->success();

View File

@ -2,7 +2,7 @@
namespace App\Controller\Api\V1;
use App\Cache\UnreadTalk;
use App\Cache\UnreadTalkCache;
use App\Constants\TalkEventConstant;
use App\Constants\TalkModeConstant;
use App\Event\TalkEvent;
@ -312,7 +312,7 @@ class TalkMessageController extends CController
if ($receive_user_ids) {
foreach ($receive_user_ids as $v) {
UnreadTalk::getInstance()->increment($user_id, $v['id']);
UnreadTalkCache::getInstance()->increment($user_id, $v['id']);
}
}

View File

@ -129,12 +129,12 @@ class UsersController extends CController
}
/**
* 通过手机号查找用户
* 通过用户ID查找用户
* @RequestMapping(path="search-user", methods="post")
*
* @return ResponseInterface
*/
public function searchUserInfo()
public function search()
{
$params = $this->request->inputs(['user_id']);
$this->validate($params, ['user_id' => 'required|integer']);

View File

@ -24,31 +24,33 @@ class FormatMessageService
private function formatTalkMessage(array $data): array
{
$message = [
"id" => 0, // 消息记录ID
"talk_type" => 1, // 消息来源[1:好友私信;2:群聊]
"msg_type" => 1, // 消息类型
"user_id" => 0, // 发送者用户ID
"receiver_id" => 0, // 接收者ID[好友ID或群ID]
"id" => 0, // 消息记录ID
"talk_type" => 1, // 消息来源[1:好友私信;2:群聊]
"msg_type" => 1, // 消息类型
"user_id" => 0, // 发送者用户ID
"receiver_id" => 0, // 接收者ID[好友ID或群ID]
// 发送消息人的信息
"nickname" => "",// 用户昵称
"avatar" => "",// 用户头像
"group_name" => "",// 群组名称
"group_avatar" => "",// 群组头像
"nickname" => '', // 用户昵称
"avatar" => '', // 用户头像
"group_name" => '', // 群组名称
"group_avatar" => '', // 群组头像
// 不同的消息类型
"file" => [],
"code_block" => [],
"forward" => [],
"invite" => [],
"vote" => [],
"file" => [], // 文件消息
"code_block" => [], // 代码消息
"forward" => [], // 转发消息
"invite" => [], // 邀请消息
"vote" => [], // 投票消息
// 消息创建时间
"content" => '',// 文本消息
"created_at" => "",
"content" => '', // 文本消息
"created_at" => '', // 发送时间
// 消息属性
"is_revoke" => 0, // 消息是否撤销
"is_revoke" => 0, // 消息是否撤销
"is_mark" => 0, // 消息是否重要
"is_read" => 0, // 消息是否已读
];
return array_merge($message, array_intersect_key($data, $message));
@ -62,7 +64,7 @@ class FormatMessageService
*/
public function handleChatRecords(array $rows)
{
if (empty($rows)) return [];
if (!$rows) return [];
$files = $codes = $forwards = $invites = $votes = [];
foreach ($rows as $value) {
@ -106,6 +108,7 @@ class FormatMessageService
$forwards = TalkRecordsForward::whereIn('record_id', $forwards)->get(['record_id', 'records_id', 'text'])->keyBy('record_id')->toArray();
}
// 查询投票消息
if ($votes) {
$votes = TalkRecordsVote::whereIn('record_id', $votes)->get([
'id', 'record_id', 'title', 'answer_mode', 'status', 'answer_option', 'answer_num', 'answered_num'
@ -120,14 +123,16 @@ class FormatMessageService
$rows[$k]['vote'] = [];
switch ($row['msg_type']) {
case TalkMessageType::FILE_MESSAGE:// 文件消息
// 文件消息
case TalkMessageType::FILE_MESSAGE:
$rows[$k]['file'] = $files[$row['id']] ?? [];
if ($rows[$k]['file']) {
$rows[$k]['file']['file_url'] = get_media_url($rows[$k]['file']['save_dir']);
}
break;
case TalkMessageType::FORWARD_MESSAGE:// 会话记录消息
// 会话记录消息
case TalkMessageType::FORWARD_MESSAGE:
if (isset($forwards[$row['id']])) {
$rows[$k]['forward'] = [
'num' => substr_count($forwards[$row['id']]['records_id'], ',') + 1,
@ -136,7 +141,8 @@ class FormatMessageService
}
break;
case TalkMessageType::CODE_MESSAGE:// 代码块消息
// 代码块消息
case TalkMessageType::CODE_MESSAGE:
$rows[$k]['code_block'] = $codes[$row['id']] ?? [];
if ($rows[$k]['code_block']) {
$rows[$k]['code_block']['code'] = htmlspecialchars_decode($rows[$k]['code_block']['code']);
@ -144,7 +150,8 @@ class FormatMessageService
}
break;
case TalkMessageType::VOTE_MESSAGE:// 投票消息
// 投票消息
case TalkMessageType::VOTE_MESSAGE:
$options = [];
foreach ($votes[$row['id']]['answer_option'] as $k2 => $value) {
$options[] = [
@ -161,7 +168,8 @@ class FormatMessageService
];
break;
case TalkMessageType::GROUP_INVITE_MESSAGE:// 入群消息/退群消息
// 入群消息/退群消息
case TalkMessageType::GROUP_INVITE_MESSAGE:
if (isset($invites[$row['id']])) {
$rows[$k]['invite'] = [
'type' => $invites[$row['id']]['type'],

View File

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace App\Service\Message;
use App\Cache\LastMessage;
use App\Cache\UnreadTalk;
use App\Cache\UnreadTalkCache;
use App\Constants\TalkEventConstant;
use App\Constants\TalkMessageType;
use App\Constants\TalkModeConstant;
@ -76,7 +76,7 @@ class ReceiveHandleService
// 判断是否私信
if ($result->talk_type == TalkModeConstant::PRIVATE_CHAT) {
UnreadTalk::getInstance()->increment($result->user_id, $result->receiver_id);
UnreadTalkCache::getInstance()->increment($result->user_id, $result->receiver_id);
}
// 缓存最后一条聊天消息

View File

@ -100,7 +100,6 @@ class SubscribeHandleService
// 客户端ID去重
if (!$fds = array_unique($fds)) return;
$result = TalkRecords::leftJoin('users', 'users.id', '=', 'talk_records.user_id')
->where('talk_records.id', $record_id)
->first([

View File

@ -4,7 +4,7 @@ namespace App\Service;
use App\Cache\LastMessage;
use App\Cache\ServerRunID;
use App\Cache\UnreadTalk;
use App\Cache\UnreadTalkCache;
use App\Constants\TalkModeConstant;
use App\Model\Talk\TalkList;
use Carbon\Carbon;
@ -140,7 +140,7 @@ class TalkListService
if ($item['talk_type'] == TalkModeConstant::PRIVATE_CHAT) {
$data['name'] = $item['nickname'];
$data['avatar'] = $item['user_avatar'];
$data['unread_num'] = UnreadTalk::getInstance()->read($item['receiver_id'], $user_id);
$data['unread_num'] = UnreadTalkCache::getInstance()->read($item['receiver_id'], $user_id);
$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']);
} else if (TalkModeConstant::GROUP_CHAT) {