From 633a086950e82f82da73a62ce60b4ff93e3a5e02 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 22 Dec 2015 23:40:45 +0800 Subject: [PATCH] =?UTF-8?q?response=E7=B1=BB=E8=B0=83=E6=95=B4=20data=20ty?= =?UTF-8?q?pe=20=E5=8F=AF=E4=BB=A5=E5=8D=95=E7=8B=AC=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- convention.php | 4 +- library/think/app.php | 6 +-- library/think/controller/rest.php | 5 +- library/think/response.php | 84 +++++++++++++++++++++++++------ 4 files changed, 77 insertions(+), 22 deletions(-) diff --git a/convention.php b/convention.php index dcf4a58e..977b2ce7 100644 --- a/convention.php +++ b/convention.php @@ -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格式返回的处理方法 diff --git a/library/think/app.php b/library/think/app.php index 82689ffb..e403b31e 100644 --- a/library/think/app.php +++ b/library/think/app.php @@ -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); } diff --git a/library/think/controller/rest.php b/library/think/controller/rest.php index 380606e0..aa09b34b 100644 --- a/library/think/controller/rest.php +++ b/library/think/controller/rest.php @@ -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); + } } /** diff --git a/library/think/response.php b/library/think/response.php index e54407fb..1eed337f 100644 --- a/library/think/response.php +++ b/library/think/response.php @@ -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; + } } /**