up
This commit is contained in:
@@ -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', '');
|
||||
|
||||
|
||||
@@ -20,16 +20,24 @@ class Auth
|
||||
// 检查是否已登录
|
||||
$username = session('username');
|
||||
|
||||
// 获取路径信息
|
||||
// 获取路径信息和方法名
|
||||
$pathinfo = $request->pathinfo();
|
||||
$controller = $request->controller(true);
|
||||
$action = $request->action(true);
|
||||
|
||||
// 判断是否为API请求(非index页面)
|
||||
$isApiRequest = $pathinfo !== 'crawler' && strpos($pathinfo, 'crawler/') === 0;
|
||||
// 判断是否为API请求
|
||||
// 1. 路径包含 /crawler/ 且不是 /crawler 本身
|
||||
// 2. 或者是 crawler 控制器的非 index 方法
|
||||
// 3. 或者是AJAX请求
|
||||
$isApiRequest = ($pathinfo !== 'crawler' && strpos($pathinfo, 'crawler/') === 0) ||
|
||||
($controller === 'crawler' && $action !== 'index') ||
|
||||
$request->isAjax() ||
|
||||
$request->header('X-Requested-With') === 'XMLHttpRequest';
|
||||
|
||||
if (empty($username)) {
|
||||
// 如果是API请求,返回JSON
|
||||
if ($isApiRequest || $request->isAjax()) {
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
if ($isApiRequest) {
|
||||
// 直接返回JSON,ThinkPHP的json()函数会自动设置响应头
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '请先登录',
|
||||
|
||||
@@ -22,7 +22,7 @@ Route::post('auth/doLogin', 'auth/doLogin');
|
||||
Route::get('auth/logout', 'auth/logout');
|
||||
|
||||
// 爬虫工具路由(需要登录)
|
||||
Route::get('crawler', 'crawler/index');
|
||||
// 注意:API路由必须在index路由之前定义,确保优先匹配
|
||||
Route::get('crawler/getUserConfig', 'crawler/getUserConfig');
|
||||
Route::post('crawler/saveUserConfig', 'crawler/saveUserConfig');
|
||||
Route::post('crawler/getDsdmOptions', 'crawler/getDsdmOptions');
|
||||
@@ -30,6 +30,7 @@ Route::post('crawler/getZwdmList', 'crawler/getZwdmList');
|
||||
Route::post('crawler/getPositionInfo', 'crawler/getPositionInfo');
|
||||
Route::post('crawler/batchGetPositionInfo', 'crawler/batchGetPositionInfo');
|
||||
Route::post('crawler/fetchAllPositions', 'crawler/fetchAllPositions');
|
||||
Route::get('crawler', 'crawler/index');
|
||||
|
||||
// 管理员路由(需要登录且为管理员)
|
||||
// 注意:API路由必须在index路由之前定义,确保优先匹配
|
||||
|
||||
Reference in New Issue
Block a user