mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
修正Query类 改进Response类 改进异常输出
This commit is contained in:
@@ -347,7 +347,7 @@ function trace($log = '[think]', $level = 'log')
|
||||
function view($template = '', $vars = [], $code = 200)
|
||||
{
|
||||
$response = new \think\response\Html();
|
||||
return $response->data($template)->vars($vars)->code($code);
|
||||
return $response->data($template)->render(true)->vars($vars)->code($code);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -86,12 +86,13 @@ class Controller
|
||||
* @access public
|
||||
* @param string $template 模板文件名
|
||||
* @param array $vars 模板输出变量
|
||||
* @param array $replace 模板替换
|
||||
* @param array $config 模板参数
|
||||
* @return mixed
|
||||
*/
|
||||
public function fetch($template = '', $vars = [], $config = [])
|
||||
public function fetch($template = '', $vars = [], $replace = [], $config = [])
|
||||
{
|
||||
return $this->view->fetch($template, $vars, $config);
|
||||
return $this->view->fetch($template, $vars, $replace, $config = []);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,8 +21,10 @@ class Response
|
||||
protected $data;
|
||||
// 是否exit
|
||||
protected $isExit = false;
|
||||
// contentType
|
||||
protected $contentType = [
|
||||
// 当前的contentType
|
||||
protected $contentType;
|
||||
// 可用的输出类型
|
||||
protected static $contentTypes = [
|
||||
'json' => 'application/json',
|
||||
'xml' => 'text/xml',
|
||||
'html' => 'text/html',
|
||||
@@ -57,12 +59,36 @@ class Response
|
||||
{
|
||||
$type = strtolower($type ?: (IS_AJAX ? 'json' : 'html'));
|
||||
if (!isset(self::$instance[$type])) {
|
||||
self::$instance[$type] = new static($options);
|
||||
self::$instance[$type]->type($type, $options);
|
||||
|
||||
$class = '\\think\\response\\' . ucfirst($type);
|
||||
$response = class_exists($class) ? new $class($options) : new static($options);
|
||||
|
||||
if (isset(self::$contentTypes[$type])) {
|
||||
$response->contentType(self::$contentTypes[$type]);
|
||||
}
|
||||
|
||||
self::$instance[$type] = $response;
|
||||
}
|
||||
return self::$instance[$type];
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出类型设置
|
||||
* @access public
|
||||
* @param string $type 输出内容的格式类型
|
||||
* @param array $options 参数
|
||||
* @return $this
|
||||
*/
|
||||
public function type($type, $options = [])
|
||||
{
|
||||
$type = strtolower($type);
|
||||
if (isset(self::$instance[$type])) {
|
||||
return self::$instance[$type];
|
||||
} else {
|
||||
return self::create($type, $options);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送数据到客户端
|
||||
* @access public
|
||||
@@ -73,6 +99,10 @@ class Response
|
||||
{
|
||||
$data = $data ?: $this->data;
|
||||
|
||||
if (isset($this->contentType)) {
|
||||
$this->contentType($this->contentType);
|
||||
}
|
||||
|
||||
if (is_callable($this->transform)) {
|
||||
$data = call_user_func_array($this->transform, [$data]);
|
||||
}
|
||||
@@ -146,26 +176,6 @@ class Response
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出类型设置
|
||||
* @access public
|
||||
* @param string $type 输出内容的格式类型
|
||||
* @param array $options 参数
|
||||
* @return $this
|
||||
*/
|
||||
public function type($type, $options = [])
|
||||
{
|
||||
$type = strtolower($type);
|
||||
if (!isset(self::$instance[$type])) {
|
||||
$class = '\\think\\response\\' . ucfirst($type);
|
||||
self::$instance[$type] = class_exists($class) ? new $class($options) : $this;
|
||||
}
|
||||
if (isset($this->contentType[$type])) {
|
||||
self::$instance[$type]->contentType($this->contentType[$type]);
|
||||
}
|
||||
return self::$instance[$type];
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出是否exit设置
|
||||
* @access public
|
||||
|
||||
@@ -112,6 +112,16 @@ class Query
|
||||
return $this->connection->execute($sql, $bind, $fetch, $getLastInsID);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最近插入的ID
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getLastInsID()
|
||||
{
|
||||
return $this->connection->getLastInsID();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前的builder实例对象
|
||||
* @access protected
|
||||
|
||||
@@ -144,14 +144,7 @@ class Handle
|
||||
// 不显示详细错误信息
|
||||
$data['message'] = Config::get('error_message');
|
||||
}
|
||||
ob_start();
|
||||
ob_implicit_flush(0);
|
||||
extract($data);
|
||||
include Config::get('exception_tmpl');
|
||||
// 获取并清空缓存
|
||||
$content = ob_get_clean();
|
||||
|
||||
$response = Response::create('html')->data($content);
|
||||
$response = Response::create('html')->render(true)->data(Config::get('exception_tmpl'))->vars($data);
|
||||
|
||||
if ($exception instanceof HttpException) {
|
||||
$statusCode = $exception->getStatusCode();
|
||||
|
||||
@@ -18,9 +18,11 @@ use think\View;
|
||||
class Html extends Response
|
||||
{
|
||||
// 输出参数
|
||||
protected $options = [];
|
||||
protected $vars = [];
|
||||
protected $replace = [];
|
||||
protected $options = [];
|
||||
protected $vars = [];
|
||||
protected $replace = [];
|
||||
protected $contentType = 'text/html';
|
||||
protected $render = false;
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
@@ -31,8 +33,25 @@ class Html extends Response
|
||||
protected function output($data)
|
||||
{
|
||||
// 返回JSON数据格式到客户端 包含状态信息
|
||||
return View::instance(Config::get('template'), Config::get('view_replace_str'))
|
||||
->fetch($data, $this->vars, $this->replace);
|
||||
if ($this->render) {
|
||||
// 渲染模板输出
|
||||
return View::instance(Config::get('template'), Config::get('view_replace_str'))
|
||||
->fetch($data, $this->vars, $this->replace, [], false, false);
|
||||
} else {
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否需要进行视图渲染
|
||||
* @access protected
|
||||
* @param bool $render 是否渲染
|
||||
* @return $this
|
||||
*/
|
||||
public function render($render = true)
|
||||
{
|
||||
$this->render = $render;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,7 +19,7 @@ class Json extends Response
|
||||
protected $options = [
|
||||
'json_encode_param' => JSON_UNESCAPED_UNICODE,
|
||||
];
|
||||
|
||||
protected $contentType = 'application/json';
|
||||
/**
|
||||
* 处理数据
|
||||
* @access protected
|
||||
|
||||
@@ -21,6 +21,7 @@ class Jsonp extends Response
|
||||
'default_jsonp_handler' => 'jsonpReturn',
|
||||
'json_encode_param' => JSON_UNESCAPED_UNICODE,
|
||||
];
|
||||
protected $contentType = 'application/json';
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
|
||||
@@ -110,7 +110,7 @@ trait Jump
|
||||
*/
|
||||
public function redirect($url, $code = 301, $params = [])
|
||||
{
|
||||
Response::create('redirect')->data($url)->code($code)->params($params);
|
||||
return Response::create('redirect')->data($url)->code($code)->params($params);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user