初始化仓库及v1.0.0提交
This commit is contained in:
106
app/Controllers/ConfigController.php
Normal file
106
app/Controllers/ConfigController.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Models\Config;
|
||||
use App\Models\Personality;
|
||||
|
||||
class ConfigController
|
||||
{
|
||||
public static function getConfig(): void
|
||||
{
|
||||
$configRow = Config::getByKey('providers');
|
||||
$providers = $configRow ? json_decode($configRow['config_value'], true) : [];
|
||||
echo json_encode(['success' => true, 'data' => ['providers' => $providers]]);
|
||||
}
|
||||
|
||||
public static function updateConfig(): void
|
||||
{
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$providers = $input['providers'] ?? [];
|
||||
|
||||
if (!is_array($providers)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'providers 必须是数组']);
|
||||
return;
|
||||
}
|
||||
|
||||
Config::setByKey('providers', json_encode($providers, JSON_UNESCAPED_UNICODE));
|
||||
echo json_encode(['success' => true, 'message' => '配置更新成功']);
|
||||
}
|
||||
|
||||
public static function listPersonalities(): void
|
||||
{
|
||||
$personalities = Personality::findAll();
|
||||
echo json_encode(['success' => true, 'data' => $personalities]);
|
||||
}
|
||||
|
||||
public static function createPersonality(): void
|
||||
{
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$name = $input['name'] ?? '';
|
||||
$prompt = $input['prompt'] ?? '';
|
||||
|
||||
if (!$name || !$prompt) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => '名称和提示词不能为空']);
|
||||
return;
|
||||
}
|
||||
|
||||
$data = [
|
||||
'name' => $name,
|
||||
'prompt' => $prompt,
|
||||
'description' => $input['description'] ?? null,
|
||||
'icon' => $input['icon'] ?? null,
|
||||
'is_preset' => 0,
|
||||
'created_by' => $GLOBALS['auth_user']['userId']
|
||||
];
|
||||
|
||||
$personality = Personality::create($data);
|
||||
echo json_encode(['success' => true, 'data' => $personality]);
|
||||
}
|
||||
|
||||
public static function updatePersonality($id): void
|
||||
{
|
||||
$personality = Personality::findById($id);
|
||||
if (!$personality) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['success' => false, 'message' => '人格不存在']);
|
||||
return;
|
||||
}
|
||||
if ($personality['is_preset']) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => '预设人格不可编辑']);
|
||||
return;
|
||||
}
|
||||
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$data = array_filter([
|
||||
'name' => $input['name'] ?? null,
|
||||
'prompt' => $input['prompt'] ?? null,
|
||||
'description' => $input['description'] ?? null,
|
||||
'icon' => $input['icon'] ?? null,
|
||||
], fn($v) => $v !== null);
|
||||
|
||||
$updated = Personality::update($id, $data);
|
||||
echo json_encode(['success' => true, 'data' => $updated]);
|
||||
}
|
||||
|
||||
public static function deletePersonality($id): void
|
||||
{
|
||||
$personality = Personality::findById($id);
|
||||
if (!$personality) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['success' => false, 'message' => '人格不存在']);
|
||||
return;
|
||||
}
|
||||
if ($personality['is_preset']) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => '预设人格不可删除']);
|
||||
return;
|
||||
}
|
||||
|
||||
Personality::delete($id);
|
||||
echo json_encode(['success' => true, 'message' => '删除成功']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user