Created
October 25, 2022 00:59
-
-
Save lvxianchao/5ad44d14831ce1e8c86cf821f081e674 to your computer and use it in GitHub Desktop.
Laravel 辅助函数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!function_exists('json')) { | |
/** | |
* 返回统一接口响应 | |
* | |
* @param bool $status | |
* @param int $code 状态码 | |
* @param string $message 提示消息 | |
* @param null $data 数据 | |
* @param array $extension 扩展数据 | |
* | |
* @return JsonResponse | |
*/ | |
function json(bool $status, int $code = 200, string $message = '', $data = null, array $extension = []): JsonResponse | |
{ | |
$request = request(); | |
$response = [ | |
'status' => $status, | |
'code' => $code, | |
'message' => $message, | |
'data' => new ArrayObject(), | |
]; | |
if ($data) { | |
$response['data'] = $data; | |
// 普通 Eloquent Collection 对象 | |
if ($data instanceof Illuminate\Database\Eloquent\Collection) { | |
$response['data'] = $data->toArray(); | |
} | |
// 单条数据的结构 | |
if ($data instanceof JsonResource) { | |
$response['data'] = $data; | |
} | |
// 列表数据的结构 | |
if ($data instanceof AnonymousResourceCollection) { | |
// links 附带查询条件 | |
$meta = json_decode($data->withQuery($request->query())->toResponse($request)->content(), true); | |
$meta['meta']['links'] = $meta['links']; | |
$response['data'] = [ | |
'items' => $data, | |
'pagination' => [ | |
'currentPage' => $meta['meta']['current_page'], | |
'perPage' => $meta['meta']['per_page'], | |
'total' => $meta['meta']['total'], | |
], | |
'extensions' => $extension ?: new ArrayObject(), | |
]; | |
} | |
// 查询构造器分页 | |
if ($data instanceof LengthAwarePaginator) { | |
$response['data'] = [ | |
'items' => $data->getCollection(), | |
'pagination' => [ | |
'perPage' => $data->perPage(), | |
'currentPage' => $data->currentPage(), | |
'total' => $data->total(), | |
], | |
'extensions' => $extension ?: new ArrayObject(), | |
]; | |
} | |
} | |
return response()->json($response, $code); | |
} | |
} | |
if (!function_exists('success')) { | |
/** | |
* 返回成功响应 | |
* | |
* @param null $data | |
* @param int $code | |
* @param string $message | |
* @param array $extension | |
* | |
* @return JsonResponse | |
*/ | |
function success($data = null, int $code = 200, string $message = '', array $extension = []): JsonResponse | |
{ | |
if (!$message) { | |
$message = __('操作成功'); | |
} | |
return json(true, $code, $message, $data, $extension); | |
} | |
} | |
if (!function_exists('error')) { | |
/** | |
* 返回失败响应 | |
* | |
* @param string $message | |
* @param int $code | |
* | |
* @return JsonResponse | |
*/ | |
function error(string $message = '', int $code = 500): JsonResponse | |
{ | |
if (!$message) { | |
$message = __('操作失败'); | |
} | |
return json(false, $code, $message); | |
} | |
} | |
if (!function_exists('format')) { | |
/** | |
* 格式化时间 | |
* | |
* @param $datetime null|string|\Illuminate\Support\Carbon 需要被格式化的时间 | |
* | |
* @return mixed|string | |
*/ | |
function format($datetime): string | |
{ | |
if ($datetime instanceof \Illuminate\Support\Carbon) { | |
return $datetime->format('Y-m-d H:i:s'); | |
} | |
if (is_null($datetime)) { | |
return ''; | |
} | |
return $datetime; | |
} | |
} | |
if (!function_exists('get_route_name')) { | |
/** | |
* 获取当前路由的名称 | |
* | |
* @param bool $last 是否获取最后一部分 | |
* | |
* @return string | |
*/ | |
function get_route_name(bool $last = false): string | |
{ | |
$name = request()->route()->getName(); | |
if ($last) { | |
$array = explode('.', $name); | |
return array_pop($array); | |
} | |
return $name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment