refactor: 优化后台兼容接口请求的判断

This commit is contained in:
augushong
2025-03-10 18:17:28 +08:00
parent 7f92b0e07a
commit e33863af46
3 changed files with 20 additions and 21 deletions

View File

@@ -44,7 +44,7 @@ class AdminBase extends AdminController
*/ */
public function index() public function index()
{ {
if ($this->request->isJson()) { if ($this->request->isAjax()) {
if (input('selectFields')) { if (input('selectFields')) {
return $this->selectList(); return $this->selectList();
} }

View File

@@ -222,7 +222,7 @@ class AdminControllerBase extends BaseController
public function checkParseApi() public function checkParseApi()
{ {
if (Config::get('app.auto_parse_api')) { if (Config::get('app.auto_parse_api')) {
if ($this->request->isJsonPure() && $this->request->param('get_page_data')) { if ($this->request->isJson() && $this->request->param('get_page_data')) {
return true; return true;
} }
} }

View File

@@ -10,35 +10,34 @@ class RequestBase extends \think\Request
protected $filter = []; protected $filter = [];
/** /**
* 当前是否JSON请求 * 当前是否Ajax请求
* @access public
* @param bool $ajax true 获取原始ajax请求
* @return bool * @return bool
*/ */
public function isJson(): bool public function isAjax(bool $ajax = false): bool
{ {
$acceptType = $this->type(); $value = $this->server('HTTP_X_REQUESTED_WITH');
$result = $value && 'xmlhttprequest' == strtolower($value) ? true : false;
$is_json = str_contains($acceptType, 'json'); if (true === $ajax) {
if (!$is_json) { return $result;
return false;
} }
$is_ajax = $this->param($this->varAjax) ? true : $result;
if (!Config::get('app.auto_parse_api')) { if (!Config::get('app.auto_parse_api')) {
return $is_ajax;
}
if($this->isJson()){
if ($this->has('get_page_data')) {
return false;
}
return true; return true;
} }
if ($this->has('get_page_data')) {
return false;
}
return true; return $is_ajax;
} }
/**
* 仅通过实际请求参数判断请求
*
* @return bool
*/
public function isJsonPure()
{
return parent::isJson();
}
} }