在Thinkphp6控制器中创建success函数和error函数

/**
     * 操作成功跳转的快捷方法
     * @access protected
     * @param  mixed     $msg 提示信息
     * @param  string    $url 跳转的URL地址
     * @param  string    $type 请求类型,默认是接口请求,如果pc端请求那么用html
     * @param  mixed     $data 返回的数据
     * @param  integer   $wait 跳转等待时间
     * @param  array     $header 发送的Header信息
     * @return void
     */
    protected function success($msg = '', $url = null, $type = '', $data = '', $wait = 3, array $header = [])
    {
        if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
            $url = $_SERVER["HTTP_REFERER"];
        } 
        $result = [
            'code' => 1,
            'msg'  => $msg,
            'data' => $data,
            'url'  => $url,
            'wait' => $wait,
        ];
        
        // 把跳转模板的渲染下沉,这样在 response_send 行为里通过getData()获得的数据是一致性的格式
        if ('html' == strtolower($type)) {
            $data = View::fetch('/dispatch_jump', $result);
            $response = Response::create($data, $type, 200)->header($header);
            throw new HttpResponseException($response);
        }else{
            print_r(json_encode($result));exit;
        }
    }

    /**
     * 操作错误跳转的快捷方法
     * @access protected
     * @param  mixed     $msg 提示信息
     * @param  string    $url 跳转的URL地址
     * @param  string    $type 请求类型,默认是接口请求,如果pc端请求那么用html
     * @param  mixed     $data 返回的数据
     * @param  integer   $code 错误代码
     * @param  integer   $wait 跳转等待时间
     * @param  array     $header 发送的Header信息
     * @return void
     */
    protected function error($msg = '', $url = null, $type = '', $data = '', $code = 0, $wait = 3, array $header = [])
    {
        if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
            $url = $_SERVER["HTTP_REFERER"];
        } 

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

        // 把跳转模板的渲染下沉,这样在 response_send 行为里通过getData()获得的数据是一致性的格式
        if ('html' == strtolower($type)) {
            $data = View::fetch('/dispatch_jump', $result);
            $response = Response::create($data, $type, 200)->header($header);
            throw new HttpResponseException($response);
        }else{
            print_r(json_encode($result));exit;
        }      
    }

把以上代码放到根控制器里面就可以了。

调用方法和以前的版本一样。

猜你喜欢

转载自blog.csdn.net/u010261924/article/details/105210855