You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.2 KiB
50 lines
1.2 KiB
<?php
|
|
|
|
namespace App\Traits;
|
|
|
|
use Throwable;
|
|
use Illuminate\Support\Facades\Response;
|
|
|
|
trait ResponseHelper
|
|
{
|
|
|
|
public function jsonSuccess($data = [], $metalData = [], $code = 0, $msg = 'success')
|
|
{
|
|
return Response::json([
|
|
'code' => $code,
|
|
'msg' => $msg,
|
|
'data' => $data,
|
|
'metal_data' => $metalData,
|
|
]);
|
|
}
|
|
|
|
public function jsonFail($code = 0, $msg = 'fail', $data = [], $metalData = [])
|
|
{
|
|
return Response::json([
|
|
'code' => $code,
|
|
'msg' => $msg,
|
|
'data' => $data,
|
|
'metal_data' => $metalData,
|
|
]);
|
|
}
|
|
|
|
public function jsonFailValidated($msg = 'validate fail', $code = 5000, $data = [], $metalData = [])
|
|
{
|
|
return Response::json([
|
|
'code' => $code,
|
|
'msg' => $msg,
|
|
'data' => $data,
|
|
'metal_data' => $metalData,
|
|
]);
|
|
}
|
|
|
|
public function jsonException(Throwable $e, $statusCode = 200)
|
|
{
|
|
return Response::json([
|
|
'code' => $e->getCode() ?: $statusCode,
|
|
'msg' => $e->getMessage(),
|
|
'data' => [],
|
|
'metal_data' => [],
|
|
], 500);
|
|
}
|
|
}
|