$this->success(),
$this->error(),
$this->ajaxReturn(),就不用修改了
/**
* 操作错误跳转的快捷方法
* @param string $message 错误信息
* @param string $jumpUrl 页面跳转地址
* @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
* @return void
*/
function error($message = '', $jumpUrl = '', $ajax = false) {
dispatchJump($message, 0, $jumpUrl, $ajax);
}
/**
* 操作成功跳转的快捷方法
* @param string $message 提示信息
* @param string $jumpUrl 页面跳转地址
* @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
* @return void
*/
function success($message = '', $jumpUrl = '', $ajax = false) {
dispatchJump($message, 1, $jumpUrl, $ajax);
}
/**
* 默认跳转操作 支持错误导向和正确跳转
* 调用模板显示 默认为public目录下面的success页面
* 提示页面为可配置 支持模板标签
* @param string $message 提示信息
* @param Boolean $status 状态
* @param string $jumpUrl 页面跳转地址
* @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
* @access private
* @return void
*/
function dispatchJump($message, $status = 1, $jumpUrl = '', $ajax = false) {
if (true === $ajax || IS_AJAX) {// AJAX提交
$data = is_array($ajax) ? $ajax : array();
$data['info'] = $message;
$data['status'] = $status;
$data['url'] = $jumpUrl;
ajaxReturn($data);
}
// { 1.组装返回数据 }
$reData = array();
if (is_int($ajax)) {
$reData['waitSecond'] = $ajax; //跳转等待时间
}
if (!empty($jumpUrl)) {
$reData['jumpUrl'] = $jumpUrl; //跳转网址
}
$reData['msgTitle'] = $status ? '操作成功!' :'操作失败!'; // 提示标题
if ($this->get('closeWin')) {
$reData['jumpUrl'] = 'javascript:window.close();'; //如果设置了关闭窗口,则提示完毕后自动关闭窗口
}
$reData['status'] = $status; // 状态
// 实例化视图类
$config = array(
'view_engine' => array(
'tpl_cache' => false, // 是否开启模板编译缓存,设为false则每次都会重新编译
'display_cache' => false, // 模板渲染缓存
),
);
$view = new \think\View($config);
// 渲染模板输出 并赋值模板变量
if ($status) {
// { //发送成功信息 }
$reData['message'] = $message; // 状态
if (!isset($reData['waitSecond'])) {
$reData['waitSecond'] = 1; // 成功操作后默认停留1秒
}
if (!isset($this->jumpUrl)) {
$reData['jumpUrl'] = $_SERVER["HTTP_REFERER"]; // 默认操作成功自动返回操作前页面
}
echo $view->fetch(Config::get('dispatch_success_tmpl'), $reData); //原来的配置参数:TMPL_ACTION_SUCCESS
} else {
// { //发送失败信息 }
$reData['error'] = $message; // 状态
if (!isset($reData['waitSecond'])) {
$reData['waitSecond'] = 3; //发生错误时候默认停留3秒
}
if (!isset($this->jumpUrl)) {
$reData['jumpUrl'] = 'javascript:history.back(-1);'; // 默认发生错误的话自动返回上页
}
echo $view->fetch(Config::get('dispatch_error_tmpl'), $reData); //原来的配置参数:TMPL_ACTION_ERROR
// 中止执行 避免出错后继续执行
exit;
}
}
/**
* IS_AJAX 返回数据
* @param type $data //要返回的数据
* @param type $type //数据格式类型,json|xml|html|jsonp|script|text
* @param type $return //是否 返回数据 (不输出,只返回数据)
* @param type $isExit //输出数据后,是否结束运行(exit;)
* @return string
*/
function ajaxReturn($data = '', $type = 'json', $return = false, $isExit = true) {
$type = strtolower($type ? : self::$type);
$headers = [
'json' => 'application/json',
'xml' => 'text/xml',
'html' => 'text/html',
'jsonp' => 'application/javascript',
'script' => 'application/javascript',
'text' => 'text/plain',
];
if (!headers_sent() && isset($headers[$type])) {
header('Content-Type:' . $headers[$type] . '; charset=utf-8');
}
switch ($type) {
case 'json':
// 返回JSON数据格式到客户端 包含状态信息
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
break;
case 'jsonp':
// 返回JSON数据格式到客户端 包含状态信息
$handler = !empty($_GET[Config::get('var_jsonp_handler')]) ? $_GET[Config::get('var_jsonp_handler')] : Config::get('default_jsonp_handler');
$data = $handler . '(' . json_encode($data, JSON_UNESCAPED_UNICODE) . ');';
break;
case 'html':
// 实例化视图类
$config = array(
'view_engine' => array(
'tpl_cache' => false, // 是否开启模板编译缓存,设为false则每次都会重新编译
'display_cache' => false, // 模板渲染缓存
),
);
$view = new \think\View($config);
$view->assign($data);
$data = $view->fetch();
case 'text':
// 不做处理
break;
default:
// 用于扩展其他返回格式数据
//APP_HOOK && Hook::listen('return_data', $data);
}
if ($return) {
return $data;
}
echo $data;
$isExit && exit();
} 最佳答案