87 lines
2.0 KiB
PHP
87 lines
2.0 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
use support\Response;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Here is your custom functions.
|
||
|
|
*/
|
||
|
|
/**
|
||
|
|
* 成功相响应
|
||
|
|
* @param string $msg
|
||
|
|
* @param array $data
|
||
|
|
* @param int $code
|
||
|
|
* @return Response
|
||
|
|
*/
|
||
|
|
function Success(array|object $data = [], string $msg = '', int $code = 200): Response
|
||
|
|
{
|
||
|
|
return new Response(200,
|
||
|
|
['Content-Type' => 'application/json', 'access-control-allow-origin' => '*'],
|
||
|
|
json_encode([
|
||
|
|
'msg' => $msg, 'code' => $code, 'data' => $data, 'time' => time()
|
||
|
|
]));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 失败响应
|
||
|
|
* @param string $msg
|
||
|
|
* @param array $data
|
||
|
|
* @param int $code
|
||
|
|
* @return Response
|
||
|
|
*/
|
||
|
|
function Error(string $msg = '', array $data = [], int $code = 500): Response
|
||
|
|
{
|
||
|
|
return new Response(200,
|
||
|
|
['Content-Type' => 'application/json', 'access-control-allow-origin' => '*'],
|
||
|
|
json_encode([
|
||
|
|
'msg' => $msg, 'code' => $code, 'data' => $data, 'time' => time()
|
||
|
|
]));
|
||
|
|
}
|
||
|
|
|
||
|
|
function getHostUrl(): bool|array|string
|
||
|
|
{
|
||
|
|
return getenv(getenv('ONLINE') ? 'APP_HOST' : 'DEV_HOST');
|
||
|
|
}
|
||
|
|
|
||
|
|
//
|
||
|
|
function ImgSrc(string $str): string
|
||
|
|
{
|
||
|
|
if (str_contains($str, '/app/admin/upload')) {
|
||
|
|
if (str_contains(",", $str)) {
|
||
|
|
$arr = explode(",", $str);
|
||
|
|
foreach ($arr as &$v) {
|
||
|
|
$v = getHostUrl() . str_replace('/app/admin/upload', '/upload', $v);
|
||
|
|
}
|
||
|
|
return implode(",", $arr);
|
||
|
|
} else {
|
||
|
|
return getHostUrl() . str_replace('/app/admin/upload', '/upload', $str);
|
||
|
|
}
|
||
|
|
|
||
|
|
} else {
|
||
|
|
if (str_contains($str, '/upload/') && str_starts_with($str, "/upload")) {
|
||
|
|
$arr = explode(",", $str);
|
||
|
|
foreach ($arr as &$v) {
|
||
|
|
$v = getHostUrl() . $v;
|
||
|
|
}
|
||
|
|
return implode(",", $arr);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $str;
|
||
|
|
}
|
||
|
|
|
||
|
|
//图片域名
|
||
|
|
function ImgSrcByArr(array $arr): array
|
||
|
|
{
|
||
|
|
foreach ($arr as &$v) {
|
||
|
|
$v = ImgSrc($v);
|
||
|
|
};
|
||
|
|
return $arr;
|
||
|
|
}
|
||
|
|
|
||
|
|
//上传url 转全路径
|
||
|
|
function UploadUrl(string $str): string
|
||
|
|
{
|
||
|
|
return str_replace('/app/admin/upload', '/upload', $str);
|
||
|
|
}
|
||
|
|
|
||
|
|
|