获取历史消息
This commit is contained in:
@@ -6,79 +6,73 @@ use app\chat\model\ChatUnreadCount;
|
|||||||
use app\chat\model\ChatTop;
|
use app\chat\model\ChatTop;
|
||||||
use app\chat\model\ChatUser;
|
use app\chat\model\ChatUser;
|
||||||
use app\chat\model\ChatGroup;
|
use app\chat\model\ChatGroup;
|
||||||
|
use app\common\controller\ApiController;
|
||||||
use app\utils\Session;
|
use app\utils\Session;
|
||||||
use support\Request;
|
use support\Request;
|
||||||
use support\Response;
|
use support\Response;
|
||||||
|
use support\think\Db;
|
||||||
|
|
||||||
class MessageController
|
class MessageController extends ApiController
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* 获取历史消息
|
* 获取历史消息
|
||||||
*/
|
*/
|
||||||
public function history(Request $request): Response
|
public function history(Request $request): Response
|
||||||
{
|
{
|
||||||
$uid = $request->uid;
|
$uid = $this->uid;
|
||||||
$chatType = $request->get('chat_type', 1); // 1=单聊,2=群聊
|
$chatType = $request->post('chat_type'); // 1=单聊,2=群聊
|
||||||
$toId = $request->get('to_id', 0); // 单聊=对方ID,群聊=群ID
|
$toId = $request->post('to_id'); // 单聊=对方ID,群聊=群ID
|
||||||
$page = $request->get('page', 1);
|
$session_id = $request->post('session_id'); // 会话ID
|
||||||
$size = $request->get('size', 20);
|
$page = $request->post('page', 1)?:1;
|
||||||
|
$size = $request->post('size', 20)?:20;
|
||||||
|
|
||||||
if (!$toId) {
|
if (!$toId || !$chatType || !$session_id) {
|
||||||
return json(['code' => 400, 'msg' => '缺少to_id']);
|
return $this->error('参数不完整');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成会话ID
|
|
||||||
$sessionId = Session::generateSessionId($chatType, $uid, $toId);
|
|
||||||
|
|
||||||
// 构建查询条件
|
// 构建查询条件
|
||||||
$query = ChatMessage::where('session_id', $sessionId);
|
$query = Db::name('chat_message')->where(['session_id' => $session_id]);
|
||||||
|
|
||||||
// 群聊需验证是否在群内
|
// 群聊需验证是否在群内
|
||||||
if ($chatType == 2) {
|
if ($chatType == 2) {
|
||||||
$isMember = app\chat\model\ChatGroupMember::where([
|
$isMember = Db::name('chat_group_member')->where([
|
||||||
'group_id' => $toId,
|
'group_id' => $toId,
|
||||||
'user_id' => $uid,
|
'user_id' => $uid,
|
||||||
'quit_time' => null,
|
'quit_time' => null,
|
||||||
'is_kicked' => 0
|
'is_kicked' => 0
|
||||||
])->exists();
|
])->find();
|
||||||
if (!$isMember) {
|
if (!$isMember) {
|
||||||
return json(['code' => 403, 'msg' => '不在群内,无法获取历史消息']);
|
return $this->error('不在群内,无法获取历史消息');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 分页查询(倒序取,再正序返回)
|
// 分页查询(倒序取,再正序返回)
|
||||||
$total = $query->count();
|
$total = $query->count();
|
||||||
$messages = $query->orderBy('send_time', 'desc')
|
$messages = $query->order('send_time', 'desc')
|
||||||
->page($page, $size)
|
->page($page, $size)
|
||||||
->get()
|
->select()->toArray();
|
||||||
->toArray();
|
|
||||||
$messages = array_reverse($messages);
|
$messages = array_reverse($messages);
|
||||||
|
|
||||||
// 单聊自动标记已读
|
// 单聊自动标记已读
|
||||||
if ($chatType == 1) {
|
if ($chatType == 1) {
|
||||||
ChatMessage::where([
|
Db::name('chat_message')->where([
|
||||||
'session_id' => $sessionId,
|
'session_id' => $session_id,
|
||||||
'to_id' => $uid,
|
'to_id' => $uid,
|
||||||
'is_read' => 0
|
'is_read' => 0
|
||||||
])->update(['is_read' => 1]);
|
])->update(['is_read' => 1]);
|
||||||
|
|
||||||
// 重置未读计数
|
// 重置未读计数
|
||||||
ChatUnreadCount::where([
|
Db::name('chat_unread_count')->where([
|
||||||
'user_id' => $uid,
|
'user_id' => $uid,
|
||||||
'session_id' => $sessionId
|
'session_id' => $session_id
|
||||||
])->update(['count' => 0, 'updated_at' => time()]);
|
])->update(['count' => 0, 'updated_time' => d()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return json([
|
return $this->success([
|
||||||
'code' => 200,
|
|
||||||
'msg' => 'success',
|
|
||||||
'data' => [
|
|
||||||
'list' => $messages,
|
'list' => $messages,
|
||||||
'page' => $page,
|
'page' => $page,
|
||||||
'size' => $size,
|
'size' => $size,
|
||||||
'total' => $total
|
'total' => $total]);
|
||||||
]
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user