129 lines
4.0 KiB
PHP
129 lines
4.0 KiB
PHP
<?php
|
|
declare (strict_types = 1);
|
|
|
|
namespace app\service;
|
|
|
|
use app\model\UserConfig;
|
|
use think\facade\Db;
|
|
|
|
/**
|
|
* 用户配置服务类
|
|
*/
|
|
class UserConfigService
|
|
{
|
|
/**
|
|
* 初始化数据库表
|
|
*/
|
|
private function initDatabase(): void
|
|
{
|
|
$dbPath = dirname(dirname(__DIR__)) . '/runtime/database.db';
|
|
$dbDir = dirname($dbPath);
|
|
|
|
if (!is_dir($dbDir)) {
|
|
mkdir($dbDir, 0755, true);
|
|
}
|
|
|
|
try {
|
|
$connection = Db::connect('sqlite');
|
|
|
|
// 创建用户配置表(如果不存在)
|
|
$sql = "CREATE TABLE IF NOT EXISTS user_configs (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
username VARCHAR(50) NOT NULL,
|
|
jsessionid1 VARCHAR(255),
|
|
jsessionid2 VARCHAR(255),
|
|
serverid VARCHAR(255),
|
|
examid VARCHAR(255),
|
|
bmid VARCHAR(255),
|
|
userid VARCHAR(255),
|
|
created_at DATETIME NOT NULL,
|
|
updated_at DATETIME NOT NULL,
|
|
UNIQUE(username)
|
|
)";
|
|
|
|
$connection->execute($sql);
|
|
} catch (\Exception $e) {
|
|
// 忽略错误
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取用户配置
|
|
* @param string $username
|
|
* @return array
|
|
*/
|
|
public function getUserConfig(string $username): array
|
|
{
|
|
$this->initDatabase();
|
|
|
|
try {
|
|
$config = UserConfig::where('username', $username)->find();
|
|
|
|
if ($config) {
|
|
return [
|
|
'jsessionid1' => $config->jsessionid1 ?? '',
|
|
'jsessionid2' => $config->jsessionid2 ?? '',
|
|
'serverid' => $config->serverid ?? '',
|
|
'examid' => $config->examid ?? '',
|
|
'bmid' => $config->bmid ?? '',
|
|
'userid' => $config->userid ?? '',
|
|
];
|
|
}
|
|
} catch (\Exception $e) {
|
|
// 忽略错误
|
|
}
|
|
|
|
return [
|
|
'jsessionid1' => '',
|
|
'jsessionid2' => '',
|
|
'serverid' => '',
|
|
'examid' => '',
|
|
'bmid' => '',
|
|
'userid' => '',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 保存用户配置
|
|
* @param string $username
|
|
* @param array $config
|
|
* @return array ['code' => 1|0, 'msg' => string]
|
|
*/
|
|
public function saveUserConfig(string $username, array $config): array
|
|
{
|
|
$this->initDatabase();
|
|
|
|
try {
|
|
$userConfig = UserConfig::where('username', $username)->find();
|
|
|
|
if ($userConfig) {
|
|
// 更新现有配置
|
|
$userConfig->jsessionid1 = $config['jsessionid1'] ?? '';
|
|
$userConfig->jsessionid2 = $config['jsessionid2'] ?? '';
|
|
$userConfig->serverid = $config['serverid'] ?? '';
|
|
$userConfig->examid = $config['examid'] ?? '';
|
|
$userConfig->bmid = $config['bmid'] ?? '';
|
|
$userConfig->userid = $config['userid'] ?? '';
|
|
$userConfig->save();
|
|
} else {
|
|
// 创建新配置
|
|
$userConfig = new UserConfig();
|
|
$userConfig->username = $username;
|
|
$userConfig->jsessionid1 = $config['jsessionid1'] ?? '';
|
|
$userConfig->jsessionid2 = $config['jsessionid2'] ?? '';
|
|
$userConfig->serverid = $config['serverid'] ?? '';
|
|
$userConfig->examid = $config['examid'] ?? '';
|
|
$userConfig->bmid = $config['bmid'] ?? '';
|
|
$userConfig->userid = $config['userid'] ?? '';
|
|
$userConfig->created_at = date('Y-m-d H:i:s');
|
|
$userConfig->updated_at = date('Y-m-d H:i:s');
|
|
$userConfig->save();
|
|
}
|
|
|
|
return ['code' => 1, 'msg' => '保存成功'];
|
|
} catch (\Exception $e) {
|
|
return ['code' => 0, 'msg' => '保存失败: ' . $e->getMessage()];
|
|
}
|
|
}
|
|
}
|