This commit is contained in:
杨志
2026-01-21 09:18:19 +08:00
parent a562c95b10
commit eed3540f3d
3 changed files with 35 additions and 12 deletions

View File

@@ -16,6 +16,26 @@ use think\facade\View;
class Crawler extends BaseController
{
protected $middleware = [Auth::class];
/**
* 初始化方法
*/
protected function initialize()
{
parent::initialize();
// 对于API方法直接设置响应头
$action = $this->request->action(true);
$apiActions = ['getUserConfig', 'saveUserConfig', 'getDsdmOptions', 'getZwdmList', 'getPositionInfo', 'batchGetPositionInfo', 'fetchAllPositions'];
if (in_array($action, $apiActions)) {
// 设置JSON响应头并禁用视图渲染
header('Content-Type: application/json; charset=utf-8');
// 禁用视图自动输出
$this->app->view->config('auto_render', false);
}
}
/**
* 显示爬虫工具首页
*/
@@ -41,9 +61,6 @@ class Crawler extends BaseController
*/
public function getUserConfig()
{
// 强制返回JSON设置响应头
header('Content-Type: application/json; charset=utf-8');
try {
$username = Session::get('username', '');
@@ -75,9 +92,6 @@ class Crawler extends BaseController
*/
public function saveUserConfig()
{
// 强制返回JSON设置响应头
header('Content-Type: application/json; charset=utf-8');
try {
$username = Session::get('username', '');