优化代码

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>
* [
* 'uuid' => '',
@ -61,7 +61,7 @@ class SubscribeHandleService
public function handle(array $data)
{
if (!isset($data['uuid'], $data['event'], $data['data'], $data['options'])) {
return false;
return;
}
if (isset(self::EVENTS[$data['event']])) {
@ -120,7 +120,7 @@ class SubscribeHandleService
if (!$result) return;
$message = di()->get(FormatMessageService::class)->handleChatRecords([$result->toArray()])[0];
$notify = [
$push = [
'sender_id' => $sender_id,
'receiver_id' => $receiver_id,
'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']);
$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));
$this->socketPushNotify($fds, json_encode([
$this->push($fds, json_encode([
TalkEventConstant::EVENT_ONLINE_STATUS, [
'user_id' => $user_id,
'status' => $status
@ -199,7 +199,7 @@ class SubscribeHandleService
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,
'sender_id' => $record->user_id,
'receiver_id' => $record->receiver_id,
@ -245,16 +245,16 @@ class SubscribeHandleService
'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 消息推送
*
* @param $fds
* @param $message
* @param array $fds
* @param string $message
*/
private function socketPushNotify($fds, $message)
private function push(array $fds, string $message): void
{
$server = server();
foreach ($fds as $fd) {

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace App\Service;

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace App\Service;
@ -43,15 +44,14 @@ class TalkService extends BaseService
'users.avatar as avatar',
];
$rowsSqlObj = TalkRecords::select($fields);
$rowsSqlObj->leftJoin('users', 'users.id', '=', 'talk_records.user_id');
$model = TalkRecords::select($fields);
$model->leftJoin('users', 'users.id', '=', 'talk_records.user_id');
if ($record_id) {
$rowsSqlObj->where('talk_records.id', '<', $record_id);
$model->where('talk_records.id', '<', $record_id);
}
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([
['talk_records.user_id', '=', $user_id],
['talk_records.receiver_id', '=', $receiver_id]
@ -61,24 +61,24 @@ class TalkService extends BaseService
]);
});
} 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) {
$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');
$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->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 (!di()->get(UserFriendService::class)->isFriend($user_id, $receiver_id, true)) {
@ -244,9 +244,9 @@ class TalkService extends BaseService
'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) {
$rowsSqlObj->where(function ($query) use ($user_id, $receiver_id) {
$model->where(function ($query) use ($user_id, $receiver_id) {
$query->where([
['talk_records.user_id', '=', $user_id],
['talk_records.receiver_id', '=', $receiver_id]
@ -256,25 +256,26 @@ class TalkService extends BaseService
]);
});
} 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'])) {
$rowsSqlObj->where('talk_records.content', 'like', "%{$params['keywords']}%");
if (isset($params['keywords']) && !empty($params['keywords'])) {
$model->where('talk_records.content', 'like', "%{$params['keywords']}%");
}
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) {
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);
return $this->getPagingRows($rows, $count, $page, $page_size);

View File

@ -50,7 +50,7 @@ class SendEmailCode
* @param string $email 邮箱地址
* @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);
if (!$sms_code = $this->getCode($key)) {