UP
This commit is contained in:
@@ -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
25
app/model/UserConfig.php
Normal 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';
|
||||
}
|
||||
128
app/service/UserConfigService.php
Normal file
128
app/service/UserConfigService.php
Normal 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()];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,8 @@ Route::get('auth/logout', 'auth/logout');
|
||||
|
||||
// 爬虫工具路由(需要登录)
|
||||
Route::get('crawler', 'crawler/index');
|
||||
Route::get('crawler/getUserConfig', 'crawler/getUserConfig');
|
||||
Route::post('crawler/saveUserConfig', 'crawler/saveUserConfig');
|
||||
Route::post('crawler/getDsdmOptions', 'crawler/getDsdmOptions');
|
||||
Route::post('crawler/getZwdmList', 'crawler/getZwdmList');
|
||||
Route::post('crawler/getPositionInfo', 'crawler/getPositionInfo');
|
||||
|
||||
@@ -280,8 +280,10 @@
|
||||
</div>
|
||||
|
||||
<div class="action-buttons">
|
||||
<button class="btn btn-secondary" onclick="getDsdmOptions()">获取地区选项</button>
|
||||
<button class="btn btn-secondary" onclick="saveUserConfig()">保存配置</button>
|
||||
<button class="btn" onclick="getDsdmOptions()">获取地区选项</button>
|
||||
</div>
|
||||
<div id="config-message"></div>
|
||||
</div>
|
||||
|
||||
<!-- 第二步:选择地区并自动抓取 -->
|
||||
@@ -335,6 +337,69 @@
|
||||
let lastAa = '';
|
||||
let lastResults = [];
|
||||
let isCrawling = false; // 爬取状态标志
|
||||
|
||||
// 页面加载时自动加载用户配置
|
||||
window.onload = function() {
|
||||
loadUserConfig();
|
||||
};
|
||||
|
||||
// 加载用户配置
|
||||
function loadUserConfig() {
|
||||
fetch(API_BASE_URL + '/crawler/getUserConfig', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
}
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.code === 1 && data.data) {
|
||||
// 填充表单
|
||||
document.getElementById('cookie-jsessionid2').value = data.data.jsessionid1 || '';
|
||||
document.getElementById('cookie-jsessionid').value = data.data.jsessionid2 || '';
|
||||
document.getElementById('cookie-serverid').value = data.data.serverid || '';
|
||||
document.getElementById('examid').value = data.data.examid || '';
|
||||
document.getElementById('bmid').value = data.data.bmid || '';
|
||||
document.getElementById('userid').value = data.data.userid || '';
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('加载配置失败:', error);
|
||||
});
|
||||
}
|
||||
|
||||
// 保存用户配置
|
||||
function saveUserConfig() {
|
||||
const config = {
|
||||
jsessionid1: document.getElementById('cookie-jsessionid2').value.trim(),
|
||||
jsessionid2: document.getElementById('cookie-jsessionid').value.trim(),
|
||||
serverid: document.getElementById('cookie-serverid').value.trim(),
|
||||
examid: document.getElementById('examid').value.trim(),
|
||||
bmid: document.getElementById('bmid').value.trim(),
|
||||
userid: document.getElementById('userid').value.trim(),
|
||||
};
|
||||
|
||||
fetch(API_BASE_URL + '/crawler/saveUserConfig', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
},
|
||||
body: `jsessionid1=${encodeURIComponent(config.jsessionid1)}&jsessionid2=${encodeURIComponent(config.jsessionid2)}&serverid=${encodeURIComponent(config.serverid)}&examid=${encodeURIComponent(config.examid)}&bmid=${encodeURIComponent(config.bmid)}&userid=${encodeURIComponent(config.userid)}`
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.code === 1) {
|
||||
showMessage('config-message', data.msg || '保存成功', 'success');
|
||||
} else {
|
||||
showMessage('config-message', data.msg || '保存失败', 'error');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
showMessage('config-message', '请求失败: ' + error.message, 'error');
|
||||
});
|
||||
}
|
||||
|
||||
// 获取地区选项
|
||||
function getDsdmOptions() {
|
||||
|
||||
Reference in New Issue
Block a user