更新
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace app\controller\api;
|
||||
|
||||
use app\model\Conversation as ConversationModel;
|
||||
use app\model\Message;
|
||||
use app\service\PermissionService;
|
||||
use think\exception\HttpResponseException;
|
||||
|
||||
class Conversation extends BaseApi
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$user = $this->authUser();
|
||||
$page = max(1, (int) $this->request->get('page', 1));
|
||||
$limit = min(50, max(1, (int) $this->request->get('limit', 20)));
|
||||
|
||||
$query = ConversationModel::alias('c')
|
||||
->leftJoin('ai_models m', 'c.model_id = m.id')
|
||||
->where('c.user_id', $user['id'])
|
||||
->whereNull('c.deleted_at')
|
||||
->field('c.*,m.name as model_name')
|
||||
->order(['c.is_pinned' => 'desc', 'c.updated_at' => 'desc']);
|
||||
|
||||
$total = (clone $query)->count();
|
||||
$list = $query->page($page, $limit)->select();
|
||||
|
||||
return $this->success([
|
||||
'list' => $list,
|
||||
'total' => $total,
|
||||
'page' => $page,
|
||||
'limit' => $limit,
|
||||
]);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$user = $this->authUser();
|
||||
PermissionService::checkConversationLimit($user);
|
||||
|
||||
$input = $this->request->post();
|
||||
$conversation = ConversationModel::create([
|
||||
'user_id' => $user['id'],
|
||||
'title' => trim($input['title'] ?? '新对话'),
|
||||
'model_id' => $input['model_id'] ?? null,
|
||||
]);
|
||||
|
||||
return $this->success($this->findConversation((int) $conversation->id, (int) $user['id']), '创建成功');
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$user = $this->authUser();
|
||||
return $this->success($this->findConversation((int) $id, (int) $user['id']));
|
||||
}
|
||||
|
||||
public function update($id)
|
||||
{
|
||||
$user = $this->authUser();
|
||||
$conversation = ConversationModel::where('id', $id)
|
||||
->where('user_id', $user['id'])
|
||||
->whereNull('deleted_at')
|
||||
->find();
|
||||
|
||||
if (!$conversation) {
|
||||
return $this->error('会话不存在', 404);
|
||||
}
|
||||
|
||||
$input = $this->request->put();
|
||||
$data = [];
|
||||
if (isset($input['title'])) {
|
||||
$data['title'] = trim($input['title']);
|
||||
}
|
||||
if (isset($input['is_pinned'])) {
|
||||
$data['is_pinned'] = (int) $input['is_pinned'];
|
||||
}
|
||||
if (array_key_exists('model_id', $input)) {
|
||||
$data['model_id'] = $input['model_id'];
|
||||
}
|
||||
|
||||
if (empty($data)) {
|
||||
return $this->error('无更新内容');
|
||||
}
|
||||
|
||||
$conversation->save($data);
|
||||
return $this->success($this->findConversation((int) $id, (int) $user['id']));
|
||||
}
|
||||
|
||||
public function delete($id)
|
||||
{
|
||||
$user = $this->authUser();
|
||||
$conversation = ConversationModel::where('id', $id)
|
||||
->where('user_id', $user['id'])
|
||||
->whereNull('deleted_at')
|
||||
->find();
|
||||
|
||||
if (!$conversation) {
|
||||
return $this->error('会话不存在', 404);
|
||||
}
|
||||
|
||||
$conversation->save(['deleted_at' => date('Y-m-d H:i:s')]);
|
||||
return $this->success(null, '删除成功');
|
||||
}
|
||||
|
||||
public function messages($id)
|
||||
{
|
||||
$user = $this->authUser();
|
||||
$this->findConversation((int) $id, (int) $user['id']);
|
||||
|
||||
$messages = Message::where('conversation_id', $id)
|
||||
->field('id,role,content,content_type,attachments,created_at')
|
||||
->order('created_at', 'asc')
|
||||
->select()
|
||||
->each(function ($item) {
|
||||
if (is_string($item->attachments)) {
|
||||
$item->attachments = json_decode($item->attachments, true) ?: [];
|
||||
}
|
||||
return $item;
|
||||
});
|
||||
|
||||
return $this->success($messages);
|
||||
}
|
||||
|
||||
private function findConversation(int $id, int $userId): array
|
||||
{
|
||||
$conversation = ConversationModel::alias('c')
|
||||
->leftJoin('ai_models m', 'c.model_id = m.id')
|
||||
->where('c.id', $id)
|
||||
->where('c.user_id', $userId)
|
||||
->whereNull('c.deleted_at')
|
||||
->field('c.*,m.name as model_name')
|
||||
->find();
|
||||
|
||||
if (!$conversation) {
|
||||
throw new HttpResponseException(json([
|
||||
'code' => 1,
|
||||
'message' => '会话不存在',
|
||||
'data' => null,
|
||||
], 404));
|
||||
}
|
||||
|
||||
return $conversation->toArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user