新增爬虫规则创建

新增爬虫规则 时可指定默认的规则内容
This commit is contained in:
779776787
2025-08-12 16:02:46 +08:00
parent efca70ecb2
commit 54a913b599
3 changed files with 207 additions and 21 deletions
+63
View File
@@ -194,4 +194,67 @@ class ProxyController extends BaseController {
}
}
}
/**
* 创建规则文件并填充默认内容
* 访问URL: index.php/Proxy/createRuleFile (POST请求)
*/
public function createRuleFileAction() {
header('Content-Type: application/json');
$relativePath = $_POST['relativePath'] ?? null;
$apiName = $_POST['apiName'] ?? null;
$customContent = $_POST['customContent'] ?? null;
$saveAsDefault = !empty($_POST['saveAsDefault']) && $_POST['saveAsDefault'] !== 'false';
if (!$relativePath || !$apiName) {
$this->ajaxReturn(['success' => false, 'message' => '缺少必要的参数']);
}
$targetPath = $this->baseSaveDir . sanitize_path($relativePath);
$targetDir = dirname($targetPath);
if (file_exists($targetPath)) {
// $this->ajaxReturn(['success' => false, 'message' => '文件已存在,无法创建']);
}
if (!is_dir($targetDir)) {
if (!mkdir($targetDir, 0755, true)) {
$this->ajaxReturn(['success' => false, 'message' => '创建目录失败,请检查 /box 目录权限']);
}
}
$finalContent = '';
if (!empty($customContent)) {
$finalContent = $customContent;
} else {
$templatePath = ROOT_PATH . '/Json/' . $apiName . '.json';
if (file_exists($templatePath)) {
$finalContent = file_get_contents($templatePath);
} else {
$this->ajaxReturn([
'success' => false,
'message' => "默认模板 Json/{$apiName}.json 未找到。请点击“内容”按钮设置默认内容,或在服务器Json目录下手动创建该文件。"
]);
return;
}
}
if (file_put_contents($targetPath, $finalContent) === false) {
$this->ajaxReturn(['success' => false, 'message' => '规则文件写入失败']);
return;
}
if ($saveAsDefault && !empty($customContent)) {
$defaultTemplatePath = ROOT_PATH . '/Json/' . $apiName . '.json';
$defaultTemplateDir = dirname($defaultTemplatePath);
if (!is_dir($defaultTemplateDir)) {
mkdir($defaultTemplateDir, 0755, true);
}
file_put_contents($defaultTemplatePath, $customContent);
}
$this->ajaxReturn(['success' => true, 'message' => '规则文件创建成功']);
}
}
+35 -14
View File
@@ -70,21 +70,42 @@
<script id="add-site-modal-template" type="text/x-handlebars-template">
<div id="create-spider-form-modal" class="details-panel create-panel active" style="max-height:none; opacity:1; padding:0; background:none;">
<div class="form-group"><label for="new-site-name-modal">规则名称</label><input id="new-site-name-modal" type="text" placeholder="例如:酷云影视"></div>
<div class="form-group"><label for="new-site-key-modal">唯一标识</label><input id="new-site-key-modal" type="text" placeholder="例如:ky_m"></div>
<div class="form-group" style="grid-column: 1 / -1;"><label for="new-site-ext-modal">规则链接</label><input id="new-site-ext-modal" type="text" placeholder="http://.../rule.json"></div>
<div class="form-group"><label for="new-site-api-modal">爬虫接口</label><input id="new-site-api-modal" type="text" value="csp_XYQHiker"></div>
<div class="form-group"><label for="new-site-type-modal">类型</label><select id="new-site-type-modal"><option value="1">1 (csp)</option><option value="0">0 (vod)</option><option value="2">2</option><option value="3" selected>3</option></select></div>
<div class="form-group"><label for="new-site-jar-modal">Jar文件</label><input id="new-site-jar-modal" type="text" placeholder="例如:./libs/Panda.jar"></div>
<div class="form-group checkbox-group">
<input type="checkbox" id="new-site-searchable-modal" style="width: auto;" checked>
<label>可搜索</label>
<input type="checkbox" id="new-site-filterable-modal" style="width: auto;" checked>
<label>可筛选</label>
<input type="checkbox" id="new-site-quick-modal" style="width: auto;" checked>
<label>快速搜索</label>
</div>
<div class="details-form-grid">
<div class="form-group"><label for="new-site-name-modal">规则名称</label><input id="new-site-name-modal" type="text" placeholder="例如:酷云影视"></div>
<div class="form-group"><label for="new-site-key-modal">唯一标识</label><input id="new-site-key-modal" type="text" placeholder="例如:ky_m"></div>
<div class="form-group" style="grid-column: 1 / -1;">
<label for="new-site-ext-modal">规则链接</label>
<div class="input-with-buttons">
<input id="new-site-ext-modal" type="text" placeholder="./some/path/rule.json">
<button type="button" id="toggle-custom-content-btn" class="btn btn-sm secondary-btn">内容</button>
</div>
</div>
<div id="custom-content-wrapper" style="display: none; grid-column: 1 / -1;">
<div class="form-group">
<label for="new-site-custom-content-modal">自定义规则内容 (留空则使用默认模板)</label>
<textarea id="new-site-custom-content-modal" rows="5"></textarea>
</div>
<div class="form-group checkbox-group">
<input type="checkbox" id="save-as-default-toggle-modal" style="width: auto;">
<label for="save-as-default-toggle-modal">将以上内容保存为该接口的默认模板</label>
</div>
</div>
<div class="form-group"><label for="new-site-api-modal">爬虫接口</label><input id="new-site-api-modal" type="text" value="csp_XYQHiker"></div>
<div class="form-group"><label for="new-site-type-modal">类型</label><select id="new-site-type-modal"><option value="1">1 (csp)</option><option value="0">0 (vod)</option><option value="2">2</option><option value="3" selected>3</option></select></div>
<div class="form-group"><label for="new-site-jar-modal">Jar文件</label><input id="new-site-jar-modal" type="text" placeholder="例如:./libs/Panda.jar"></div>
<div class="form-group checkbox-group">
<input type="checkbox" id="new-site-searchable-modal" style="width: auto;" checked>
<label>可搜索</label>
<input type="checkbox" id="new-site-filterable-modal" style="width: auto;" checked>
<label>可筛选</label>
<input type="checkbox" id="new-site-quick-modal" style="width: auto;" checked>
<label>快速搜索</label>
</div>
</div>
</div>
</script>