44 lines
1.1 KiB
PHP
44 lines
1.1 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
|
// +----------------------------------------------------------------------
|
|
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
|
|
// +----------------------------------------------------------------------
|
|
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
|
// +----------------------------------------------------------------------
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|