mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 20:52:48 +08:00
response类调整 data type 可以单独设置
This commit is contained in:
@@ -13,8 +13,8 @@ return [
|
||||
'default_return_type' => 'html',
|
||||
// 默认语言
|
||||
'default_lang' => 'zh-cn',
|
||||
// response输出方式 0 echo 1 return 2 exit
|
||||
'response_type' => 0,
|
||||
// response是否返回方式
|
||||
'response_return' => false,
|
||||
// 默认AJAX 数据返回格式,可选JSON XML ...
|
||||
'default_ajax_return' => 'JSON',
|
||||
// 默认JSONP格式返回的处理方法
|
||||
|
||||
@@ -34,7 +34,7 @@ class App
|
||||
Config::load($file, $file);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 获取配置参数
|
||||
$config = Config::get();
|
||||
|
||||
@@ -111,7 +111,7 @@ class App
|
||||
// 操作方法执行完成监听
|
||||
APP_HOOK && Hook::listen('action_end', $data);
|
||||
// 输出数据
|
||||
return Response::returnData($data, Config::get('default_return_type'), Config::get('response_type'));
|
||||
return Response::send($data, '', Config::get('response_return'));
|
||||
} else {
|
||||
// 操作方法不是Public 抛出异常
|
||||
throw new \ReflectionException();
|
||||
@@ -124,7 +124,7 @@ class App
|
||||
// 操作方法执行完成监听
|
||||
APP_HOOK && Hook::listen('action_end', $data);
|
||||
// 输出数据
|
||||
return Response::returnData($data, Config::get('default_return_type'), Config::get('response_type'));
|
||||
return Response::send($data, '', Config::get('response_return'));
|
||||
} else {
|
||||
throw new Exception('method [ ' . (new \ReflectionClass($instance))->getName() . '->' . $action . ' ] not exists ', 10002);
|
||||
}
|
||||
|
||||
@@ -93,7 +93,10 @@ abstract class Rest
|
||||
protected function response($data, $type = '', $code = 200)
|
||||
{
|
||||
Response::sendHttpStatus($code);
|
||||
return Response::returnData($data, $type, 1);
|
||||
Response::data($data);
|
||||
if ($type) {
|
||||
Response::type($type);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -15,17 +15,25 @@ class Response
|
||||
{
|
||||
// 输出数据的转换方法
|
||||
protected static $tramsform = null;
|
||||
// 输出数据的类型
|
||||
protected static $type = 'json';
|
||||
// 输出数据
|
||||
protected static $data = '';
|
||||
// 是否exit
|
||||
protected static $isExit = false;
|
||||
|
||||
/**
|
||||
* 返回数据到客户端
|
||||
* 发送数据到客户端
|
||||
* @access protected
|
||||
* @param mixed $data 要返回的数据
|
||||
* @param String $type 返回数据格式
|
||||
* @param integer $return 是否返回数据 0 echo 1 return 2 exit
|
||||
* @param bool $return 是否返回数据
|
||||
* @return void
|
||||
*/
|
||||
public static function returnData($data, $type = '', $return = 0)
|
||||
public static function send($data = '', $type = '', $return = false)
|
||||
{
|
||||
$type = strtolower($type ?: self::$type);
|
||||
|
||||
$headers = [
|
||||
'json' => 'application/json',
|
||||
'xml' => 'text/xml',
|
||||
@@ -34,11 +42,12 @@ class Response
|
||||
'script' => 'application/javascript',
|
||||
'text' => 'text/plain',
|
||||
];
|
||||
$type = strtolower($type);
|
||||
|
||||
if (!headers_sent() && isset($headers[$type])) {
|
||||
header('Content-Type:' . $headers[$type] . '; charset=utf-8');
|
||||
}
|
||||
|
||||
$data = $data ?: self::$data;
|
||||
if (is_callable(self::$tramsform)) {
|
||||
$data = call_user_func_array(self::$tramsform, [$data]);
|
||||
} else {
|
||||
@@ -53,21 +62,21 @@ class Response
|
||||
$data = $handler . '(' . json_encode($data, JSON_UNESCAPED_UNICODE) . ');';
|
||||
break;
|
||||
case '':
|
||||
break
|
||||
// 类型为空不做处理
|
||||
break;
|
||||
default:
|
||||
// 用于扩展其他返回格式数据
|
||||
Hook::listen('return_data', $data);
|
||||
}
|
||||
}
|
||||
|
||||
switch ($return) {
|
||||
case 1:
|
||||
return $data;
|
||||
case 2:
|
||||
exit($data);
|
||||
case 0:
|
||||
default:
|
||||
echo $data;
|
||||
if ($return) {
|
||||
return $data;
|
||||
} else {
|
||||
echo $data;
|
||||
}
|
||||
if (self::$isExit) {
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,6 +91,39 @@ class Response
|
||||
self::$tramsform = $callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出类型设置
|
||||
* @access public
|
||||
* @param string $type 输出内容的格式类型
|
||||
* @return void
|
||||
*/
|
||||
public static function type($type)
|
||||
{
|
||||
self::$type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出数据设置
|
||||
* @access public
|
||||
* @param mixed $data 输出数据
|
||||
* @return void
|
||||
*/
|
||||
public static function data($data)
|
||||
{
|
||||
self::$data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出是否exit设置
|
||||
* @access public
|
||||
* @param bool $exit 是否退出
|
||||
* @return void
|
||||
*/
|
||||
public static function isExit($exit = false)
|
||||
{
|
||||
self::$isExit = $exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回封装后的API数据到客户端
|
||||
* @access public
|
||||
@@ -99,7 +141,11 @@ class Response
|
||||
'time' => NOW_TIME,
|
||||
'data' => $data,
|
||||
];
|
||||
return self::returnData($result, $type, true);
|
||||
|
||||
self::$data = $result;
|
||||
if ($type) {
|
||||
self::$type = $type;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,7 +171,10 @@ class Response
|
||||
$view = new \think\View();
|
||||
$result = $view->fetch(Config::get('dispatch_jump_tmpl'), $result);
|
||||
}
|
||||
return self::returnData($result, $type, true);
|
||||
self::$data = $result;
|
||||
if ($type) {
|
||||
self::$type = $type;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -151,7 +200,10 @@ class Response
|
||||
$view = new \think\View();
|
||||
$result = $view->fetch(Config::get('dispatch_jump_tmpl'), $result);
|
||||
}
|
||||
return self::returnData($result, $type, true);
|
||||
self::$data = $result;
|
||||
if ($type) {
|
||||
self::$type = $type;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user