关于TP6 $this->success() , $this->error() 的解决办法

浏览:1510 发布日期:2019/09/28 分类:ThinkPHP6专区 关键字: Thinkphp6
从TP5过来 ,学习TP6 的时候踩了很多坑,其中之一 就是 没有$this-success 这样的提示信息,研究了好久找到了解决办法。 方法如下
第一步:在baseController.php基础控制器中封装success 方法
代码如下需要引入的 文件
use think\exception\HttpResponseException;
use think\facade\Request;


/**
     * 操作成功跳转的快捷方法
     * @access protected
     * @param  mixed     $msg 提示信息
     * @param  string    $url 跳转的URL地址
     * @param  mixed     $data 返回的数据
     * @param  integer   $wait 跳转等待时间
     * @param  array     $header 发送的Header信息
     * @return void
     */
    protected function success($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
    {
        if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
            $url = $_SERVER["HTTP_REFERER"];
        } elseif ($url) {
            $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url);
        }

        $result = [
            'code' => 1,
            'msg'  => $msg,
            'data' => $data,
            'url'  => $url,
            'wait' => $wait,
        ];

        $type = $this->getResponseType();
        if ($type == 'html'){
            $response = view($this->app->config->get('app.dispatch_success_tmpl'), $result);
        } else if ($type == 'json') {
            $response = json($result);
        }
        throw new HttpResponseException($response);
    }

    /**
     * 操作错误跳转的快捷方法
     * @access protected
     * @param  mixed     $msg 提示信息
     * @param  string    $url 跳转的URL地址
     * @param  mixed     $data 返回的数据
     * @param  integer   $wait 跳转等待时间
     * @param  array     $header 发送的Header信息
     * @return void
     */
    protected function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
    {
        if (is_null($url)) {
            $url = $this->request->isAjax() ? '' : 'javascript:history.back(-1);';
        } elseif ($url) {
            $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url);
        }

        $result = [
            'code' => 0,
            'msg'  => $msg,
            'data' => $data,
            'url'  => $url,
            'wait' => $wait,
        ];

        $type = $this->getResponseType();
        if ($type == 'html'){
            $response = view($this->app->config->get('app.dispatch_error_tmpl'), $result);
        } else if ($type == 'json') {
            $response = json($result);
        }
        throw new HttpResponseException($response);
    }


    /**
     * URL重定向  自带重定向无效
     * @access protected
     * @param  string         $url 跳转的URL表达式
     * @param  array|integer  $params 其它URL参数
     * @param  integer        $code http code
     * @param  array          $with 隐式传参
     * @return void
     */
    protected function redirect($url, $params = [], $code = 302, $with = [])
    {
        $response = Response::create($url, 'redirect');

        if (is_integer($params)) {
            $code   = $params;
            $params = [];
        }

        $response->code($code)->params($params)->with($with);

        throw new HttpResponseException($response);
    }



    /**
     * 获取当前的response 输出类型
     * @access protected
     * @return string
     */
    protected function getResponseType()
    {
        return $this->request->isJson() || $this->request->isAjax() ? 'json' : 'html';
    }
第二步:在对应的配置文件中 加上
    // 默认跳转页面对应的模板文件
    'dispatch_success_tmpl' => \think\facade\App::getAppPath(). '../../view/template/dispatch_jump.tpl',
    'dispatch_error_tmpl'   => \think\facade\App::getAppPath(). '../../view/template/dispatch_jump.tpl',
第三步:制作跳转的页面 提示,这个提示页面可以自己设置样式哈,我也是随便弄的,下面的代码 是整个提示模板文件的 代码
文件路径 <!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>跳转提示</title>
</head>
<body>


<div class="ip-attack"><dl>
        <?php switch ($code) {?>
        <?php case 1:?>
        <dt style="color: green"><?php echo(strip_tags($msg));?></dt>
        <?php break;?>
        <?php case 0:?>
        <dt style="color: red"><?php echo(strip_tags($msg));?></dt>
        <?php break;?>
        <?php } ?>
        <br>
        <dt>
            页面自动 <a id="href" href="<?php echo($url);?>">跳转</a> 等待时间: <b id="wait"><?php echo($wait);?></b>
        </dt></dl>
</div>


<script type="text/javascript">
    (function(){
        var wait = document.getElementById('wait'),
            href = document.getElementById('href').href;
        var interval = setInterval(function(){
            var time = --wait.innerHTML;
            if(time <= 0) {

                location.href = href;
                clearInterval(interval);
            };
        }, 1000);


    })();
</script>


</body>
</html>
最佳答案
评论( 相关
后面还有条评论,点击查看>>