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.
107 lines
2.8 KiB
107 lines
2.8 KiB
<?php
|
|
// 应用公共文件
|
|
|
|
if (!function_exists('_error'))
|
|
{
|
|
function _error($msg, int $code = 400, $data = null, $return_array = false){
|
|
|
|
$result = [
|
|
'code' => $code,
|
|
'msg' => $msg
|
|
];
|
|
if($data instanceof \Exception && (config('app.app_debug') || request()->param('xdebug') == 'xdebug')){
|
|
$result['data'] = $data->getTrace();
|
|
}elseif ($data !== null){
|
|
$result['data'] = $data;
|
|
}
|
|
if ($return_array){
|
|
return $result;
|
|
}else{
|
|
return json($result);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
if (! function_exists('_success'))
|
|
{
|
|
function _success($msg, $data = null, $return_array = false){
|
|
$result = [
|
|
'code' => 200,
|
|
'msg' => $msg
|
|
];
|
|
if ($data !== null){
|
|
$result['data'] = $data;
|
|
}
|
|
if ($return_array){
|
|
return $result;
|
|
}else{
|
|
return json($result);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
if (! function_exists('upload_file'))
|
|
{
|
|
/**
|
|
* 上传文件.
|
|
*
|
|
* @param string $file 上传的文件
|
|
* @param string $name 上传的位置
|
|
* @param string $path 上传的文件夹
|
|
* @param string $validate 规则验证
|
|
*
|
|
* @return string|bool
|
|
* @author niu
|
|
*/
|
|
function upload_file($file = null, $name = 'local', $path = '', $validate = '')
|
|
{
|
|
//文件
|
|
if (! $file) {
|
|
return false;
|
|
}
|
|
//上传配置
|
|
$config_name = 'filesystem.disks.'.$name;
|
|
$filesystem = config($config_name);
|
|
if (! $filesystem) {
|
|
return false;
|
|
}
|
|
//上传文件
|
|
if ($validate) {
|
|
validate(['file' => $validate])->check(['file' => $file]);
|
|
}
|
|
$savename = \think\facade\Filesystem::disk($name)->putFile($path, $file, function ($file) {
|
|
//重命名
|
|
return date('Ymd').'/'.md5((string) microtime(true));
|
|
});
|
|
if (isset($filesystem['url'])) {
|
|
$savename = $filesystem['url'].$savename;
|
|
}
|
|
|
|
return $savename;
|
|
}
|
|
}
|
|
|
|
if (! function_exists('getSourceFileSize')) {
|
|
function getSourceFileSize($size)
|
|
{
|
|
|
|
$file_size = $size - 1;
|
|
|
|
if ($file_size >= 1099511627776) $show_filesize = number_format(($file_size / 1099511627776), 2) . " TB";
|
|
|
|
elseif ($file_size >= 1073741824) $show_filesize = number_format(($file_size / 1073741824), 2) . " GB";
|
|
|
|
elseif ($file_size >= 1048576) $show_filesize = number_format(($file_size / 1048576), 2) . " MB";
|
|
|
|
elseif ($file_size >= 1024) $show_filesize = number_format(($file_size / 1024), 2) . " KB";
|
|
|
|
elseif ($file_size > 0) $show_filesize = $file_size . " b";
|
|
|
|
elseif ($file_size == 0 || $file_size == -1) $show_filesize = "0 b";
|
|
|
|
return $show_filesize;
|
|
|
|
}
|
|
}
|
|
|