diff --git a/app/controller/Crawler.php b/app/controller/Crawler.php index 52c5bc3..4c7d2a1 100644 --- a/app/controller/Crawler.php +++ b/app/controller/Crawler.php @@ -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中提取) diff --git a/app/model/UserConfig.php b/app/model/UserConfig.php new file mode 100644 index 0000000..7837f26 --- /dev/null +++ b/app/model/UserConfig.php @@ -0,0 +1,25 @@ +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()]; + } + } +} diff --git a/route/app.php b/route/app.php index 949b56f..694812e 100644 --- a/route/app.php +++ b/route/app.php @@ -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'); diff --git a/view/crawler/index.html b/view/crawler/index.html index 2cd4fda..e6fb1c1 100644 --- a/view/crawler/index.html +++ b/view/crawler/index.html @@ -280,8 +280,10 @@
- + +
+
@@ -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() {