Files
2026-06-11 16:22:12 +08:00

67 lines
2.3 KiB
PHP

<?php
namespace app\api\controller;
use app\common\service\chat\PrivateChatService;
use app\common\service\chat\WhisperAsrService;
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']);
}
public function voiceToText()
{
if ($this->userId <= 0) {
return $this->fail('请先登录');
}
$result = WhisperAsrService::transcribeUploadedFile($this->request->file('file'));
if (empty($result['success'])) {
return $this->fail($result['error'] ?? '语音识别失败');
}
return $this->data($result['data']);
}
}