This commit is contained in:
杨志
2026-01-21 09:07:10 +08:00
parent e5396f5f83
commit 35f9f6f31f
5 changed files with 292 additions and 1 deletions

View File

@@ -6,6 +6,8 @@ namespace app\controller;
use app\BaseController;
use app\middleware\Auth;
use app\service\CrawlerService;
use app\service\UserConfigService;
use think\facade\Session;
use think\facade\View;
/**
@@ -19,8 +21,77 @@ class Crawler extends BaseController
*/
public function index()
{
// 获取当前用户的配置
$username = Session::get('username', '');
$config = [];
if ($username) {
$configService = new UserConfigService();
$config = $configService->getUserConfig($username);
}
// 将配置传递给视图
View::assign('userConfig', $config);
return View::fetch();
}
/**
* 获取用户配置
*/
public function getUserConfig()
{
header('Content-Type: application/json; charset=utf-8');
$username = Session::get('username', '');
if (empty($username)) {
return json([
'code' => 0,
'msg' => '未登录',
]);
}
$configService = new UserConfigService();
$config = $configService->getUserConfig($username);
return json([
'code' => 1,
'data' => $config,
'msg' => '获取成功',
]);
}
/**
* 保存用户配置
*/
public function saveUserConfig()
{
header('Content-Type: application/json; charset=utf-8');
$username = Session::get('username', '');
if (empty($username)) {
return json([
'code' => 0,
'msg' => '未登录',
]);
}
$config = [
'jsessionid1' => $this->request->param('jsessionid1', ''),
'jsessionid2' => $this->request->param('jsessionid2', ''),
'serverid' => $this->request->param('serverid', ''),
'examid' => $this->request->param('examid', ''),
'bmid' => $this->request->param('bmid', ''),
'userid' => $this->request->param('userid', ''),
];
$configService = new UserConfigService();
$result = $configService->saveUserConfig($username, $config);
return json($result);
}
/**
* 获取地区选项从网页HTML中提取

25
app/model/UserConfig.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
declare (strict_types = 1);
namespace app\model;
use think\Model;
/**
* 用户配置模型
*/
class UserConfig extends Model
{
// 设置数据库连接
protected $connection = 'sqlite';
// 设置表名
protected $name = 'user_configs';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
// 定义时间戳字段名
protected $createTime = 'created_at';
protected $updateTime = 'updated_at';
}

View File

@@ -0,0 +1,128 @@
<?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()];
}
}
}