89 lines
2.7 KiB
PHP
89 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Models\Session;
|
|
|
|
class SessionController
|
|
{
|
|
public static function index(): void
|
|
{
|
|
$user = $GLOBALS['auth_user'];
|
|
$sessions = Session::findByUserId($user['userId']);
|
|
echo json_encode(['success' => true, 'data' => $sessions]);
|
|
}
|
|
|
|
public static function create(): void
|
|
{
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$user = $GLOBALS['auth_user'];
|
|
|
|
$data = [
|
|
'user_id' => $user['userId'],
|
|
'name' => $input['name'] ?? '新会话',
|
|
'provider' => $input['provider'] ?? 'newapi',
|
|
'model' => $input['model'] ?? 'gpt-3.5-turbo',
|
|
'system_prompt' => $input['system_prompt'] ?? '',
|
|
'thinking_mode' => $input['thinking_mode'] ?? false,
|
|
];
|
|
|
|
if (isset($input['personality_id'])) {
|
|
$data['personality_id'] = $input['personality_id'];
|
|
}
|
|
|
|
$session = Session::create($data);
|
|
echo json_encode(['success' => true, 'data' => $session]);
|
|
}
|
|
|
|
public static function update(int $id): void
|
|
{
|
|
$session = Session::findById($id);
|
|
if (!$session) {
|
|
http_response_code(404);
|
|
echo json_encode(['success' => false, 'message' => '会话不存在']);
|
|
return;
|
|
}
|
|
|
|
$user = $GLOBALS['auth_user'];
|
|
if ($session['user_id'] != $user['userId']) {
|
|
http_response_code(404);
|
|
echo json_encode(['success' => false, 'message' => '会话不存在']);
|
|
return;
|
|
}
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
$allowedFields = ['name', 'provider', 'model', 'system_prompt', 'personality_id', 'thinking_mode'];
|
|
$data = [];
|
|
foreach ($allowedFields as $field) {
|
|
if (isset($input[$field])) {
|
|
$data[$field] = $input[$field];
|
|
}
|
|
}
|
|
|
|
Session::update($id, $data);
|
|
$updatedSession = Session::findById($id);
|
|
echo json_encode(['success' => true, 'data' => $updatedSession]);
|
|
}
|
|
|
|
public static function delete(int $id): void
|
|
{
|
|
$session = Session::findById($id);
|
|
if (!$session) {
|
|
http_response_code(404);
|
|
echo json_encode(['success' => false, 'message' => '会话不存在']);
|
|
return;
|
|
}
|
|
|
|
$user = $GLOBALS['auth_user'];
|
|
if ($session['user_id'] != $user['userId']) {
|
|
http_response_code(404);
|
|
echo json_encode(['success' => false, 'message' => '会话不存在']);
|
|
return;
|
|
}
|
|
|
|
Session::delete($id);
|
|
echo json_encode(['success' => true, 'message' => '删除成功']);
|
|
}
|
|
}
|