53 lines
1.8 KiB
PHP
53 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\common\service\chat\PrivateChatService;
|
|
|
|
class ChatController extends BaseApiController
|
|
{
|
|
public function sessions()
|
|
{
|
|
$page = (int) $this->request->get('page_no/d', 1);
|
|
$size = (int) $this->request->get('page_size/d', 20);
|
|
return $this->data(PrivateChatService::sessions($this->userId, $page, $size));
|
|
}
|
|
|
|
public function open()
|
|
{
|
|
$targetUserId = (int) $this->request->post('target_user_id/d', 0);
|
|
$result = PrivateChatService::open($this->userId, $targetUserId, true);
|
|
if (empty($result['success'])) {
|
|
return $this->fail($result['error'] ?? '打开会话失败');
|
|
}
|
|
return $this->data($result['data']);
|
|
}
|
|
|
|
public function messages()
|
|
{
|
|
$sessionId = (int) $this->request->get('session_id/d', 0);
|
|
$afterId = (int) $this->request->get('after_id/d', 0);
|
|
$page = (int) $this->request->get('page_no/d', 1);
|
|
$size = (int) $this->request->get('page_size/d', 50);
|
|
|
|
$result = PrivateChatService::messages($this->userId, $sessionId, $afterId, $page, $size);
|
|
if (empty($result['success'])) {
|
|
return $this->fail($result['error'] ?? '获取消息失败');
|
|
}
|
|
return $this->data($result['data']);
|
|
}
|
|
|
|
public function send()
|
|
{
|
|
$sessionId = (int) $this->request->post('session_id/d', 0);
|
|
$messageType = trim((string) $this->request->post('message_type/s', PrivateChatService::MESSAGE_TYPE_TEXT));
|
|
$content = trim((string) $this->request->post('content/s', ''));
|
|
|
|
$result = PrivateChatService::send($this->userId, $sessionId, $messageType, $content);
|
|
if (empty($result['success'])) {
|
|
return $this->fail($result['error'] ?? '发送失败');
|
|
}
|
|
return $this->data($result['data']);
|
|
}
|
|
}
|