Commit fa6370a1 authored by 王源's avatar 王源
Browse files

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

parent ddcf983b
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -18,7 +18,8 @@
    "hyperf/validation": "~1.1.0",
    "hyperf/command": "~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",
  "autoload": {
+11 −1
Original line number Diff line number Diff line
@@ -76,7 +76,7 @@ class UploadManager
            }
            // 判断文件大小
            if ($file->getSize() > $options['maxSize']) {
                throw new HttpResponseException('文件超出系统规定的大小,最大不能超过' . $options['maxSize']);
                throw new HttpResponseException('文件超出系统规定的大小,最大不能超过' . num_2_file_size($options['maxSize']));
            }
            // 文件重命名,由当前日期时间 + 唯一ID + 扩展名
            $fileName = date('YmdHis') . uniqid() . '.' . $extension;
@@ -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
+8 −7
Original line number Diff line number Diff line
@@ -3,13 +3,14 @@
namespace Meibuyu\Micro\Model;

use Hyperf\Utils\Context;
use Meibuyu\Micro\Exceptions\ObjectNotExistException;
use Meibuyu\Micro\Exceptions\HttpResponseException;

class Auth
{

    /**
     * @return bool|mixed|string|null
     * @throws ObjectNotExistException
     * @throws HttpResponseException
     */
    private static function init()
    {
@@ -17,21 +18,21 @@ class Auth
            return Context::get('auth');
        } else {
            $token = token();
            if (!$token) throw new ObjectNotExistException('Token');
            if (!$token) throw new HttpResponseException('Token不存在');
            $auth = redis()->get($token);
            if ($auth) {
                $auth = json_decode($auth, true);
                Context::set('auth', $auth);
                return $auth;
            } else {
                throw new ObjectNotExistException('User');
                throw new HttpResponseException('用户不存在');
            }
        }
    }

    /**
     * @return object
     * @throws ObjectNotExistException
     * @throws HttpResponseException
     */
    public static function user()
    {
@@ -40,7 +41,7 @@ class Auth

    /**
     * @return integer
     * @throws ObjectNotExistException
     * @throws HttpResponseException
     */
    public static function id()
    {
+27 −0
Original line number Diff line number Diff line
@@ -370,3 +370,30 @@ if (!function_exists('flushAnnotationCache')) {
        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;
    }
}