初始化仓库及v1.0.0提交
This commit is contained in:
0
app/Models/.gitkeep
Normal file
0
app/Models/.gitkeep
Normal file
27
app/Models/Config.php
Normal file
27
app/Models/Config.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Config\Database;
|
||||
|
||||
class Config
|
||||
{
|
||||
public static function getByKey(string $key): ?array
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare('SELECT * FROM config WHERE config_key = :config_key');
|
||||
$stmt->execute(['config_key' => $key]);
|
||||
$result = $stmt->fetch();
|
||||
return $result ?: null;
|
||||
}
|
||||
|
||||
public static function setByKey(string $key, string $value): bool
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare('INSERT INTO config (config_key, config_value) VALUES (:config_key, :config_value) ON DUPLICATE KEY UPDATE config_value = VALUES(config_value)');
|
||||
return $stmt->execute([
|
||||
'config_key' => $key,
|
||||
'config_value' => $value,
|
||||
]);
|
||||
}
|
||||
}
|
||||
33
app/Models/Message.php
Normal file
33
app/Models/Message.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Config\Database;
|
||||
|
||||
class Message
|
||||
{
|
||||
public static function findBySessionId(int $sessionId): array
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare('SELECT * FROM messages WHERE session_id = :session_id ORDER BY created_at ASC');
|
||||
$stmt->execute(['session_id' => $sessionId]);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
public static function create(array $data): array
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare('INSERT INTO messages (session_id, role, content, file_info, thinking_content) VALUES (:session_id, :role, :content, :file_info, :thinking_content)');
|
||||
$stmt->execute([
|
||||
'session_id' => $data['session_id'],
|
||||
'role' => $data['role'],
|
||||
'content' => $data['content'],
|
||||
'file_info' => isset($data['file_info']) ? json_encode($data['file_info']) : null,
|
||||
'thinking_content' => $data['thinking_content'] ?? null,
|
||||
]);
|
||||
|
||||
$stmt = $db->prepare('SELECT * FROM messages WHERE id = :id');
|
||||
$stmt->execute(['id' => (int) $db->lastInsertId()]);
|
||||
return $stmt->fetch();
|
||||
}
|
||||
}
|
||||
65
app/Models/Personality.php
Normal file
65
app/Models/Personality.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Config\Database;
|
||||
|
||||
class Personality
|
||||
{
|
||||
public static function findAll(): array
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->query('SELECT * FROM personalities ORDER BY id ASC');
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
public static function findById(int $id): ?array
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare('SELECT * FROM personalities WHERE id = :id');
|
||||
$stmt->execute(['id' => $id]);
|
||||
$result = $stmt->fetch();
|
||||
return $result ?: null;
|
||||
}
|
||||
|
||||
public static function create(array $data): array
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare('INSERT INTO personalities (name, prompt, description, icon, is_preset, created_by) VALUES (:name, :prompt, :description, :icon, :is_preset, :created_by)');
|
||||
$stmt->execute([
|
||||
'name' => $data['name'],
|
||||
'prompt' => $data['prompt'],
|
||||
'description' => $data['description'] ?? null,
|
||||
'icon' => $data['icon'] ?? null,
|
||||
'is_preset' => $data['is_preset'] ?? 0,
|
||||
'created_by' => $data['created_by'] ?? null,
|
||||
]);
|
||||
|
||||
return self::findById((int) $db->lastInsertId());
|
||||
}
|
||||
|
||||
public static function update(int $id, array $data): bool
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
|
||||
$fields = [];
|
||||
$params = ['id' => $id];
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
$fields[] = "{$key} = :{$key}";
|
||||
$params[$key] = $value;
|
||||
}
|
||||
|
||||
$sql = 'UPDATE personalities SET ' . implode(', ', $fields) . ' WHERE id = :id';
|
||||
$stmt = $db->prepare($sql);
|
||||
|
||||
return $stmt->execute($params);
|
||||
}
|
||||
|
||||
public static function delete(int $id): bool
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare('DELETE FROM personalities WHERE id = :id');
|
||||
return $stmt->execute(['id' => $id]);
|
||||
}
|
||||
}
|
||||
78
app/Models/Session.php
Normal file
78
app/Models/Session.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Config\Database;
|
||||
|
||||
class Session
|
||||
{
|
||||
public static function findByUserId(int $userId): array
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare('SELECT * FROM sessions WHERE user_id = :user_id ORDER BY updated_at DESC');
|
||||
$stmt->execute(['user_id' => $userId]);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
public static function findById(int $id): ?array
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare('SELECT * FROM sessions WHERE id = :id');
|
||||
$stmt->execute(['id' => $id]);
|
||||
$result = $stmt->fetch();
|
||||
return $result ?: null;
|
||||
}
|
||||
|
||||
public static function create(array $data): array
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare('INSERT INTO sessions (user_id, name, provider, model, system_prompt, personality_id, thinking_mode) VALUES (:user_id, :name, :provider, :model, :system_prompt, :personality_id, :thinking_mode)');
|
||||
$stmt->execute([
|
||||
'user_id' => $data['user_id'],
|
||||
'name' => $data['name'] ?? '新会话',
|
||||
'provider' => $data['provider'] ?? 'newapi',
|
||||
'model' => $data['model'] ?? 'gpt-3.5-turbo',
|
||||
'system_prompt' => $data['system_prompt'] ?? '',
|
||||
'personality_id' => $data['personality_id'] ?? null,
|
||||
'thinking_mode' => $data['thinking_mode'] ?? 0,
|
||||
]);
|
||||
|
||||
return self::findById((int) $db->lastInsertId());
|
||||
}
|
||||
|
||||
public static function update(int $id, array $data): bool
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
|
||||
$fields = [];
|
||||
$params = ['id' => $id];
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
$fields[] = "{$key} = :{$key}";
|
||||
$params[$key] = $value;
|
||||
}
|
||||
|
||||
$fields[] = 'updated_at = CURRENT_TIMESTAMP';
|
||||
|
||||
$sql = 'UPDATE sessions SET ' . implode(', ', $fields) . ' WHERE id = :id';
|
||||
$stmt = $db->prepare($sql);
|
||||
|
||||
return $stmt->execute($params);
|
||||
}
|
||||
|
||||
public static function delete(int $id): bool
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
|
||||
$db->beginTransaction();
|
||||
try {
|
||||
$db->prepare('DELETE FROM messages WHERE session_id = :session_id')->execute(['session_id' => $id]);
|
||||
$db->prepare('DELETE FROM sessions WHERE id = :id')->execute(['id' => $id]);
|
||||
$db->commit();
|
||||
return true;
|
||||
} catch (\Throwable $e) {
|
||||
$db->rollBack();
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
55
app/Models/User.php
Normal file
55
app/Models/User.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Config\Database;
|
||||
|
||||
class User
|
||||
{
|
||||
public static function findByUsername(string $username): ?array
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare('SELECT * FROM users WHERE username = :username');
|
||||
$stmt->execute(['username' => $username]);
|
||||
$result = $stmt->fetch();
|
||||
return $result ?: null;
|
||||
}
|
||||
|
||||
public static function findById(int $id): ?array
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare('SELECT * FROM users WHERE id = :id');
|
||||
$stmt->execute(['id' => $id]);
|
||||
$result = $stmt->fetch();
|
||||
return $result ?: null;
|
||||
}
|
||||
|
||||
public static function create(array $data): array
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare('INSERT INTO users (username, password_hash, role) VALUES (:username, :password_hash, :role)');
|
||||
$stmt->execute([
|
||||
'username' => $data['username'],
|
||||
'password_hash' => password_hash($data['password'], PASSWORD_DEFAULT),
|
||||
'role' => $data['role'] ?? 'user',
|
||||
]);
|
||||
|
||||
$user = self::findById((int) $db->lastInsertId());
|
||||
unset($user['password_hash']);
|
||||
return $user;
|
||||
}
|
||||
|
||||
public static function verifyPassword(string $username, string $password): bool
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare('SELECT password_hash FROM users WHERE username = :username');
|
||||
$stmt->execute(['username' => $username]);
|
||||
$result = $stmt->fetch();
|
||||
|
||||
if (!$result) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return password_verify($password, $result['password_hash']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user