// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\middleware; class Api{ public $data = ['code' => 1, 'data' => '', 'message' => '']; /** * 处理请求 * * @param \think\Request $request * @param \Closure $next * @return Response */ public function handle($request, \Closure $next){ $response = $next($request); if (is_array($response->getData())) { $this->data = array_merge($this->data, $response->getData()); } else { $this->data = $response->getData(); } if ($request->isAjax()) { return json($this->data); } else { if (\is_string($this->data) && $this->data != '') { return $response; } else { return json($this->data); } } } }