diff --git a/convention.php b/convention.php index 421ab39c..25c859c5 100644 --- a/convention.php +++ b/convention.php @@ -33,10 +33,8 @@ return [ 'url_params_bind' => true, // 异常页面的模板文件 'exception_tmpl' => THINK_PATH . 'tpl/think_exception.tpl', - // 默认错误跳转对应的模板文件 - 'error_tmpl' => THINK_PATH . 'tpl/dispatch_jump.tpl', - // 默认成功跳转对应的模板文件 - 'success_tmpl' => THINK_PATH . 'tpl/dispatch_jump.tpl', + // 默认跳转页面对应的模板文件 + 'dispatch_jump_tmpl' => THINK_PATH . 'tpl/dispatch_jump.tpl', // 默认AJAX 数据返回格式,可选JSON XML ... 'default_ajax_return' => 'JSON', // 默认JSONP格式返回的处理方法 diff --git a/library/think/controller.php b/library/think/controller.php index f1d80cd4..989f07fe 100644 --- a/library/think/controller.php +++ b/library/think/controller.php @@ -130,15 +130,29 @@ class Controller } /** - * Ajax方式返回数据到客户端 + * 返回封装后的API数据到客户端 * @access protected * @param mixed $data 要返回的数据 - * @param String $type AJAX返回数据格式 + * @param string $msg 提示信息 + * @param integer $code 返回的code + * @param string $url 重定向地址 + * @param integer $wait 跳转等待时间 * @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 * @param string $message 错误信息 * @param string $jumpUrl 页面跳转地址 - * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间 + * @param integer $wait 跳转等待时间 * @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 * @param string $message 提示信息 * @param string $jumpUrl 页面跳转地址 - * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间 + * @param integer $wait 跳转等待时间 * @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); - } - } } diff --git a/tpl/dispatch_jump.tpl b/tpl/dispatch_jump.tpl index bf035c83..e775aabe 100644 --- a/tpl/dispatch_jump.tpl +++ b/tpl/dispatch_jump.tpl @@ -16,16 +16,17 @@ body{ background: #fff; font-family: '微软雅黑'; color: #333; font-size: 16p