196 lines
4.6 KiB
PHP
196 lines
4.6 KiB
PHP
<?php
|
||
|
||
if (!function_exists('is_wechat')) {
|
||
/**
|
||
* 判断是否是微信访问.
|
||
*
|
||
* @return bool
|
||
*/
|
||
function is_wechat(): bool
|
||
{
|
||
return str_contains(\Jenssegers\Agent\Facades\Agent::getUserAgent(), 'MicroMessenger');
|
||
}
|
||
}
|
||
|
||
if (!function_exists('is_mobile')) {
|
||
/**
|
||
* 是否手机
|
||
*
|
||
* @return bool
|
||
*/
|
||
function is_mobile(): bool
|
||
{
|
||
$agent = new Jenssegers\Agent\Agent();
|
||
|
||
return $agent->isMobile();
|
||
}
|
||
}
|
||
|
||
if (!function_exists('json_success')) {
|
||
/**
|
||
* 成功
|
||
*
|
||
* @param mixed $message
|
||
* @param mixed $data
|
||
* @return void
|
||
*/
|
||
function json_success(mixed $message = '', mixed $data = []): void
|
||
{
|
||
if (is_string($message)) {
|
||
$result = [
|
||
'msg' => $message,
|
||
'data' => $data,
|
||
];
|
||
} else {
|
||
$result = [
|
||
'msg' => '',
|
||
'data' => $message,
|
||
];
|
||
}
|
||
$result['code'] = 0;
|
||
response()->json($result)->throwResponse();
|
||
}
|
||
}
|
||
|
||
if (!function_exists('json_page')) {
|
||
/**
|
||
* 分页
|
||
*
|
||
* @param $data
|
||
* @param mixed $pageData
|
||
* @param mixed $message
|
||
* @return void
|
||
*/
|
||
function json_page($data, mixed $pageData = [], mixed $message = ''): void
|
||
{
|
||
$pageData['total'] = method_exists($data, 'total') ? $data->total() : null;
|
||
$pageData['count'] = $data->count();
|
||
$pageData['page'] = $data->currentPage();
|
||
$pageData['per_page'] = $data->perPage();
|
||
$pageData['last'] = method_exists($data, 'lastPage') ? $data->lastPage() : null;
|
||
json_success($message, [
|
||
'page_info' => $pageData,
|
||
'page_data' => $data->toArray()['data'],
|
||
]);
|
||
}
|
||
}
|
||
|
||
if (!function_exists('json_error')) {
|
||
/**
|
||
* 失败
|
||
*
|
||
* @param mixed $message
|
||
* @param mixed $data
|
||
* @param int $code
|
||
* @return void
|
||
*/
|
||
function json_error(mixed $message = '', mixed $data = [], int $code = 1): void
|
||
{
|
||
$result = [
|
||
'code' => $code,
|
||
'msg' => $message,
|
||
'data' => $data,
|
||
];
|
||
response()->json($result)->throwResponse();
|
||
}
|
||
}
|
||
|
||
if (!function_exists('json_guest')) {
|
||
/**
|
||
* 失败 - 未登录
|
||
*
|
||
* @param mixed $message
|
||
* @param int $code
|
||
* @return void
|
||
*/
|
||
function json_guest(mixed $message = '未登录,请先登录', int $code = 99): void
|
||
{
|
||
json_error($message, [], $code);
|
||
}
|
||
}
|
||
|
||
if (!function_exists('json_result')) {
|
||
/**
|
||
* 结果
|
||
*
|
||
* @param mixed $message
|
||
* @param mixed $data
|
||
* @param int $code
|
||
* @return array
|
||
*/
|
||
function json_result(mixed $message = '', mixed $data = [], int $code = 0): array
|
||
{
|
||
return [
|
||
'code' => $code,
|
||
'msg' => $message,
|
||
'data' => $data,
|
||
];
|
||
}
|
||
}
|
||
|
||
if (!function_exists('url_to_uri')) {
|
||
/**
|
||
* 网址去除域名段 获取 url
|
||
*
|
||
* @param $url
|
||
* @return string|null
|
||
*/
|
||
function url_to_uri($url): ?string
|
||
{
|
||
if (!$url) {
|
||
return null;
|
||
}
|
||
$arr = parse_url($url);
|
||
if (isset($arr['scheme']) && isset($arr['host'])) {
|
||
$domains = [config('filesystems.disks.' . config('filesystems.default') . '.domain')];
|
||
$domain = $arr['scheme'] . '://' . $arr['host'];
|
||
if (in_array($domain, $domains)) {
|
||
$url = str_replace($arr['scheme'] . '://' . $arr['host'], '', $url);
|
||
$url = preg_replace('/!.*/', '', $url);
|
||
}
|
||
} elseif (!str_starts_with($url, '/')) {
|
||
$url = '/' . $url;
|
||
}
|
||
return $url;
|
||
}
|
||
}
|
||
|
||
if (!function_exists('uri_to_image_url')) {
|
||
/**
|
||
* 还原图片网址
|
||
*
|
||
* @param $uri
|
||
* @return string|null
|
||
*/
|
||
function uri_to_image_url($uri): ?string
|
||
{
|
||
if (!$uri) {
|
||
return null;
|
||
}
|
||
return config('filesystems.disks.' . config('filesystems.default') . '.domain') . url_to_uri($uri);
|
||
}
|
||
}
|
||
|
||
if (!function_exists('string_to_array')) {
|
||
/**
|
||
* 字符串转数组(针对非0传值场景)
|
||
*
|
||
* @param mixed $string 原始字符串
|
||
* @param bool $sort 是否重新排序
|
||
* @param string $separator 切割字符
|
||
* @return array
|
||
*/
|
||
function string_to_array(mixed $string, bool $sort = false, string $separator = ','): array
|
||
{
|
||
$arr = explode($separator, $string);
|
||
foreach ($arr as &$value) {
|
||
$value = trim($value);
|
||
}
|
||
$arr = array_filter($arr);
|
||
if ($sort) {
|
||
sort($arr);
|
||
}
|
||
return $arr;
|
||
}
|
||
}
|