response类调整 data type 可以单独设置

This commit is contained in:
thinkphp
2015-12-22 23:40:45 +08:00
parent daf7d3adbe
commit 633a086950
4 changed files with 77 additions and 22 deletions

View File

@@ -13,8 +13,8 @@ return [
'default_return_type' => 'html', 'default_return_type' => 'html',
// 默认语言 // 默认语言
'default_lang' => 'zh-cn', 'default_lang' => 'zh-cn',
// response输出方式 0 echo 1 return 2 exit // response是否返回方式
'response_type' => 0, 'response_return' => false,
// 默认AJAX 数据返回格式,可选JSON XML ... // 默认AJAX 数据返回格式,可选JSON XML ...
'default_ajax_return' => 'JSON', 'default_ajax_return' => 'JSON',
// 默认JSONP格式返回的处理方法 // 默认JSONP格式返回的处理方法

View File

@@ -111,7 +111,7 @@ class App
// 操作方法执行完成监听 // 操作方法执行完成监听
APP_HOOK && Hook::listen('action_end', $data); 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 { } else {
// 操作方法不是Public 抛出异常 // 操作方法不是Public 抛出异常
throw new \ReflectionException(); throw new \ReflectionException();
@@ -124,7 +124,7 @@ class App
// 操作方法执行完成监听 // 操作方法执行完成监听
APP_HOOK && Hook::listen('action_end', $data); 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 { } else {
throw new Exception('method [ ' . (new \ReflectionClass($instance))->getName() . '->' . $action . ' ] not exists ', 10002); throw new Exception('method [ ' . (new \ReflectionClass($instance))->getName() . '->' . $action . ' ] not exists ', 10002);
} }

View File

@@ -93,7 +93,10 @@ abstract class Rest
protected function response($data, $type = '', $code = 200) protected function response($data, $type = '', $code = 200)
{ {
Response::sendHttpStatus($code); Response::sendHttpStatus($code);
return Response::returnData($data, $type, 1); Response::data($data);
if ($type) {
Response::type($type);
}
} }
/** /**

View File

@@ -15,17 +15,25 @@ class Response
{ {
// 输出数据的转换方法 // 输出数据的转换方法
protected static $tramsform = null; protected static $tramsform = null;
// 输出数据的类型
protected static $type = 'json';
// 输出数据
protected static $data = '';
// 是否exit
protected static $isExit = false;
/** /**
* 返回数据到客户端 * 发送数据到客户端
* @access protected * @access protected
* @param mixed $data 要返回的数据 * @param mixed $data 要返回的数据
* @param String $type 返回数据格式 * @param String $type 返回数据格式
* @param integer $return 是否返回数据 0 echo 1 return 2 exit * @param bool $return 是否返回数据
* @return void * @return void
*/ */
public static function returnData($data, $type = '', $return = 0) public static function send($data = '', $type = '', $return = false)
{ {
$type = strtolower($type ?: self::$type);
$headers = [ $headers = [
'json' => 'application/json', 'json' => 'application/json',
'xml' => 'text/xml', 'xml' => 'text/xml',
@@ -34,11 +42,12 @@ class Response
'script' => 'application/javascript', 'script' => 'application/javascript',
'text' => 'text/plain', 'text' => 'text/plain',
]; ];
$type = strtolower($type);
if (!headers_sent() && isset($headers[$type])) { if (!headers_sent() && isset($headers[$type])) {
header('Content-Type:' . $headers[$type] . '; charset=utf-8'); header('Content-Type:' . $headers[$type] . '; charset=utf-8');
} }
$data = $data ?: self::$data;
if (is_callable(self::$tramsform)) { if (is_callable(self::$tramsform)) {
$data = call_user_func_array(self::$tramsform, [$data]); $data = call_user_func_array(self::$tramsform, [$data]);
} else { } else {
@@ -53,21 +62,21 @@ class Response
$data = $handler . '(' . json_encode($data, JSON_UNESCAPED_UNICODE) . ');'; $data = $handler . '(' . json_encode($data, JSON_UNESCAPED_UNICODE) . ');';
break; break;
case '': case '':
break // 类型为空不做处理
break;
default: default:
// 用于扩展其他返回格式数据 // 用于扩展其他返回格式数据
Hook::listen('return_data', $data); Hook::listen('return_data', $data);
} }
} }
switch ($return) { if ($return) {
case 1: return $data;
return $data; } else {
case 2: echo $data;
exit($data); }
case 0: if (self::$isExit) {
default: exit;
echo $data;
} }
} }
@@ -82,6 +91,39 @@ class Response
self::$tramsform = $callback; 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数据到客户端 * 返回封装后的API数据到客户端
* @access public * @access public
@@ -99,7 +141,11 @@ class Response
'time' => NOW_TIME, 'time' => NOW_TIME,
'data' => $data, '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(); $view = new \think\View();
$result = $view->fetch(Config::get('dispatch_jump_tmpl'), $result); $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(); $view = new \think\View();
$result = $view->fetch(Config::get('dispatch_jump_tmpl'), $result); $result = $view->fetch(Config::get('dispatch_jump_tmpl'), $result);
} }
return self::returnData($result, $type, true); self::$data = $result;
if ($type) {
self::$type = $type;
}
} }
/** /**