2020-11-04 11:57:16 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
2020-11-04 16:47:17 +08:00
|
|
|
|
namespace App\Service;
|
2020-11-04 11:57:16 +08:00
|
|
|
|
|
2021-05-20 22:23:48 +08:00
|
|
|
|
use Exception;
|
|
|
|
|
use App\Model\User;
|
|
|
|
|
use App\Model\UsersChatList;
|
|
|
|
|
use App\Model\UsersFriend;
|
|
|
|
|
use App\Model\Group\Group;
|
2020-11-09 22:59:25 +08:00
|
|
|
|
use App\Model\Chat\ChatRecord;
|
|
|
|
|
use App\Model\Chat\ChatRecordsCode;
|
|
|
|
|
use App\Model\Chat\ChatRecordsFile;
|
|
|
|
|
use App\Model\Chat\ChatRecordsForward;
|
|
|
|
|
use App\Model\Chat\ChatRecordsInvite;
|
|
|
|
|
use App\Traits\PagingTrait;
|
|
|
|
|
use Hyperf\DbConnection\Db;
|
2021-05-20 22:23:48 +08:00
|
|
|
|
use App\Cache\FriendRemark;
|
|
|
|
|
use App\Cache\LastMessage;
|
|
|
|
|
use App\Cache\UnreadTalk;
|
2020-11-09 22:59:25 +08:00
|
|
|
|
|
2020-11-04 11:57:16 +08:00
|
|
|
|
class TalkService extends BaseService
|
|
|
|
|
{
|
2020-11-09 22:59:25 +08:00
|
|
|
|
use PagingTrait;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取用户的聊天列表
|
|
|
|
|
*
|
|
|
|
|
* @param int $user_id 用户ID
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function talks(int $user_id)
|
|
|
|
|
{
|
|
|
|
|
$filed = [
|
|
|
|
|
'list.id', 'list.type', 'list.friend_id', 'list.group_id', 'list.updated_at', 'list.not_disturb', 'list.is_top',
|
|
|
|
|
'users.avatar as user_avatar', 'users.nickname',
|
|
|
|
|
'group.group_name', 'group.avatar as group_avatar'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$rows = UsersChatList::from('users_chat_list as list')
|
|
|
|
|
->leftJoin('users', 'users.id', '=', 'list.friend_id')
|
2021-03-25 17:32:36 +08:00
|
|
|
|
->leftJoin('group', 'group.id', '=', 'list.group_id')
|
2020-11-09 22:59:25 +08:00
|
|
|
|
->where('list.uid', $user_id)
|
|
|
|
|
->where('list.status', 1)
|
|
|
|
|
->orderBy('updated_at', 'desc')
|
|
|
|
|
->get($filed)
|
|
|
|
|
->toArray();
|
|
|
|
|
|
2021-03-25 17:32:36 +08:00
|
|
|
|
if (!$rows) return [];
|
2020-11-22 23:10:00 +08:00
|
|
|
|
|
2020-11-29 14:44:11 +08:00
|
|
|
|
$socketFDService = make(SocketClientService::class);
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$runIdAll = $socketFDService->getServerRunIdAll();
|
2020-11-22 23:10:00 +08:00
|
|
|
|
|
2021-04-22 16:14:34 +08:00
|
|
|
|
return array_map(function ($item) use ($user_id, $socketFDService, $runIdAll) {
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$data['id'] = $item['id'];
|
|
|
|
|
$data['type'] = $item['type'];
|
|
|
|
|
$data['friend_id'] = $item['friend_id'];
|
|
|
|
|
$data['group_id'] = $item['group_id'];
|
|
|
|
|
$data['name'] = '';//对方昵称/群名称
|
2021-04-20 16:30:57 +08:00
|
|
|
|
$data['unread_num'] = 0; //未读消息数量
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$data['avatar'] = '';//默认头像
|
2020-11-09 22:59:25 +08:00
|
|
|
|
$data['remark_name'] = '';//好友备注
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$data['msg_text'] = '......';
|
|
|
|
|
$data['updated_at'] = $item['updated_at'];
|
|
|
|
|
$data['online'] = 0;
|
2020-11-09 22:59:25 +08:00
|
|
|
|
$data['not_disturb'] = $item['not_disturb'];
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$data['is_top'] = $item['is_top'];
|
2020-11-09 22:59:25 +08:00
|
|
|
|
|
|
|
|
|
if ($item['type'] == 1) {
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$data['name'] = $item['nickname'];
|
|
|
|
|
$data['avatar'] = $item['user_avatar'];
|
2021-05-20 22:23:48 +08:00
|
|
|
|
$data['unread_num'] = UnreadTalk::getInstance()->read((int)$item['friend_id'], $user_id);
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$data['online'] = $socketFDService->isOnlineAll($item['friend_id'], $runIdAll);
|
2020-11-09 22:59:25 +08:00
|
|
|
|
|
2021-05-20 22:23:48 +08:00
|
|
|
|
$remark = FriendRemark::getInstance()->read($user_id, (int)$item['friend_id']);
|
2020-11-09 22:59:25 +08:00
|
|
|
|
if ($remark) {
|
|
|
|
|
$data['remark_name'] = $remark;
|
|
|
|
|
} else {
|
|
|
|
|
$info = UsersFriend::select('user1', 'user2', 'user1_remark', 'user2_remark')
|
|
|
|
|
->where('user1', ($user_id < $item['friend_id']) ? $user_id : $item['friend_id'])
|
|
|
|
|
->where('user2', ($user_id < $item['friend_id']) ? $item['friend_id'] : $user_id)->first();
|
|
|
|
|
if ($info) {
|
|
|
|
|
$data['remark_name'] = ($info->user1 == $item['friend_id']) ? $info->user2_remark : $info->user1_remark;
|
|
|
|
|
|
2021-05-20 22:23:48 +08:00
|
|
|
|
FriendRemark::getInstance()->save($user_id, (int)$item['friend_id'], $data['remark_name']);
|
2020-11-09 22:59:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$data['name'] = strval($item['group_name']);
|
2020-11-09 22:59:25 +08:00
|
|
|
|
$data['avatar'] = $item['group_avatar'];
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-20 22:23:48 +08:00
|
|
|
|
$records = LastMessage::getInstance()->read((int)$item['type'], $user_id, $item['type'] == 1 ? (int)$item['friend_id'] : (int)$item['group_id']);
|
2020-11-09 22:59:25 +08:00
|
|
|
|
if ($records) {
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$data['msg_text'] = $records['text'];
|
2020-11-09 22:59:25 +08:00
|
|
|
|
$data['updated_at'] = $records['created_at'];
|
2021-05-20 22:56:55 +08:00
|
|
|
|
} else {
|
|
|
|
|
$data['updated_at'] = '2020-01-01 00:00:00';
|
2020-11-09 22:59:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}, $rows);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 同步未读的消息到数据库中
|
|
|
|
|
*
|
|
|
|
|
* @param int $user_id 用户ID
|
2021-04-20 16:30:57 +08:00
|
|
|
|
* @param $data
|
2020-11-09 22:59:25 +08:00
|
|
|
|
*/
|
|
|
|
|
public function updateUnreadTalkList(int $user_id, $data)
|
|
|
|
|
{
|
|
|
|
|
foreach ($data as $friend_id => $num) {
|
|
|
|
|
UsersChatList::updateOrCreate([
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'uid' => $user_id,
|
2020-11-09 22:59:25 +08:00
|
|
|
|
'friend_id' => intval($friend_id),
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'type' => 1
|
2020-11-09 22:59:25 +08:00
|
|
|
|
], [
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'status' => 1,
|
2020-11-09 22:59:25 +08:00
|
|
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
|
|
|
'updated_at' => date('Y-m-d H:i:s')
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理聊天记录信息
|
|
|
|
|
*
|
|
|
|
|
* @param array $rows 聊天记录
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function handleChatRecords(array $rows)
|
|
|
|
|
{
|
2020-12-01 13:54:40 +08:00
|
|
|
|
if (empty($rows)) return [];
|
2020-11-09 22:59:25 +08:00
|
|
|
|
|
|
|
|
|
$files = $codes = $forwards = $invites = [];
|
|
|
|
|
foreach ($rows as $value) {
|
|
|
|
|
switch ($value['msg_type']) {
|
|
|
|
|
case 2:
|
|
|
|
|
$files[] = $value['id'];
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
$invites[] = $value['id'];
|
|
|
|
|
break;
|
|
|
|
|
case 4:
|
|
|
|
|
$forwards[] = $value['id'];
|
|
|
|
|
break;
|
|
|
|
|
case 5:
|
|
|
|
|
$codes[] = $value['id'];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查询聊天文件信息
|
|
|
|
|
if ($files) {
|
|
|
|
|
$files = ChatRecordsFile::whereIn('record_id', $files)->get(['id', 'record_id', 'user_id', 'file_source', 'file_type', 'save_type', 'original_name', 'file_suffix', 'file_size', 'save_dir'])->keyBy('record_id')->toArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查询群聊邀请信息
|
|
|
|
|
if ($invites) {
|
|
|
|
|
$invites = ChatRecordsInvite::whereIn('record_id', $invites)->get(['record_id', 'type', 'operate_user_id', 'user_ids'])->keyBy('record_id')->toArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查询代码块消息
|
|
|
|
|
if ($codes) {
|
|
|
|
|
$codes = ChatRecordsCode::whereIn('record_id', $codes)->get(['record_id', 'code_lang', 'code'])->keyBy('record_id')->toArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查询消息转发信息
|
|
|
|
|
if ($forwards) {
|
|
|
|
|
$forwards = ChatRecordsForward::whereIn('record_id', $forwards)->get(['record_id', 'records_id', 'text'])->keyBy('record_id')->toArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($rows as $k => $row) {
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$rows[$k]['file'] = [];
|
2020-11-09 22:59:25 +08:00
|
|
|
|
$rows[$k]['code_block'] = [];
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$rows[$k]['forward'] = [];
|
|
|
|
|
$rows[$k]['invite'] = [];
|
2020-11-09 22:59:25 +08:00
|
|
|
|
|
|
|
|
|
switch ($row['msg_type']) {
|
2021-04-22 16:54:01 +08:00
|
|
|
|
case 2:// 文件消息
|
2020-11-09 22:59:25 +08:00
|
|
|
|
$rows[$k]['file'] = $files[$row['id']] ?? [];
|
|
|
|
|
if ($rows[$k]['file']) {
|
|
|
|
|
$rows[$k]['file']['file_url'] = get_media_url($rows[$k]['file']['save_dir']);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2021-04-22 16:54:01 +08:00
|
|
|
|
case 3:// 入群消息/退群消息
|
2020-11-09 22:59:25 +08:00
|
|
|
|
if (isset($invites[$row['id']])) {
|
|
|
|
|
$rows[$k]['invite'] = [
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'type' => $invites[$row['id']]['type'],
|
2020-11-09 22:59:25 +08:00
|
|
|
|
'operate_user' => [
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'id' => $invites[$row['id']]['operate_user_id'],
|
2020-11-09 22:59:25 +08:00
|
|
|
|
'nickname' => User::where('id', $invites[$row['id']]['operate_user_id'])->value('nickname')
|
|
|
|
|
],
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'users' => []
|
2020-11-09 22:59:25 +08:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if ($rows[$k]['invite']['type'] == 1 || $rows[$k]['invite']['type'] == 3) {
|
2020-12-01 13:54:40 +08:00
|
|
|
|
$rows[$k]['invite']['users'] = User::select('id', 'nickname')->whereIn('id', parse_ids($invites[$row['id']]['user_ids']))->get()->toArray();
|
2020-11-09 22:59:25 +08:00
|
|
|
|
} else {
|
|
|
|
|
$rows[$k]['invite']['users'] = $rows[$k]['invite']['operate_user'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2021-04-22 16:54:01 +08:00
|
|
|
|
case 4:// 会话记录消息
|
2020-11-09 22:59:25 +08:00
|
|
|
|
if (isset($forwards[$row['id']])) {
|
|
|
|
|
$rows[$k]['forward'] = [
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'num' => substr_count($forwards[$row['id']]['records_id'], ',') + 1,
|
2020-11-09 22:59:25 +08:00
|
|
|
|
'list' => json_decode($forwards[$row['id']]['text'], true) ?? []
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
break;
|
2021-04-22 16:54:01 +08:00
|
|
|
|
case 5:// 代码块消息
|
2020-11-09 22:59:25 +08:00
|
|
|
|
$rows[$k]['code_block'] = $codes[$row['id']] ?? [];
|
|
|
|
|
if ($rows[$k]['code_block']) {
|
|
|
|
|
$rows[$k]['code_block']['code'] = htmlspecialchars_decode($rows[$k]['code_block']['code']);
|
|
|
|
|
unset($rows[$k]['code_block']['record_id']);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unset($files, $codes, $forwards, $invites);
|
|
|
|
|
return $rows;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询对话页面的历史聊天记录
|
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
|
* @param int $user_id 用户ID
|
|
|
|
|
* @param int $receive_id 接收者ID(好友ID或群ID)
|
|
|
|
|
* @param int $source 消息来源 1:好友消息 2:群聊消息
|
|
|
|
|
* @param int $record_id 上一次查询的聊天记录ID
|
|
|
|
|
* @param int $limit 查询数据长度
|
|
|
|
|
* @param array $msg_type 消息类型
|
2020-12-03 11:57:46 +08:00
|
|
|
|
* @return array
|
2020-11-09 22:59:25 +08:00
|
|
|
|
*/
|
|
|
|
|
public function getChatRecords(int $user_id, int $receive_id, int $source, int $record_id, $limit = 30, $msg_type = [])
|
|
|
|
|
{
|
|
|
|
|
$fields = [
|
|
|
|
|
'chat_records.id',
|
|
|
|
|
'chat_records.source',
|
|
|
|
|
'chat_records.msg_type',
|
|
|
|
|
'chat_records.user_id',
|
|
|
|
|
'chat_records.receive_id',
|
|
|
|
|
'chat_records.content',
|
|
|
|
|
'chat_records.is_revoke',
|
|
|
|
|
'chat_records.created_at',
|
|
|
|
|
'users.nickname',
|
|
|
|
|
'users.avatar as avatar',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$rowsSqlObj = ChatRecord::select($fields);
|
|
|
|
|
|
|
|
|
|
$rowsSqlObj->leftJoin('users', 'users.id', '=', 'chat_records.user_id');
|
|
|
|
|
if ($record_id) {
|
|
|
|
|
$rowsSqlObj->where('chat_records.id', '<', $record_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($source == 1) {
|
|
|
|
|
$rowsSqlObj->where(function ($query) use ($user_id, $receive_id) {
|
|
|
|
|
$query->where([
|
|
|
|
|
['chat_records.user_id', '=', $user_id],
|
|
|
|
|
['chat_records.receive_id', '=', $receive_id]
|
|
|
|
|
])->orWhere([
|
|
|
|
|
['chat_records.user_id', '=', $receive_id],
|
|
|
|
|
['chat_records.receive_id', '=', $user_id]
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
$rowsSqlObj->where('chat_records.receive_id', $receive_id);
|
|
|
|
|
$rowsSqlObj->where('chat_records.source', $source);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($msg_type) {
|
|
|
|
|
$rowsSqlObj->whereIn('chat_records.msg_type', $msg_type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//过滤用户删除记录
|
|
|
|
|
$rowsSqlObj->whereNotExists(function ($query) use ($user_id) {
|
|
|
|
|
$prefix = config('databases.default.prefix');
|
|
|
|
|
$query->select(Db::raw(1))->from('chat_records_delete');
|
|
|
|
|
$query->whereRaw("{$prefix}chat_records_delete.record_id = {$prefix}chat_records.id and {$prefix}chat_records_delete.user_id = {$user_id}");
|
|
|
|
|
$query->limit(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$rows = $rowsSqlObj->orderBy('chat_records.id', 'desc')->limit($limit)->get()->toArray();
|
|
|
|
|
return $this->handleChatRecords($rows);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取转发会话记录信息
|
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
|
* @param int $user_id 用户ID
|
2020-11-09 22:59:25 +08:00
|
|
|
|
* @param int $record_id 聊天记录ID
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getForwardRecords(int $user_id, int $record_id)
|
|
|
|
|
{
|
|
|
|
|
$result = ChatRecord::where('id', $record_id)->first([
|
|
|
|
|
'id', 'source', 'msg_type', 'user_id', 'receive_id', 'content', 'is_revoke', 'created_at'
|
|
|
|
|
]);
|
|
|
|
|
|
2021-04-22 16:54:01 +08:00
|
|
|
|
// 判断是否有权限查看
|
2020-11-09 22:59:25 +08:00
|
|
|
|
if ($result->source == 1 && ($result->user_id != $user_id && $result->receive_id != $user_id)) {
|
|
|
|
|
return [];
|
2021-03-25 17:32:36 +08:00
|
|
|
|
} else if ($result->source == 2 && !Group::isMember($result->receive_id, $user_id)) {
|
2020-11-09 22:59:25 +08:00
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$forward = ChatRecordsForward::where('record_id', $record_id)->first();
|
|
|
|
|
|
|
|
|
|
$fields = [
|
|
|
|
|
'chat_records.id',
|
|
|
|
|
'chat_records.source',
|
|
|
|
|
'chat_records.msg_type',
|
|
|
|
|
'chat_records.user_id',
|
|
|
|
|
'chat_records.receive_id',
|
|
|
|
|
'chat_records.content',
|
|
|
|
|
'chat_records.is_revoke',
|
|
|
|
|
'chat_records.created_at',
|
|
|
|
|
'users.nickname',
|
|
|
|
|
'users.avatar as avatar',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$rowsSqlObj = ChatRecord::select($fields);
|
|
|
|
|
$rowsSqlObj->leftJoin('users', 'users.id', '=', 'chat_records.user_id');
|
|
|
|
|
$rowsSqlObj->whereIn('chat_records.id', explode(',', $forward->records_id));
|
|
|
|
|
|
|
|
|
|
return $this->handleChatRecords($rowsSqlObj->get()->toArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 批量删除聊天消息
|
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
|
* @param int $user_id 用户ID
|
|
|
|
|
* @param int $source 消息来源 1:好友消息 2:群聊消息
|
|
|
|
|
* @param int $receive_id 好友ID或者群聊ID
|
2020-11-09 22:59:25 +08:00
|
|
|
|
* @param array $record_ids 聊天记录ID
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function removeRecords(int $user_id, int $source, int $receive_id, array $record_ids)
|
|
|
|
|
{
|
2021-04-22 16:54:01 +08:00
|
|
|
|
if ($source == 1) {// 私聊信息
|
2020-11-09 22:59:25 +08:00
|
|
|
|
$ids = ChatRecord::whereIn('id', $record_ids)->where(function ($query) use ($user_id, $receive_id) {
|
|
|
|
|
$query->where([['user_id', '=', $user_id], ['receive_id', '=', $receive_id]])->orWhere([['user_id', '=', $receive_id], ['receive_id', '=', $user_id]]);
|
|
|
|
|
})->where('source', 1)->pluck('id');
|
2021-04-22 16:54:01 +08:00
|
|
|
|
} else {// 群聊信息
|
2020-11-09 22:59:25 +08:00
|
|
|
|
$ids = ChatRecord::whereIn('id', $record_ids)->where('source', 2)->pluck('id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 判断要删除的消息在数据库中是否存在
|
|
|
|
|
if (count($ids) != count($record_ids)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 判读是否属于群消息并且判断是否是群成员
|
2021-03-25 17:32:36 +08:00
|
|
|
|
if ($source == 2 && !Group::isMember($receive_id, $user_id)) {
|
2020-11-09 22:59:25 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = array_map(function ($record_id) use ($user_id) {
|
|
|
|
|
return [
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'record_id' => $record_id,
|
|
|
|
|
'user_id' => $user_id,
|
2020-11-09 22:59:25 +08:00
|
|
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
|
|
|
];
|
|
|
|
|
}, $ids->toArray());
|
|
|
|
|
|
|
|
|
|
return Db::table('chat_records_delete')->insert($data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 撤回单条聊天消息
|
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
|
* @param int $user_id 用户ID
|
2020-11-09 22:59:25 +08:00
|
|
|
|
* @param int $record_id 聊天记录ID
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function revokeRecord(int $user_id, int $record_id)
|
|
|
|
|
{
|
|
|
|
|
$result = ChatRecord::where('id', $record_id)->first(['id', 'source', 'user_id', 'receive_id', 'created_at']);
|
|
|
|
|
if (!$result) return [false, '消息记录不存在'];
|
|
|
|
|
|
2021-04-22 16:54:01 +08:00
|
|
|
|
// 判断是否在两分钟之内撤回消息,超过2分钟不能撤回消息
|
2020-11-09 22:59:25 +08:00
|
|
|
|
if ((time() - strtotime($result->created_at) > 120)) {
|
|
|
|
|
return [false, '已超过有效的撤回时间', []];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($result->source == 1) {
|
|
|
|
|
if ($result->user_id != $user_id && $result->receive_id != $user_id) {
|
|
|
|
|
return [false, '非法操作', []];
|
|
|
|
|
}
|
|
|
|
|
} else if ($result->source == 2) {
|
2021-03-25 17:32:36 +08:00
|
|
|
|
if (!Group::isMember($result->receive_id, $user_id)) {
|
2020-11-09 22:59:25 +08:00
|
|
|
|
return [false, '非法操作', []];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result->is_revoke = 1;
|
|
|
|
|
$result->save();
|
|
|
|
|
|
|
|
|
|
return [true, '消息已撤回', $result->toArray()];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 转发消息(单条转发)
|
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
|
* @param int $user_id 转发的用户ID
|
|
|
|
|
* @param int $record_id 转发消息的记录ID
|
2021-04-22 16:14:34 +08:00
|
|
|
|
* @param array $receive_ids 接受者数组 例如:[['source' => 1,'id' => 3045],['source' => 1,'id' => 3046],['source' =>
|
|
|
|
|
* 1,'id' => 1658]] 二维数组
|
2020-11-09 22:59:25 +08:00
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function forwardRecords(int $user_id, int $record_id, array $receive_ids)
|
|
|
|
|
{
|
|
|
|
|
$result = ChatRecord::where('id', $record_id)->whereIn('msg_type', [1, 2, 5])->first();
|
|
|
|
|
if (!$result) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据消息类型判断用户是否有转发权限
|
|
|
|
|
if ($result->source == 1) {
|
|
|
|
|
if ($result->user_id != $user_id && $result->receive_id != $user_id) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
} else if ($result->source == 2) {
|
2021-03-25 17:32:36 +08:00
|
|
|
|
if (!Group::isMember($result->receive_id, $user_id)) {
|
2020-11-09 22:59:25 +08:00
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$fileInfo = null;
|
2020-11-09 22:59:25 +08:00
|
|
|
|
$codeBlock = null;
|
|
|
|
|
if ($result->msg_type == 2) {
|
|
|
|
|
$fileInfo = ChatRecordsFile::where('record_id', $record_id)->first();
|
|
|
|
|
} else if ($result->msg_type == 5) {
|
|
|
|
|
$codeBlock = ChatRecordsCode::where('record_id', $record_id)->first();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$insRecordIds = [];
|
|
|
|
|
Db::beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
foreach ($receive_ids as $item) {
|
|
|
|
|
$res = ChatRecord::create([
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'source' => $item['source'],
|
|
|
|
|
'msg_type' => $result->msg_type,
|
|
|
|
|
'user_id' => $user_id,
|
2020-11-09 22:59:25 +08:00
|
|
|
|
'receive_id' => $item['id'],
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'content' => $result->content,
|
2020-11-09 22:59:25 +08:00
|
|
|
|
'created_at' => date('Y-m-d H:i:s')
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (!$res) {
|
|
|
|
|
throw new Exception('插入消息记录失败');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$insRecordIds[] = $res->id;
|
|
|
|
|
|
|
|
|
|
if ($result->msg_type == 2) {
|
|
|
|
|
if (!ChatRecordsFile::create([
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'record_id' => $res->id,
|
|
|
|
|
'user_id' => $fileInfo->user_id,
|
|
|
|
|
'file_source' => $fileInfo->file_source,
|
|
|
|
|
'file_type' => $fileInfo->file_type,
|
|
|
|
|
'save_type' => $fileInfo->save_type,
|
2020-11-09 22:59:25 +08:00
|
|
|
|
'original_name' => $fileInfo->original_name,
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'file_suffix' => $fileInfo->file_suffix,
|
|
|
|
|
'file_size' => $fileInfo->file_size,
|
|
|
|
|
'save_dir' => $fileInfo->save_dir,
|
|
|
|
|
'created_at' => date('Y-m-d H:i:s')
|
2020-11-09 22:59:25 +08:00
|
|
|
|
])) {
|
2020-12-03 11:57:46 +08:00
|
|
|
|
throw new Exception('插入文件消息记录失败');
|
2020-11-09 22:59:25 +08:00
|
|
|
|
}
|
|
|
|
|
} else if ($result->msg_type == 5) {
|
|
|
|
|
if (!ChatRecordsCode::create([
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'record_id' => $res->id,
|
|
|
|
|
'user_id' => $user_id,
|
|
|
|
|
'code_lang' => $codeBlock->code_lang,
|
|
|
|
|
'code' => $codeBlock->code,
|
2020-11-09 22:59:25 +08:00
|
|
|
|
'created_at' => date('Y-m-d H:i:s')
|
|
|
|
|
])) {
|
2020-12-03 11:57:46 +08:00
|
|
|
|
throw new Exception('插入代码消息记录失败');
|
2020-11-09 22:59:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Db::commit();
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
Db::rollBack();
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $insRecordIds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 转发消息(多条合并转发)
|
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
|
* @param int $user_id 转发的用户ID
|
|
|
|
|
* @param int $receive_id 当前转发消息的所属者(好友ID或者群聊ID)
|
|
|
|
|
* @param int $source 消息来源 1:好友消息 2:群聊消息
|
2020-11-09 22:59:25 +08:00
|
|
|
|
* @param array $records_ids 转发消息的记录ID
|
2021-04-22 16:14:34 +08:00
|
|
|
|
* @param array $receive_ids 接受者数组 例如:[['source' => 1,'id' => 3045],['source' => 1,'id' => 3046],['source' =>
|
|
|
|
|
* 1,'id' => 1658]] 二维数组
|
2020-12-02 17:15:32 +08:00
|
|
|
|
* @return array|bool
|
2020-11-09 22:59:25 +08:00
|
|
|
|
*/
|
|
|
|
|
public function mergeForwardRecords(int $user_id, int $receive_id, int $source, $records_ids, array $receive_ids)
|
|
|
|
|
{
|
|
|
|
|
// 支持转发的消息类型
|
|
|
|
|
$msg_type = [1, 2, 5];
|
|
|
|
|
|
|
|
|
|
$sqlObj = ChatRecord::whereIn('id', $records_ids);
|
|
|
|
|
|
2021-04-22 16:14:34 +08:00
|
|
|
|
// 验证是否有权限转发
|
|
|
|
|
if ($source == 2) {// 群聊消息
|
|
|
|
|
// 判断是否是群聊成员
|
2021-03-25 17:32:36 +08:00
|
|
|
|
if (!Group::isMember($receive_id, $user_id)) {
|
2020-11-21 22:47:21 +08:00
|
|
|
|
return false;
|
2020-11-09 22:59:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sqlObj = $sqlObj->where('receive_id', $receive_id)->whereIn('msg_type', $msg_type)->where('source', 2)->where('is_revoke', 0);
|
2021-04-22 16:14:34 +08:00
|
|
|
|
} else {// 私聊消息
|
|
|
|
|
// 判断是否存在好友关系
|
2020-11-09 22:59:25 +08:00
|
|
|
|
if (!UsersFriend::isFriend($user_id, $receive_id)) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sqlObj = $sqlObj->where(function ($query) use ($user_id, $receive_id) {
|
|
|
|
|
$query->where([
|
|
|
|
|
['user_id', '=', $user_id],
|
|
|
|
|
['receive_id', '=', $receive_id]
|
|
|
|
|
])->orWhere([
|
|
|
|
|
['user_id', '=', $receive_id],
|
|
|
|
|
['receive_id', '=', $user_id]
|
|
|
|
|
]);
|
|
|
|
|
})->whereIn('msg_type', $msg_type)->where('source', 1)->where('is_revoke', 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result = $sqlObj->get();
|
|
|
|
|
|
2021-04-22 16:14:34 +08:00
|
|
|
|
// 判断消息记录是否存在
|
2020-11-09 22:59:25 +08:00
|
|
|
|
if (count($result) != count($records_ids)) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$rows = ChatRecord::leftJoin('users', 'users.id', '=', 'chat_records.user_id')
|
|
|
|
|
->whereIn('chat_records.id', array_slice($records_ids, 0, 3))
|
|
|
|
|
->get(['chat_records.msg_type', 'chat_records.content', 'users.nickname']);
|
|
|
|
|
|
|
|
|
|
$jsonText = [];
|
|
|
|
|
foreach ($rows as $row) {
|
2020-12-02 17:15:32 +08:00
|
|
|
|
switch ($row->msg_type) {
|
|
|
|
|
case 1:
|
|
|
|
|
$jsonText[] = [
|
|
|
|
|
'nickname' => $row->nickname,
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'text' => mb_substr(str_replace(PHP_EOL, "", $row->content), 0, 30)
|
2020-12-02 17:15:32 +08:00
|
|
|
|
];
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
$jsonText[] = [
|
|
|
|
|
'nickname' => $row->nickname,
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'text' => '【文件消息】'
|
2020-12-02 17:15:32 +08:00
|
|
|
|
];
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
$jsonText[] = [
|
|
|
|
|
'nickname' => $row->nickname,
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'text' => '【代码消息】'
|
2020-12-02 17:15:32 +08:00
|
|
|
|
];
|
|
|
|
|
break;
|
2020-11-09 22:59:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$insRecordIds = [];
|
|
|
|
|
Db::beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
foreach ($receive_ids as $item) {
|
|
|
|
|
$res = ChatRecord::create([
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'source' => $item['source'],
|
|
|
|
|
'msg_type' => 4,
|
|
|
|
|
'user_id' => $user_id,
|
2020-11-09 22:59:25 +08:00
|
|
|
|
'receive_id' => $item['id'],
|
|
|
|
|
'created_at' => date('Y-m-d H:i:s')
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (!$res) {
|
|
|
|
|
throw new Exception('插入消息失败');
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-21 22:47:21 +08:00
|
|
|
|
$insRecordIds[] = [
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'record_id' => $res->id,
|
2020-11-21 22:47:21 +08:00
|
|
|
|
'receive_id' => $item['id'],
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'source' => $item['source']
|
2020-11-21 22:47:21 +08:00
|
|
|
|
];
|
2020-11-09 22:59:25 +08:00
|
|
|
|
|
|
|
|
|
if (!ChatRecordsForward::create([
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'record_id' => $res->id,
|
|
|
|
|
'user_id' => $user_id,
|
2020-11-09 22:59:25 +08:00
|
|
|
|
'records_id' => implode(',', $records_ids),
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'text' => json_encode($jsonText),
|
2020-11-09 22:59:25 +08:00
|
|
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
|
|
|
])) {
|
|
|
|
|
throw new Exception('插入转发消息失败');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Db::commit();
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
Db::rollBack();
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $insRecordIds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 关键词搜索聊天记录
|
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
|
* @param int $user_id 用户ID
|
|
|
|
|
* @param int $receive_id 接收者ID(用户ID或群聊接收ID)
|
|
|
|
|
* @param int $source 聊天来源(1:私信 2:群聊)
|
|
|
|
|
* @param int $page 当前查询分页
|
|
|
|
|
* @param int $page_size 分页大小
|
|
|
|
|
* @param array $params 查询参数
|
2021-04-22 16:14:34 +08:00
|
|
|
|
* @return array
|
2020-11-09 22:59:25 +08:00
|
|
|
|
*/
|
|
|
|
|
public function searchRecords(int $user_id, int $receive_id, int $source, int $page, int $page_size, array $params)
|
|
|
|
|
{
|
|
|
|
|
$fields = [
|
|
|
|
|
'chat_records.id',
|
|
|
|
|
'chat_records.source',
|
|
|
|
|
'chat_records.msg_type',
|
|
|
|
|
'chat_records.user_id',
|
|
|
|
|
'chat_records.receive_id',
|
|
|
|
|
'chat_records.content',
|
|
|
|
|
'chat_records.is_revoke',
|
|
|
|
|
'chat_records.created_at',
|
|
|
|
|
|
|
|
|
|
'users.nickname',
|
|
|
|
|
'users.avatar as avatar',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$rowsSqlObj = ChatRecord::select($fields)->leftJoin('users', 'users.id', '=', 'chat_records.user_id');
|
|
|
|
|
if ($source == 1) {
|
|
|
|
|
$rowsSqlObj->where(function ($query) use ($user_id, $receive_id) {
|
|
|
|
|
$query->where([
|
|
|
|
|
['chat_records.user_id', '=', $user_id],
|
|
|
|
|
['chat_records.receive_id', '=', $receive_id]
|
|
|
|
|
])->orWhere([
|
|
|
|
|
['chat_records.user_id', '=', $receive_id],
|
|
|
|
|
['chat_records.receive_id', '=', $user_id]
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
$rowsSqlObj->where('chat_records.receive_id', $receive_id);
|
|
|
|
|
$rowsSqlObj->where('chat_records.source', $source);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset($params['keywords'])) {
|
|
|
|
|
$rowsSqlObj->where('chat_records.content', 'like', "%{$params['keywords']}%");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset($params['date'])) {
|
|
|
|
|
$rowsSqlObj->whereDate('chat_records.created_at', $params['date']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$count = $rowsSqlObj->count();
|
|
|
|
|
if ($count == 0) {
|
|
|
|
|
return $this->getPagingRows([], 0, $page, $page_size);
|
|
|
|
|
}
|
2020-11-04 11:57:16 +08:00
|
|
|
|
|
2020-11-09 22:59:25 +08:00
|
|
|
|
$rows = $rowsSqlObj->orderBy('chat_records.id', 'desc')->forPage($page, $page_size)->get()->toArray();
|
|
|
|
|
return $this->getPagingRows($this->handleChatRecords($rows), $count, $page, $page_size);
|
|
|
|
|
}
|
2020-11-21 19:53:01 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建图片消息
|
|
|
|
|
*
|
|
|
|
|
* @param $message
|
|
|
|
|
* @param $fileInfo
|
2020-12-02 15:14:29 +08:00
|
|
|
|
* @return bool|int
|
2020-11-21 19:53:01 +08:00
|
|
|
|
*/
|
|
|
|
|
public function createImgMessage($message, $fileInfo)
|
|
|
|
|
{
|
|
|
|
|
Db::beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
$message['created_at'] = date('Y-m-d H:i:s');
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$insert = ChatRecord::create($message);
|
2020-11-21 19:53:01 +08:00
|
|
|
|
|
|
|
|
|
if (!$insert) {
|
2020-12-03 11:57:46 +08:00
|
|
|
|
throw new Exception('插入聊天记录失败...');
|
2020-11-21 19:53:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$fileInfo['record_id'] = $insert->id;
|
2020-11-21 19:53:01 +08:00
|
|
|
|
$fileInfo['created_at'] = date('Y-m-d H:i:s');
|
|
|
|
|
if (!ChatRecordsFile::create($fileInfo)) {
|
2020-12-03 11:57:46 +08:00
|
|
|
|
throw new Exception('插入聊天记录(文件消息)失败...');
|
2020-11-21 19:53:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Db::commit();
|
2020-12-03 11:57:46 +08:00
|
|
|
|
} catch (Exception $e) {
|
2020-11-21 19:53:01 +08:00
|
|
|
|
Db::rollBack();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $insert->id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建代码块消息
|
|
|
|
|
*
|
|
|
|
|
* @param array $message
|
|
|
|
|
* @param array $codeBlock
|
2020-12-02 15:14:29 +08:00
|
|
|
|
* @return bool|int
|
2020-11-21 19:53:01 +08:00
|
|
|
|
*/
|
|
|
|
|
public function createCodeMessage(array $message, array $codeBlock)
|
|
|
|
|
{
|
|
|
|
|
Db::beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
$message['created_at'] = date('Y-m-d H:i:s');
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$insert = ChatRecord::create($message);
|
2020-11-21 19:53:01 +08:00
|
|
|
|
if (!$insert) {
|
2020-12-03 11:57:46 +08:00
|
|
|
|
throw new Exception('插入聊天记录失败...');
|
2020-11-21 19:53:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$codeBlock['record_id'] = $insert->id;
|
2020-11-21 19:53:01 +08:00
|
|
|
|
$codeBlock['created_at'] = date('Y-m-d H:i:s');
|
|
|
|
|
if (!ChatRecordsCode::create($codeBlock)) {
|
2020-12-03 11:57:46 +08:00
|
|
|
|
throw new Exception('插入聊天记录(代码消息)失败...');
|
2020-11-21 19:53:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Db::commit();
|
2020-12-03 11:57:46 +08:00
|
|
|
|
} catch (Exception $e) {
|
2020-11-21 19:53:01 +08:00
|
|
|
|
Db::rollBack();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $insert->id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建代码块消息
|
|
|
|
|
*
|
|
|
|
|
* @param array $message
|
|
|
|
|
* @param array $emoticon
|
2020-12-02 15:14:29 +08:00
|
|
|
|
* @return bool|int
|
2020-11-21 19:53:01 +08:00
|
|
|
|
*/
|
|
|
|
|
public function createEmoticonMessage(array $message, array $emoticon)
|
|
|
|
|
{
|
|
|
|
|
Db::beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
$message['created_at'] = date('Y-m-d H:i:s');
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$insert = ChatRecord::create($message);
|
2020-11-21 19:53:01 +08:00
|
|
|
|
if (!$insert) {
|
2020-12-03 11:57:46 +08:00
|
|
|
|
throw new Exception('插入聊天记录失败...');
|
2020-11-21 19:53:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$emoticon['record_id'] = $insert->id;
|
2020-11-21 19:53:01 +08:00
|
|
|
|
$emoticon['created_at'] = date('Y-m-d H:i:s');
|
|
|
|
|
if (!ChatRecordsFile::create($emoticon)) {
|
2020-12-03 11:57:46 +08:00
|
|
|
|
throw new Exception('插入聊天记录(代码消息)失败...');
|
2020-11-21 19:53:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Db::commit();
|
2020-12-03 11:57:46 +08:00
|
|
|
|
} catch (Exception $e) {
|
2020-11-21 19:53:01 +08:00
|
|
|
|
Db::rollBack();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $insert->id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建代码块消息
|
|
|
|
|
*
|
|
|
|
|
* @param array $message
|
|
|
|
|
* @param array $emoticon
|
2020-12-02 15:14:29 +08:00
|
|
|
|
* @return bool|int
|
2020-11-21 19:53:01 +08:00
|
|
|
|
*/
|
|
|
|
|
public function createFileMessage(array $message, array $emoticon)
|
|
|
|
|
{
|
|
|
|
|
Db::beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
$message['created_at'] = date('Y-m-d H:i:s');
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$insert = ChatRecord::create($message);
|
2020-11-21 19:53:01 +08:00
|
|
|
|
if (!$insert) {
|
2020-12-03 11:57:46 +08:00
|
|
|
|
throw new Exception('插入聊天记录失败...');
|
2020-11-21 19:53:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 17:32:36 +08:00
|
|
|
|
$emoticon['record_id'] = $insert->id;
|
2020-11-21 19:53:01 +08:00
|
|
|
|
$emoticon['created_at'] = date('Y-m-d H:i:s');
|
|
|
|
|
if (!ChatRecordsFile::create($emoticon)) {
|
2020-12-03 11:57:46 +08:00
|
|
|
|
throw new Exception('插入聊天记录(代码消息)失败...');
|
2020-11-21 19:53:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Db::commit();
|
2020-12-03 11:57:46 +08:00
|
|
|
|
} catch (Exception $e) {
|
2020-11-21 19:53:01 +08:00
|
|
|
|
Db::rollBack();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $insert->id;
|
|
|
|
|
}
|
2020-11-04 11:57:16 +08:00
|
|
|
|
}
|