From 296de8943105b0a630af969332e5598f248c488a Mon Sep 17 00:00:00 2001 From: gzydong <837215079@qq.com> Date: Tue, 13 Jul 2021 22:53:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Api/V1/TalkMessageController.php | 69 ++++--------------- app/Model/Talk/TalkList.php | 2 +- app/Model/Talk/TalkRecords.php | 2 - app/Model/Talk/TalkRecordsVote.php | 2 - app/Service/Message/ReceiveHandleService.php | 2 +- app/Service/TalkListService.php | 3 +- app/Service/TalkMessageService.php | 49 +++++++++++-- 7 files changed, 60 insertions(+), 69 deletions(-) diff --git a/app/Controller/Api/V1/TalkMessageController.php b/app/Controller/Api/V1/TalkMessageController.php index 51373ee..983da2a 100644 --- a/app/Controller/Api/V1/TalkMessageController.php +++ b/app/Controller/Api/V1/TalkMessageController.php @@ -2,7 +2,6 @@ namespace App\Controller\Api\V1; -use App\Cache\LastMessage; use App\Cache\UnreadTalk; use App\Constants\TalkMessageEvent; use App\Constants\TalkMode; @@ -60,7 +59,7 @@ class TalkMessageController extends CController return $this->response->fail('暂不属于好友关系或群聊成员,无法发送聊天消息!'); } - $record_id = $this->talkMessageService->insertCodeMessage([ + $isTrue = $this->talkMessageService->insertCodeMessage([ 'talk_type' => $params['talk_type'], 'user_id' => $user_id, 'receiver_id' => $params['receiver_id'], @@ -70,19 +69,7 @@ class TalkMessageController extends CController 'code' => $params['code'] ]); - if (!$record_id) return $this->response->fail('消息发送失败!'); - - MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ - 'sender_id' => $user_id, - 'receiver_id' => (int)$params['receiver_id'], - 'talk_type' => (int)$params['talk_type'], - 'record_id' => $record_id - ])); - - LastMessage::getInstance()->save((int)$params['talk_type'], $user_id, (int)$params['receiver_id'], [ - 'text' => '[代码消息]', - 'created_at' => date('Y-m-d H:i:s') - ]); + if (!$isTrue) return $this->response->fail('消息发送失败!'); return $this->response->success(); } @@ -122,7 +109,7 @@ class TalkMessageController extends CController } // 创建图片消息记录 - $record_id = $this->talkMessageService->insertFileMessage([ + $isTrue = $this->talkMessageService->insertFileMessage([ 'talk_type' => $params['talk_type'], 'user_id' => $user_id, 'receiver_id' => $params['receiver_id'], @@ -134,19 +121,7 @@ class TalkMessageController extends CController 'original_name' => $file->getClientFilename(), ]); - if (!$record_id) return $this->response->fail('图片上传失败!'); - - MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ - 'sender_id' => $user_id, - 'receiver_id' => (int)$params['receiver_id'], - 'talk_type' => (int)$params['talk_type'], - 'record_id' => $record_id, - ])); - - LastMessage::getInstance()->save((int)$params['talk_type'], $user_id, (int)$params['receiver_id'], [ - 'text' => '[图片消息]', - 'created_at' => date('Y-m-d H:i:s') - ]); + if (!$isTrue) return $this->response->fail('图片上传失败!'); return $this->response->success(); } @@ -181,7 +156,7 @@ class TalkMessageController extends CController return $this->response->fail('文件不存在...'); } - $record_id = $this->talkMessageService->insertFileMessage([ + $isTrue = $this->talkMessageService->insertFileMessage([ 'talk_type' => $params['talk_type'], 'user_id' => $user_id, 'receiver_id' => $params['receiver_id'] @@ -194,19 +169,7 @@ class TalkMessageController extends CController 'save_dir' => $save_dir, ]); - if (!$record_id) return $this->response->fail('表情发送失败!'); - - MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ - 'sender_id' => $user_id, - 'receiver_id' => (int)$params['receiver_id'], - 'talk_type' => (int)$params['talk_type'], - 'record_id' => $record_id - ])); - - LastMessage::getInstance()->save((int)$params['talk_type'], $user_id, (int)$params['receiver_id'], [ - 'text' => '[文件消息]', - 'created_at' => date('Y-m-d H:i:s') - ]); + if (!$isTrue) return $this->response->fail('表情发送失败!'); return $this->response->success(); } @@ -236,6 +199,10 @@ class TalkMessageController extends CController 'title' => $params['title'], 'options' => $params['options'], ]); + + if (!$isTrue) return $this->response->fail('发起投票失败!'); + + return $this->response->success(); } /** @@ -260,7 +227,7 @@ class TalkMessageController extends CController if (!$emoticon) return $this->response->fail('表情不存在!'); - $record_id = $this->talkMessageService->insertFileMessage([ + $isTrue = $this->talkMessageService->insertFileMessage([ 'talk_type' => $params['talk_type'], 'user_id' => $user_id, 'receiver_id' => $params['receiver_id'], @@ -272,19 +239,7 @@ class TalkMessageController extends CController 'original_name' => '图片表情', ]); - if (!$record_id) return $this->response->fail('表情发送失败!'); - - MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ - 'sender_id' => $user_id, - 'receiver_id' => (int)$params['receiver_id'], - 'talk_type' => (int)$params['talk_type'], - 'record_id' => $record_id - ])); - - LastMessage::getInstance()->save((int)$params['talk_type'], $user_id, (int)$params['receiver_id'], [ - 'text' => '[表情包消息]', - 'created_at' => date('Y-m-d H:i:s') - ]); + if (!$isTrue) return $this->response->fail('表情发送失败!'); return $this->response->success(); } diff --git a/app/Model/Talk/TalkList.php b/app/Model/Talk/TalkList.php index 50e2426..73f7f59 100644 --- a/app/Model/Talk/TalkList.php +++ b/app/Model/Talk/TalkList.php @@ -18,7 +18,7 @@ use App\Model\BaseModel; * @property integer $is_disturb 消息免打扰 * @property integer $is_robot 是否机器人 * @property string $created_at 创建时间 - * @property string $updated_at 更新时间 + * @property \Carbon\Carbon $updated_at 更新时间 * @package App\Model */ class TalkList extends BaseModel diff --git a/app/Model/Talk/TalkRecords.php b/app/Model/Talk/TalkRecords.php index 758231b..befcce7 100644 --- a/app/Model/Talk/TalkRecords.php +++ b/app/Model/Talk/TalkRecords.php @@ -28,8 +28,6 @@ class TalkRecords extends BaseModel { protected $table = 'talk_records'; - public $timestamps = true; - protected $fillable = [ 'talk_type', 'msg_type', diff --git a/app/Model/Talk/TalkRecordsVote.php b/app/Model/Talk/TalkRecordsVote.php index b367d1c..6296bdf 100644 --- a/app/Model/Talk/TalkRecordsVote.php +++ b/app/Model/Talk/TalkRecordsVote.php @@ -26,8 +26,6 @@ class TalkRecordsVote extends BaseModel { protected $table = 'talk_records_vote'; - public $timestamps = true; - protected $fillable = [ 'record_id', 'user_id', diff --git a/app/Service/Message/ReceiveHandleService.php b/app/Service/Message/ReceiveHandleService.php index 9fd78b8..141ca5b 100644 --- a/app/Service/Message/ReceiveHandleService.php +++ b/app/Service/Message/ReceiveHandleService.php @@ -84,7 +84,7 @@ class ReceiveHandleService // 缓存最后一条聊天消息 LastMessage::getInstance()->save($result->talk_type, $result->user_id, $result->receiver_id, [ 'text' => mb_substr($result->content, 0, 30), - 'created_at' => $result->created_at + 'created_at' => date('Y-m-d H:i:s') ]); MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ diff --git a/app/Service/TalkListService.php b/app/Service/TalkListService.php index d761224..5cda408 100644 --- a/app/Service/TalkListService.php +++ b/app/Service/TalkListService.php @@ -7,6 +7,7 @@ use App\Cache\ServerRunID; use App\Cache\UnreadTalk; use App\Constants\TalkMode; use App\Model\Talk\TalkList; +use Carbon\Carbon; class TalkListService { @@ -133,7 +134,7 @@ class TalkListService 'receiver_id' => $item['receiver_id'], 'is_top' => $item['is_top'], 'is_disturb' => $item['is_disturb'], - 'updated_at' => $item['updated_at'] ?: '2020-01-01 00:00:00', + 'updated_at' => Carbon::parse($item['updated_at'])->toDateTimeString(), ]); if ($item['talk_type'] == TalkMode::PRIVATE_CHAT) { diff --git a/app/Service/TalkMessageService.php b/app/Service/TalkMessageService.php index d58cfc8..07d6ceb 100644 --- a/app/Service/TalkMessageService.php +++ b/app/Service/TalkMessageService.php @@ -2,9 +2,12 @@ namespace App\Service; +use App\Cache\LastMessage; +use App\Constants\TalkMessageEvent; use App\Constants\TalkMessageType; use App\Model\Talk\TalkRecordsCode; use App\Model\Talk\TalkRecordsVote; +use App\Support\MessageProducer; use Exception; use App\Constants\MediaFileType; use App\Model\Talk\TalkRecords; @@ -18,7 +21,7 @@ class TalkMessageService * * @param array $message * @param array $code - * @return bool|int + * @return bool */ public function insertCodeMessage(array $message, array $code) { @@ -45,7 +48,19 @@ class TalkMessageService return false; } - return $insert->id; + MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ + 'sender_id' => $insert->user_id, + 'receiver_id' => $insert->receiver_id, + 'talk_type' => $insert->talk_type, + 'record_id' => $insert->id + ])); + + LastMessage::getInstance()->save($insert->talk_type, $insert->user_id, $insert->receiver_id, [ + 'text' => '[代码消息]', + 'created_at' => date('Y-m-d H:i:s') + ]); + + return true; } /** @@ -53,7 +68,7 @@ class TalkMessageService * * @param array $message * @param array $file - * @return bool|int + * @return bool */ public function insertFileMessage(array $message, array $file) { @@ -81,7 +96,19 @@ class TalkMessageService return false; } - return $insert->id; + MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ + 'sender_id' => $insert->user_id, + 'receiver_id' => $insert->receiver_id, + 'talk_type' => $insert->talk_type, + 'record_id' => $insert->id + ])); + + LastMessage::getInstance()->save($insert->talk_type, $insert->user_id, $insert->receiver_id, [ + 'text' => '[图片消息]', + 'created_at' => date('Y-m-d H:i:s') + ]); + + return true; } /** @@ -112,6 +139,18 @@ class TalkMessageService return false; } - return $insert->id; + MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_TALK, [ + 'sender_id' => $insert->user_id, + 'receiver_id' => $insert->receiver_id, + 'talk_type' => $insert->talk_type, + 'record_id' => $insert->id + ])); + + LastMessage::getInstance()->save($insert->talk_type, $insert->user_id, $insert->receiver_id, [ + 'text' => '[投票消息]', + 'created_at' => date('Y-m-d H:i:s') + ]); + + return true; } }