thinkphp6封装json返回数据

文章描述:

thinkphp6封装ajax请求返回的json格式数据

Preview

Response

1、打开文件tp/app/common.php文件,添加以下代码

// function_exists('success')判断是否有这个文件

// 成功时调用
if(!function_exists('success'))
{
    function success($code,$mes,$data)
    {
        return json([
            'code'=>$code,
            'message'=>$mes,
            'data'=>$data,
        ]);
    }
}
// 失败时调用
if(!function_exists('error'))
{
    function error($code,$mes,$data)
    {
        return json([
            'code'=>$code,
            'message'=>$mes,
            'data'=>$data,

        ]);

    }
}

2、控制器方法中使用

//成功时调用
return success(200,'登录成功',null);
//失败时调用
return error(300,'登录失败',null);

 

发布时间:2022/12/27

发表评论