mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
改写 控制器类的 error和sucess方法
This commit is contained in:
@@ -33,10 +33,8 @@ return [
|
|||||||
'url_params_bind' => true,
|
'url_params_bind' => true,
|
||||||
// 异常页面的模板文件
|
// 异常页面的模板文件
|
||||||
'exception_tmpl' => THINK_PATH . 'tpl/think_exception.tpl',
|
'exception_tmpl' => THINK_PATH . 'tpl/think_exception.tpl',
|
||||||
// 默认错误跳转对应的模板文件
|
// 默认跳转页面对应的模板文件
|
||||||
'error_tmpl' => THINK_PATH . 'tpl/dispatch_jump.tpl',
|
'dispatch_jump_tmpl' => THINK_PATH . 'tpl/dispatch_jump.tpl',
|
||||||
// 默认成功跳转对应的模板文件
|
|
||||||
'success_tmpl' => THINK_PATH . 'tpl/dispatch_jump.tpl',
|
|
||||||
// 默认AJAX 数据返回格式,可选JSON XML ...
|
// 默认AJAX 数据返回格式,可选JSON XML ...
|
||||||
'default_ajax_return' => 'JSON',
|
'default_ajax_return' => 'JSON',
|
||||||
// 默认JSONP格式返回的处理方法
|
// 默认JSONP格式返回的处理方法
|
||||||
|
|||||||
@@ -130,15 +130,29 @@ class Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ajax方式返回数据到客户端
|
* 返回封装后的API数据到客户端
|
||||||
* @access protected
|
* @access protected
|
||||||
* @param mixed $data 要返回的数据
|
* @param mixed $data 要返回的数据
|
||||||
* @param String $type AJAX返回数据格式
|
* @param string $msg 提示信息
|
||||||
|
* @param integer $code 返回的code
|
||||||
|
* @param string $url 重定向地址
|
||||||
|
* @param integer $wait 跳转等待时间
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function ajaxReturn($data, $type = '')
|
public function result($data = '', $msg = '', $code = 0, $url = '', $wait = 0)
|
||||||
{
|
{
|
||||||
Response::returnData($data, $type);
|
$result = [
|
||||||
|
'code' => $code,
|
||||||
|
'msg' => $msg,
|
||||||
|
'data' => $data,
|
||||||
|
'url' => $url,
|
||||||
|
'wait' => $wait,
|
||||||
|
];
|
||||||
|
if ('html' == Config::get('default_return_type')) {
|
||||||
|
return $this->fetch(Config::get('dispatch_jump_tmpl'), $result);
|
||||||
|
} else {
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -146,12 +160,13 @@ class Controller
|
|||||||
* @access protected
|
* @access protected
|
||||||
* @param string $message 错误信息
|
* @param string $message 错误信息
|
||||||
* @param string $jumpUrl 页面跳转地址
|
* @param string $jumpUrl 页面跳转地址
|
||||||
* @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
|
* @param integer $wait 跳转等待时间
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function error($message, $jumpUrl = '', $ajax = false)
|
protected function error($message, $jumpUrl = '', $wait = 5)
|
||||||
{
|
{
|
||||||
return $this->dispatchJump($message, 0, $jumpUrl, $ajax);
|
$jumpUrl = $jumpUrl ?: 'javascript:history.back(-1);';
|
||||||
|
return $this->result('', $message, 0, $jumpUrl, $wait);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -159,72 +174,13 @@ class Controller
|
|||||||
* @access protected
|
* @access protected
|
||||||
* @param string $message 提示信息
|
* @param string $message 提示信息
|
||||||
* @param string $jumpUrl 页面跳转地址
|
* @param string $jumpUrl 页面跳转地址
|
||||||
* @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
|
* @param integer $wait 跳转等待时间
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function success($message, $jumpUrl = '', $ajax = false)
|
protected function success($message, $jumpUrl = '', $wait = 3)
|
||||||
{
|
{
|
||||||
return $this->dispatchJump($message, 1, $jumpUrl, $ajax);
|
$jumpUrl = $jumpUrl ?: $_SERVER["HTTP_REFERER"];
|
||||||
|
return $this->result('', $message, 1, $jumpUrl, $wait);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 默认跳转操作 支持错误导向和正确跳转
|
|
||||||
* 调用模板显示 默认为public目录下面的success页面
|
|
||||||
* 提示页面为可配置 支持模板标签
|
|
||||||
* @access private
|
|
||||||
* @param string $message 提示信息
|
|
||||||
* @param Boolean $status 状态
|
|
||||||
* @param string $jumpUrl 页面跳转地址
|
|
||||||
* @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
private function dispatchJump($message, $status = 1, $jumpUrl = '', $ajax = false)
|
|
||||||
{
|
|
||||||
if (true === $ajax || IS_AJAX) {
|
|
||||||
// AJAX提交
|
|
||||||
$data = is_array($ajax) ? $ajax : [];
|
|
||||||
$data['info'] = $message;
|
|
||||||
$data['status'] = $status;
|
|
||||||
$data['url'] = $jumpUrl;
|
|
||||||
$this->ajaxReturn($data);
|
|
||||||
}
|
|
||||||
// 模板变量
|
|
||||||
$data = [];
|
|
||||||
if (is_int($ajax)) {
|
|
||||||
$data['waitSecond'] = $ajax;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($jumpUrl)) {
|
|
||||||
$data['jumpUrl'] = $jumpUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 提示标题
|
|
||||||
$data['msgTitle'] = Lang::get($status ? '_OPERATION_SUCCESS_' : '_OPERATION_FAIL_');
|
|
||||||
$data['status'] = $status; // 状态
|
|
||||||
|
|
||||||
//保证输出不受静态缓存影响
|
|
||||||
Config::set('html_cache_on', false);
|
|
||||||
if ($status) {
|
|
||||||
//发送成功信息
|
|
||||||
$data['message'] = $message; // 提示信息
|
|
||||||
// 成功操作后默认停留1秒
|
|
||||||
$data['waitSecond'] = '1';
|
|
||||||
// 默认操作成功自动返回操作前页面
|
|
||||||
if (!$jumpUrl) {
|
|
||||||
$data["jumpUrl"] = $_SERVER["HTTP_REFERER"];
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->display(Config::get('success_tmpl'), $data);
|
|
||||||
} else {
|
|
||||||
$data['error'] = $message; // 提示信息
|
|
||||||
//发生错误时候默认停留3秒
|
|
||||||
$data['waitSecond'] = '3';
|
|
||||||
// 默认发生错误的话自动返回上页
|
|
||||||
if (!$jumpUrl) {
|
|
||||||
$data['jumpUrl'] = 'javascript:history.back(-1);';
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->display(Config::get('error_tmpl'), $data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,16 +16,17 @@ body{ background: #fff; font-family: '微软雅黑'; color: #333; font-size: 16p
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="system-message">
|
<div class="system-message">
|
||||||
<present name="message">
|
<switch name="code">
|
||||||
<h1>:)</h1>
|
<case value="1"><h1>:)</h1>
|
||||||
<p class="success"><?php echo(strip_tags($message)); ?></p>
|
<p class="success"><?php echo(strip_tags($msg)); ?></p>
|
||||||
<else/>
|
</case>
|
||||||
<h1>:(</h1>
|
<case value="0"><h1>:(</h1>
|
||||||
<p class="error"><?php echo(strip_tags($error)); ?></p>
|
<p class="error"><?php echo(strip_tags($msg)); ?></p>
|
||||||
</present>
|
</case>
|
||||||
|
</switch>
|
||||||
<p class="detail"></p>
|
<p class="detail"></p>
|
||||||
<p class="jump">
|
<p class="jump">
|
||||||
页面自动 <a id="href" href="<?php echo($jumpUrl); ?>">跳转</a> 等待时间: <b id="wait"><?php echo($waitSecond); ?></b>
|
页面自动 <a id="href" href="<?php echo($url); ?>">跳转</a> 等待时间: <b id="wait"><?php echo($wait); ?></b>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
|||||||
Reference in New Issue
Block a user