64 lines
2.0 KiB
PHP
64 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\common\service\ai\AiAssistantService;
|
|
|
|
class AssistantController extends BaseApiController
|
|
{
|
|
public array $notNeedLogin = ['chat', 'sessions', 'messages', 'clear'];
|
|
|
|
public function chat()
|
|
{
|
|
$message = trim((string) $this->request->post('message', ''));
|
|
$clientId = trim((string) $this->request->post('client_id', ''));
|
|
$sessionId = (int) $this->request->post('session_id/d', 0);
|
|
|
|
if ($message === '') {
|
|
return $this->fail('请输入要咨询的问题');
|
|
}
|
|
|
|
$result = AiAssistantService::chat($message, $this->userId, $clientId, $sessionId, $this->request->ip());
|
|
if (empty($result['success'])) {
|
|
return $this->fail($result['error'] ?? 'AI助手暂时不可用');
|
|
}
|
|
|
|
return $this->data($result['data']);
|
|
}
|
|
|
|
public function sessions()
|
|
{
|
|
$clientId = trim((string) $this->request->get('client_id', ''));
|
|
return $this->data(AiAssistantService::sessions($this->userId, $clientId));
|
|
}
|
|
|
|
public function messages()
|
|
{
|
|
$sessionId = (int) $this->request->get('session_id/d', 0);
|
|
$clientId = trim((string) $this->request->get('client_id', ''));
|
|
if ($sessionId <= 0) {
|
|
return $this->fail('会话参数缺失');
|
|
}
|
|
|
|
$result = AiAssistantService::messages($sessionId, $this->userId, $clientId);
|
|
if (empty($result['success'])) {
|
|
return $this->fail($result['error'] ?? '会话不存在');
|
|
}
|
|
|
|
return $this->data($result['data']);
|
|
}
|
|
|
|
public function clear()
|
|
{
|
|
$sessionId = (int) $this->request->post('session_id/d', 0);
|
|
$clientId = trim((string) $this->request->post('client_id', ''));
|
|
|
|
$result = AiAssistantService::clear($sessionId, $this->userId, $clientId);
|
|
if (empty($result['success'])) {
|
|
return $this->fail($result['error'] ?? '清空失败');
|
|
}
|
|
|
|
return $this->success('操作成功');
|
|
}
|
|
}
|