Uploadfiy.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\common\service\upload;
  10. use app\common\service\Service;
  11. use think\exception\ValidateException;
  12. use think\facade\Config;
  13. use think\facade\Filesystem;
  14. use think\facade\Request;
  15. use think\Image;
  16. class Uploadfiy extends Service {
  17. /**
  18. * 获取上传的文件信息对象
  19. * @param string $name
  20. * @return array|\think\file\UploadedFile|null
  21. */
  22. public static function get($name=""){
  23. return Request::file($name);
  24. }
  25. public static function upload($name="",$isThumb=false,$disk="public",$suffix=[]){
  26. try{
  27. $file = self::get($name);
  28. if(empty($suffix)){
  29. $suffix = ["jpg","png","gif","jpeg","bmp","mp4","zip","rar","txt","apk","wgt"];
  30. }
  31. if(!in_array($file->extension(),$suffix)){
  32. throw new \Exception("您所选择的文件不允许上传。",0);
  33. }
  34. $dir = self::getUploadPath($disk);
  35. $uploadFile = Filesystem::disk($disk)->putFile( self::getDirectory($file->extension()), $file);
  36. $name = basename($uploadFile);
  37. $filePath = str_replace('\\','/',$dir . '/' . $uploadFile);
  38. // 如果是证书文件,将不上传到oss存储服务器上
  39. $ossInfo = ["type"=>0,"path"=>""];
  40. //生成缩略图
  41. if(Config::get("base.is_thumb_image") && $isThumb && in_array($file->extension(),["jpg","png","gif","jpeg","bmp"])){
  42. $thumb_image_list = Config::get("base.thumb_image_list");
  43. foreach($thumb_image_list as $key=>$val){
  44. $image = Image::open($filePath);
  45. $tempPath = str_replace($name, $key . '_' . $name, $filePath);
  46. $image->thumb($val[0], $val[1])->save($tempPath);
  47. }
  48. }
  49. // 返回文件信息
  50. return [
  51. "type" => $ossInfo["type"],
  52. "oss_url" => ($ossInfo["type"] == 0 ? "" : $ossInfo["path"]),
  53. "title" => $file->getOriginalName(),
  54. "name" => $name,
  55. "path" => "/".$filePath,
  56. "suffix" => strtolower($file->extension()),
  57. "size" => $file->getSize(),
  58. "time"=>time()
  59. ];
  60. }catch (ValidateException $ex){
  61. throw new \Exception($ex->getError(),$ex->getCode());
  62. }catch (\Exception $ex){
  63. throw new \Exception($ex->getMessage() .' a '. $ex->getFile() . ' b ' . $ex->getLine(),$ex->getCode());
  64. }
  65. }
  66. /**
  67. * 获取上传目录
  68. * @param $disk
  69. * @return string
  70. */
  71. public static function getUploadPath($disk){
  72. switch($disk){
  73. case "certificate":
  74. return "runtime/certificate";
  75. case "public":
  76. default:
  77. return "uploads";
  78. }
  79. }
  80. /**
  81. * 获取文件分类目录
  82. * @param $suffix
  83. * @return string
  84. */
  85. public static function getDirectory($suffix){
  86. if(in_array($suffix,["jpg","png","gif","jpeg","bmp"])){
  87. return "images";
  88. }else if(in_array($suffix,["mp4"])){
  89. return "video";
  90. }else if(in_array($suffix,["zip","rar","txt","apk","wgt"])){
  91. return "file";
  92. }else if(in_array($suffix,["pem"])){
  93. return "";
  94. }
  95. }
  96. }