优化代码

main
gzydong 2021-09-05 21:13:48 +08:00
parent 86b54e48fb
commit de2ecea56c
4 changed files with 51 additions and 49 deletions

View File

@ -48,7 +48,7 @@ class SubscribeHandleService
} }
/** /**
* @param array $data * @param array $data 数据
* <pre> * <pre>
* [ * [
* 'uuid' => '', * 'uuid' => '',
@ -61,7 +61,7 @@ class SubscribeHandleService
public function handle(array $data) public function handle(array $data)
{ {
if (!isset($data['uuid'], $data['event'], $data['data'], $data['options'])) { if (!isset($data['uuid'], $data['event'], $data['data'], $data['options'])) {
return false; return;
} }
if (isset(self::EVENTS[$data['event']])) { if (isset(self::EVENTS[$data['event']])) {
@ -120,7 +120,7 @@ class SubscribeHandleService
if (!$result) return; if (!$result) return;
$message = di()->get(FormatMessageService::class)->handleChatRecords([$result->toArray()])[0]; $message = di()->get(FormatMessageService::class)->handleChatRecords([$result->toArray()])[0];
$notify = [ $push = [
'sender_id' => $sender_id, 'sender_id' => $sender_id,
'receiver_id' => $receiver_id, 'receiver_id' => $receiver_id,
'talk_type' => $talk_type, 'talk_type' => $talk_type,
@ -130,7 +130,7 @@ class SubscribeHandleService
]) ])
]; ];
$this->socketPushNotify($fds, json_encode([TalkEventConstant::EVENT_TALK, $notify])); $this->push($fds, json_encode([TalkEventConstant::EVENT_TALK, $push]));
} }
/** /**
@ -142,7 +142,7 @@ class SubscribeHandleService
{ {
$fds = $this->clientService->findUserFds($data['data']['receiver_id']); $fds = $this->clientService->findUserFds($data['data']['receiver_id']);
$this->socketPushNotify($fds, json_encode([TalkEventConstant::EVENT_KEYBOARD, $data['data']])); $this->push($fds, json_encode([TalkEventConstant::EVENT_KEYBOARD, $data['data']]));
} }
/** /**
@ -166,7 +166,7 @@ class SubscribeHandleService
$fds = array_unique(array_merge(...$fds)); $fds = array_unique(array_merge(...$fds));
$this->socketPushNotify($fds, json_encode([ $this->push($fds, json_encode([
TalkEventConstant::EVENT_ONLINE_STATUS, [ TalkEventConstant::EVENT_ONLINE_STATUS, [
'user_id' => $user_id, 'user_id' => $user_id,
'status' => $status 'status' => $status
@ -199,7 +199,7 @@ class SubscribeHandleService
if (!$fds) return; if (!$fds) return;
$this->socketPushNotify($fds, json_encode([TalkEventConstant::EVENT_REVOKE_TALK, [ $this->push($fds, json_encode([TalkEventConstant::EVENT_REVOKE_TALK, [
'talk_type' => $record->talk_type, 'talk_type' => $record->talk_type,
'sender_id' => $record->user_id, 'sender_id' => $record->user_id,
'receiver_id' => $record->receiver_id, 'receiver_id' => $record->receiver_id,
@ -245,16 +245,16 @@ class SubscribeHandleService
'mobile' => $friendInfo->mobile, 'mobile' => $friendInfo->mobile,
]; ];
$this->socketPushNotify(array_unique($fds), json_encode([TalkEventConstant::EVENT_FRIEND_APPLY, $msg])); $this->push(array_unique($fds), json_encode([TalkEventConstant::EVENT_FRIEND_APPLY, $msg]));
} }
/** /**
* WebSocket 消息推送 * WebSocket 消息推送
* *
* @param $fds * @param array $fds
* @param $message * @param string $message
*/ */
private function socketPushNotify($fds, $message) private function push(array $fds, string $message): void
{ {
$server = server(); $server = server();
foreach ($fds as $fd) { foreach ($fds as $fd) {

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
namespace App\Service; namespace App\Service;
@ -33,8 +34,8 @@ class TalkMessageService
*/ */
public function insertTextMessage(array $message): bool public function insertTextMessage(array $message): bool
{ {
$message['msg_type'] = TalkMessageType::TEXT_MESSAGE; $message['msg_type'] = TalkMessageType::TEXT_MESSAGE;
$message['content'] = htmlspecialchars($message['content']); $message['content'] = htmlspecialchars($message['content']);
$message['created_at'] = date('Y-m-d H:i:s'); $message['created_at'] = date('Y-m-d H:i:s');
$message['updated_at'] = date('Y-m-d H:i:s'); $message['updated_at'] = date('Y-m-d H:i:s');
@ -73,7 +74,7 @@ class TalkMessageService
{ {
Db::beginTransaction(); Db::beginTransaction();
try { try {
$message['msg_type'] = TalkMessageType::CODE_MESSAGE; $message['msg_type'] = TalkMessageType::CODE_MESSAGE;
$message['created_at'] = date('Y-m-d H:i:s'); $message['created_at'] = date('Y-m-d H:i:s');
$message['updated_at'] = date('Y-m-d H:i:s'); $message['updated_at'] = date('Y-m-d H:i:s');
@ -82,7 +83,7 @@ class TalkMessageService
throw new Exception('插入聊天记录失败...'); throw new Exception('插入聊天记录失败...');
} }
$code['record_id'] = $insert->id; $code['record_id'] = $insert->id;
$code['created_at'] = date('Y-m-d H:i:s'); $code['created_at'] = date('Y-m-d H:i:s');
if (!TalkRecordsCode::create($code)) { if (!TalkRecordsCode::create($code)) {
throw new Exception('插入聊天记录(代码消息)失败...'); throw new Exception('插入聊天记录(代码消息)失败...');
@ -120,7 +121,7 @@ class TalkMessageService
{ {
Db::beginTransaction(); Db::beginTransaction();
try { try {
$message['msg_type'] = TalkMessageType::FILE_MESSAGE; $message['msg_type'] = TalkMessageType::FILE_MESSAGE;
$message['created_at'] = date('Y-m-d H:i:s'); $message['created_at'] = date('Y-m-d H:i:s');
$message['updated_at'] = date('Y-m-d H:i:s'); $message['updated_at'] = date('Y-m-d H:i:s');
@ -129,8 +130,8 @@ class TalkMessageService
throw new Exception('插入聊天记录失败...'); throw new Exception('插入聊天记录失败...');
} }
$file['record_id'] = $insert->id; $file['record_id'] = $insert->id;
$file['file_type'] = MediaTypeConstant::getMediaType($file['file_suffix']); $file['file_type'] = MediaTypeConstant::getMediaType($file['file_suffix']);
$file['created_at'] = date('Y-m-d H:i:s'); $file['created_at'] = date('Y-m-d H:i:s');
if (!TalkRecordsFile::create($file)) { if (!TalkRecordsFile::create($file)) {
throw new Exception('插入聊天记录(代码消息)失败...'); throw new Exception('插入聊天记录(代码消息)失败...');
@ -170,21 +171,21 @@ class TalkMessageService
Db::beginTransaction(); Db::beginTransaction();
try { try {
$message['msg_type'] = TalkMessageType::VOTE_MESSAGE; $message['msg_type'] = TalkMessageType::VOTE_MESSAGE;
$message['created_at'] = date('Y-m-d H:i:s'); $message['created_at'] = date('Y-m-d H:i:s');
$message['updated_at'] = date('Y-m-d H:i:s'); $message['updated_at'] = date('Y-m-d H:i:s');
$insert = TalkRecords::create($message); $insert = TalkRecords::create($message);
$options = []; $options = [];
foreach ($vote['answer_option'] as $k => $option) { foreach ($vote['answer_option'] as $k => $option) {
$options[chr(65 + $k)] = $option; $options[chr(65 + $k)] = $option;
} }
$vote['record_id'] = $insert->id; $vote['record_id'] = $insert->id;
$vote['answer_option'] = $options; $vote['answer_option'] = $options;
$vote['answer_num'] = $answer_num; $vote['answer_num'] = $answer_num;
$vote['created_at'] = date('Y-m-d H:i:s'); $vote['created_at'] = date('Y-m-d H:i:s');
$vote['updated_at'] = $vote['created_at']; $vote['updated_at'] = $vote['created_at'];
if (!TalkRecordsVote::create($vote)) { if (!TalkRecordsVote::create($vote)) {
throw new Exception('插入聊天记录(投票消息)失败...'); throw new Exception('插入聊天记录(投票消息)失败...');
@ -298,17 +299,17 @@ class TalkMessageService
Db::beginTransaction(); Db::beginTransaction();
try { try {
$message['receiver_id'] = RobotConstant::LOGIN_ROBOT; $message['receiver_id'] = RobotConstant::LOGIN_ROBOT;
$message['talk_type'] = TalkModeConstant::PRIVATE_CHAT; $message['talk_type'] = TalkModeConstant::PRIVATE_CHAT;
$message['msg_type'] = TalkMessageType::USER_LOGIN_MESSAGE; $message['msg_type'] = TalkMessageType::USER_LOGIN_MESSAGE;
$message['created_at'] = date('Y-m-d H:i:s'); $message['created_at'] = date('Y-m-d H:i:s');
$message['updated_at'] = date('Y-m-d H:i:s'); $message['updated_at'] = date('Y-m-d H:i:s');
$insert = TalkRecords::create($message); $insert = TalkRecords::create($message);
if (!$insert) { if (!$insert) {
throw new Exception('插入聊天记录失败...'); throw new Exception('插入聊天记录失败...');
} }
$loginParams['record_id'] = $insert->id; $loginParams['record_id'] = $insert->id;
$loginParams['created_at'] = date('Y-m-d H:i:s'); $loginParams['created_at'] = date('Y-m-d H:i:s');
if (!TalkRecordsLogin::create($loginParams)) { if (!TalkRecordsLogin::create($loginParams)) {

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
namespace App\Service; namespace App\Service;
@ -43,15 +44,14 @@ class TalkService extends BaseService
'users.avatar as avatar', 'users.avatar as avatar',
]; ];
$rowsSqlObj = TalkRecords::select($fields); $model = TalkRecords::select($fields);
$model->leftJoin('users', 'users.id', '=', 'talk_records.user_id');
$rowsSqlObj->leftJoin('users', 'users.id', '=', 'talk_records.user_id');
if ($record_id) { if ($record_id) {
$rowsSqlObj->where('talk_records.id', '<', $record_id); $model->where('talk_records.id', '<', $record_id);
} }
if ($talk_type == TalkModeConstant::PRIVATE_CHAT) { if ($talk_type == TalkModeConstant::PRIVATE_CHAT) {
$rowsSqlObj->where(function ($query) use ($user_id, $receiver_id) { $model->where(function ($query) use ($user_id, $receiver_id) {
$query->where([ $query->where([
['talk_records.user_id', '=', $user_id], ['talk_records.user_id', '=', $user_id],
['talk_records.receiver_id', '=', $receiver_id] ['talk_records.receiver_id', '=', $receiver_id]
@ -61,24 +61,24 @@ class TalkService extends BaseService
]); ]);
}); });
} else { } else {
$rowsSqlObj->where('talk_records.receiver_id', $receiver_id); $model->where('talk_records.receiver_id', $receiver_id);
} }
$rowsSqlObj->where('talk_records.talk_type', $talk_type); $model->where('talk_records.talk_type', $talk_type);
if ($msg_type) { if ($msg_type) {
$rowsSqlObj->whereIn('talk_records.msg_type', $msg_type); $model->whereIn('talk_records.msg_type', $msg_type);
} }
// 过滤用户删除记录 // 过滤用户删除记录
$rowsSqlObj->whereNotExists(function ($query) use ($user_id) { $model->whereNotExists(function ($query) use ($user_id) {
$prefix = config('databases.default.prefix'); $prefix = config('databases.default.prefix');
$query->select(Db::raw(1))->from('talk_records_delete'); $query->select(Db::raw(1))->from('talk_records_delete');
$query->whereRaw("{$prefix}talk_records_delete.record_id = {$prefix}talk_records.id and {$prefix}talk_records_delete.user_id = {$user_id}"); $query->whereRaw("{$prefix}talk_records_delete.record_id = {$prefix}talk_records.id and {$prefix}talk_records_delete.user_id = {$user_id}");
$query->limit(1); $query->limit(1);
}); });
$rows = $rowsSqlObj->orderBy('talk_records.id', 'desc')->limit($limit)->get()->toArray(); $rows = $model->orderBy('talk_records.id', 'desc')->limit($limit)->get()->toArray();
if ($record_id === 0 && $talk_type == TalkModeConstant::PRIVATE_CHAT && empty($msg_type)) { if ($record_id === 0 && $talk_type == TalkModeConstant::PRIVATE_CHAT && empty($msg_type)) {
if (!di()->get(UserFriendService::class)->isFriend($user_id, $receiver_id, true)) { if (!di()->get(UserFriendService::class)->isFriend($user_id, $receiver_id, true)) {
@ -244,9 +244,9 @@ class TalkService extends BaseService
'users.avatar as avatar', 'users.avatar as avatar',
]; ];
$rowsSqlObj = TalkRecords::select($fields)->leftJoin('users', 'users.id', '=', 'talk_records.user_id'); $model = TalkRecords::select($fields)->leftJoin('users', 'users.id', '=', 'talk_records.user_id');
if ($talk_type == 1) { if ($talk_type == 1) {
$rowsSqlObj->where(function ($query) use ($user_id, $receiver_id) { $model->where(function ($query) use ($user_id, $receiver_id) {
$query->where([ $query->where([
['talk_records.user_id', '=', $user_id], ['talk_records.user_id', '=', $user_id],
['talk_records.receiver_id', '=', $receiver_id] ['talk_records.receiver_id', '=', $receiver_id]
@ -256,25 +256,26 @@ class TalkService extends BaseService
]); ]);
}); });
} else { } else {
$rowsSqlObj->where('talk_records.receiver_id', $receiver_id); $model->where('talk_records.receiver_id', $receiver_id);
} }
$rowsSqlObj->where('talk_records.talk_type', $talk_type); $model->where('talk_records.talk_type', $talk_type);
if (isset($params['keywords'])) { if (isset($params['keywords']) && !empty($params['keywords'])) {
$rowsSqlObj->where('talk_records.content', 'like', "%{$params['keywords']}%"); $model->where('talk_records.content', 'like', "%{$params['keywords']}%");
} }
if (isset($params['date'])) { if (isset($params['date'])) {
$rowsSqlObj->whereDate('talk_records.created_at', $params['date']); $model->whereDate('talk_records.created_at', $params['date']);
} }
$count = $rowsSqlObj->count(); $count = $model->count();
if ($count == 0) { if ($count == 0) {
return $this->getPagingRows([], 0, $page, $page_size); return $this->getPagingRows([], 0, $page, $page_size);
} }
$rows = $rowsSqlObj->orderBy('talk_records.id', 'desc')->forPage($page, $page_size)->get()->toArray(); $rows = $model->orderBy('talk_records.id', 'desc')->forPage($page, $page_size)->get()->toArray();
$rows = di()->get(FormatMessageService::class)->handleChatRecords($rows); $rows = di()->get(FormatMessageService::class)->handleChatRecords($rows);
return $this->getPagingRows($rows, $count, $page, $page_size); return $this->getPagingRows($rows, $count, $page, $page_size);

View File

@ -50,7 +50,7 @@ class SendEmailCode
* @param string $email 邮箱地址 * @param string $email 邮箱地址
* @return bool * @return bool
*/ */
public function send(string $type, string $title, string $email) public function send(string $type, string $title, string $email): bool
{ {
$key = $this->getKey($type, $email); $key = $this->getKey($type, $email);
if (!$sms_code = $this->getCode($key)) { if (!$sms_code = $this->getCode($key)) {