优化代码

main
gzydong 2021-07-13 22:53:13 +08:00
parent 2fed3e583b
commit 296de89431
7 changed files with 60 additions and 69 deletions

View File

@ -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();
}

View File

@ -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

View File

@ -28,8 +28,6 @@ class TalkRecords extends BaseModel
{
protected $table = 'talk_records';
public $timestamps = true;
protected $fillable = [
'talk_type',
'msg_type',

View File

@ -26,8 +26,6 @@ class TalkRecordsVote extends BaseModel
{
protected $table = 'talk_records_vote';
public $timestamps = true;
protected $fillable = [
'record_id',
'user_id',

View File

@ -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, [

View File

@ -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) {

View File

@ -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;
}
}