Commit fa6370a1 authored by 王源's avatar 王源 🎧

优化代码,添加数字转文件大小方法,添加删除文件方法

parent ddcf983b
...@@ -18,7 +18,8 @@ ...@@ -18,7 +18,8 @@
"hyperf/validation": "~1.1.0", "hyperf/validation": "~1.1.0",
"hyperf/command": "~1.1.0", "hyperf/command": "~1.1.0",
"hyperf/redis": "~1.1.0", "hyperf/redis": "~1.1.0",
"hyperf/guzzle": "^1.1@dev" "hyperf/guzzle": "^1.1@dev",
"hyperf/config": "^1.1@dev"
}, },
"minimum-stability": "dev", "minimum-stability": "dev",
"autoload": { "autoload": {
......
...@@ -76,7 +76,7 @@ class UploadManager ...@@ -76,7 +76,7 @@ class UploadManager
} }
// 判断文件大小 // 判断文件大小
if ($file->getSize() > $options['maxSize']) { if ($file->getSize() > $options['maxSize']) {
throw new HttpResponseException('文件超出系统规定的大小,最大不能超过' . $options['maxSize']); throw new HttpResponseException('文件超出系统规定的大小,最大不能超过' . num_2_file_size($options['maxSize']));
} }
// 文件重命名,由当前日期时间 + 唯一ID + 扩展名 // 文件重命名,由当前日期时间 + 唯一ID + 扩展名
$fileName = date('YmdHis') . uniqid() . '.' . $extension; $fileName = date('YmdHis') . uniqid() . '.' . $extension;
...@@ -167,4 +167,14 @@ class UploadManager ...@@ -167,4 +167,14 @@ class UploadManager
} }
} }
public static function deleteFile($path)
{
$path = str_replace(config('app_domain'), '', $path);
$path = ltrim($path, '/');
$path = 'public/' . $path;
if (file_exists($path)) {
unlink($path);
}
}
} }
\ No newline at end of file
...@@ -3,35 +3,36 @@ ...@@ -3,35 +3,36 @@
namespace Meibuyu\Micro\Model; namespace Meibuyu\Micro\Model;
use Hyperf\Utils\Context; use Hyperf\Utils\Context;
use Meibuyu\Micro\Exceptions\ObjectNotExistException; use Meibuyu\Micro\Exceptions\HttpResponseException;
class Auth class Auth
{ {
/** /**
* @return bool|mixed|string|null * @return bool|mixed|string|null
* @throws ObjectNotExistException * @throws HttpResponseException
*/ */
private static function init() private static function init()
{ {
if (Context::has('auth')) { if (Context::has('auth')) {
return Context::get('auth'); return Context::get('auth');
} else { } else {
$token = token(); $token = token();
if (!$token) throw new ObjectNotExistException('Token'); if (!$token) throw new HttpResponseException('Token不存在');
$auth = redis()->get($token); $auth = redis()->get($token);
if ($auth) { if ($auth) {
$auth = json_decode($auth, true); $auth = json_decode($auth, true);
Context::set('auth', $auth); Context::set('auth', $auth);
return $auth; return $auth;
} else { } else {
throw new ObjectNotExistException('User'); throw new HttpResponseException('用户不存在');
} }
} }
} }
/** /**
* @return object * @return object
* @throws ObjectNotExistException * @throws HttpResponseException
*/ */
public static function user() public static function user()
{ {
...@@ -40,7 +41,7 @@ class Auth ...@@ -40,7 +41,7 @@ class Auth
/** /**
* @return integer * @return integer
* @throws ObjectNotExistException * @throws HttpResponseException
*/ */
public static function id() public static function id()
{ {
......
...@@ -370,3 +370,30 @@ if (!function_exists('flushAnnotationCache')) { ...@@ -370,3 +370,30 @@ if (!function_exists('flushAnnotationCache')) {
return true; return true;
} }
} }
if (!function_exists('num_2_file_size')) {
/**
* 数字转文件大小
* @param $num
* @return string
*/
function num_2_file_size($num)
{
$p = 0;
$format = 'KB';
if ($num > 0 && $num < 1024) {
return number_format($num) . ' ' . $format;
} else if ($num >= 1024 && $num < pow(1024, 2)) {
$p = 1;
$format = 'MB';
} else if ($num >= pow(1024, 2) && $num < pow(1024, 3)) {
$p = 2;
$format = 'GB';
} else if ($num >= pow(1024, 3) && $num < pow(1024, 4)) {
$p = 3;
$format = 'TB';
}
$num /= pow(1024, $p);
return number_format($num, 2) . ' ' . $format;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment