2021-07-05 21:52:44 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Service;
|
|
|
|
|
2021-07-13 22:53:13 +08:00
|
|
|
use App\Cache\LastMessage;
|
|
|
|
use App\Constants\TalkMessageEvent;
|
2021-07-08 23:44:43 +08:00
|
|
|
use App\Constants\TalkMessageType;
|
2021-07-09 22:57:19 +08:00
|
|
|
use App\Model\Talk\TalkRecordsCode;
|
2021-07-12 23:19:58 +08:00
|
|
|
use App\Model\Talk\TalkRecordsVote;
|
2021-07-13 22:53:13 +08:00
|
|
|
use App\Support\MessageProducer;
|
2021-07-08 23:44:43 +08:00
|
|
|
use Exception;
|
|
|
|
use App\Constants\MediaFileType;
|
2021-07-09 22:57:19 +08:00
|
|
|
use App\Model\Talk\TalkRecords;
|
|
|
|
use App\Model\Talk\TalkRecordsFile;
|
2021-07-08 23:44:43 +08:00
|
|
|
use Hyperf\DbConnection\Db;
|
2021-07-05 21:52:44 +08:00
|
|
|
|
|
|
|
class TalkMessageService
|
|
|
|
{
|
2021-07-08 23:44:43 +08:00
|
|
|
/**
|
|
|
|
* 创建代码块消息
|
|
|
|
*
|
|
|
|
* @param array $message
|
|
|
|
* @param array $code
|
2021-07-13 22:53:13 +08:00
|
|
|
* @return bool
|
2021-07-08 23:44:43 +08:00
|
|
|
*/
|
|
|
|
public function insertCodeMessage(array $message, array $code)
|
|
|
|
{
|
|
|
|
Db::beginTransaction();
|
|
|
|
try {
|
|
|
|
$message['msg_type'] = TalkMessageType::CODE_MESSAGE;
|
|
|
|
$message['created_at'] = date('Y-m-d H:i:s');
|
|
|
|
$message['updated_at'] = date('Y-m-d H:i:s');
|
2021-07-05 21:52:44 +08:00
|
|
|
|
2021-07-08 23:44:43 +08:00
|
|
|
$insert = TalkRecords::create($message);
|
|
|
|
if (!$insert) {
|
|
|
|
throw new Exception('插入聊天记录失败...');
|
|
|
|
}
|
|
|
|
|
|
|
|
$code['record_id'] = $insert->id;
|
|
|
|
$code['created_at'] = date('Y-m-d H:i:s');
|
|
|
|
if (!TalkRecordsCode::create($code)) {
|
|
|
|
throw new Exception('插入聊天记录(代码消息)失败...');
|
|
|
|
}
|
|
|
|
|
|
|
|
Db::commit();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Db::rollBack();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:53:13 +08:00
|
|
|
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;
|
2021-07-08 23:44:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 创建文件类消息
|
|
|
|
*
|
|
|
|
* @param array $message
|
|
|
|
* @param array $file
|
2021-07-13 22:53:13 +08:00
|
|
|
* @return bool
|
2021-07-08 23:44:43 +08:00
|
|
|
*/
|
|
|
|
public function insertFileMessage(array $message, array $file)
|
|
|
|
{
|
|
|
|
Db::beginTransaction();
|
|
|
|
try {
|
|
|
|
$message['msg_type'] = TalkMessageType::FILE_MESSAGE;
|
|
|
|
$message['created_at'] = date('Y-m-d H:i:s');
|
|
|
|
$message['updated_at'] = date('Y-m-d H:i:s');
|
|
|
|
|
|
|
|
$insert = TalkRecords::create($message);
|
|
|
|
if (!$insert) {
|
|
|
|
throw new Exception('插入聊天记录失败...');
|
|
|
|
}
|
|
|
|
|
|
|
|
$file['record_id'] = $insert->id;
|
|
|
|
$file['file_type'] = MediaFileType::getMediaType($file['file_suffix']);
|
|
|
|
$file['created_at'] = date('Y-m-d H:i:s');
|
|
|
|
if (!TalkRecordsFile::create($file)) {
|
|
|
|
throw new Exception('插入聊天记录(代码消息)失败...');
|
|
|
|
}
|
|
|
|
|
|
|
|
Db::commit();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Db::rollBack();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:53:13 +08:00
|
|
|
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;
|
2021-07-08 23:44:43 +08:00
|
|
|
}
|
2021-07-12 23:19:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $message
|
|
|
|
* @param array $vote
|
|
|
|
*/
|
|
|
|
public function insertVoteMessage(array $message, array $vote)
|
|
|
|
{
|
|
|
|
Db::beginTransaction();
|
|
|
|
try {
|
|
|
|
$message['msg_type'] = TalkMessageType::FILE_MESSAGE;
|
|
|
|
$message['created_at'] = date('Y-m-d H:i:s');
|
|
|
|
$message['updated_at'] = date('Y-m-d H:i:s');
|
|
|
|
|
|
|
|
$insert = TalkRecords::create($message);
|
|
|
|
if (!$insert) {
|
|
|
|
throw new Exception('插入聊天记录失败...');
|
|
|
|
}
|
|
|
|
|
|
|
|
$vote['record_id'] = $insert->id;
|
|
|
|
if (!TalkRecordsVote::create($vote)) {
|
|
|
|
throw new Exception('插入聊天记录(投票消息)失败...');
|
|
|
|
}
|
|
|
|
|
|
|
|
Db::commit();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Db::rollBack();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:53:13 +08:00
|
|
|
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;
|
2021-07-12 23:19:58 +08:00
|
|
|
}
|
2021-07-05 21:52:44 +08:00
|
|
|
}
|