优化代码

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 * @package App\Cache
*/ */
class UnreadTalk extends HashRedis class UnreadTalkCache extends HashRedis
{ {
public $name = 'unread-talk'; public $name = 'unread-talk';

View File

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

View File

@ -2,7 +2,7 @@
namespace App\Controller\Api\V1; namespace App\Controller\Api\V1;
use App\Cache\UnreadTalk; use App\Cache\UnreadTalkCache;
use App\Constants\TalkEventConstant; use App\Constants\TalkEventConstant;
use App\Constants\TalkModeConstant; use App\Constants\TalkModeConstant;
use App\Event\TalkEvent; use App\Event\TalkEvent;
@ -312,7 +312,7 @@ class TalkMessageController extends CController
if ($receive_user_ids) { if ($receive_user_ids) {
foreach ($receive_user_ids as $v) { 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") * @RequestMapping(path="search-user", methods="post")
* *
* @return ResponseInterface * @return ResponseInterface
*/ */
public function searchUserInfo() public function search()
{ {
$params = $this->request->inputs(['user_id']); $params = $this->request->inputs(['user_id']);
$this->validate($params, ['user_id' => 'required|integer']); $this->validate($params, ['user_id' => 'required|integer']);

View File

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

View File

@ -4,7 +4,7 @@ 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\UnreadTalkCache;
use App\Constants\TalkEventConstant; use App\Constants\TalkEventConstant;
use App\Constants\TalkMessageType; use App\Constants\TalkMessageType;
use App\Constants\TalkModeConstant; use App\Constants\TalkModeConstant;
@ -76,7 +76,7 @@ class ReceiveHandleService
// 判断是否私信 // 判断是否私信
if ($result->talk_type == TalkModeConstant::PRIVATE_CHAT) { 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去重 // 客户端ID去重
if (!$fds = array_unique($fds)) return; if (!$fds = array_unique($fds)) return;
$result = TalkRecords::leftJoin('users', 'users.id', '=', 'talk_records.user_id') $result = TalkRecords::leftJoin('users', 'users.id', '=', 'talk_records.user_id')
->where('talk_records.id', $record_id) ->where('talk_records.id', $record_id)
->first([ ->first([

View File

@ -4,7 +4,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\UnreadTalkCache;
use App\Constants\TalkModeConstant; use App\Constants\TalkModeConstant;
use App\Model\Talk\TalkList; use App\Model\Talk\TalkList;
use Carbon\Carbon; use Carbon\Carbon;
@ -140,7 +140,7 @@ class TalkListService
if ($item['talk_type'] == TalkModeConstant::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'] = UnreadTalkCache::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 (TalkModeConstant::GROUP_CHAT) { } else if (TalkModeConstant::GROUP_CHAT) {