App_Tools_CommonService.proxy.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Tools;
  3. use App\Constants\ErrorCode;
  4. use Hyperf\Di\Annotation\Inject;
  5. use Hyperf\HttpMessage\Upload\UploadedFile;
  6. use App\Tools\Result;
  7. use ZX\Tools\File\MimeTypes;
  8. use function Hyperf\Support\env;
  9. use Hyperf\HttpServer\Contract\RequestInterface;
  10. class CommonService
  11. {
  12. use \Hyperf\Di\Aop\ProxyTrait;
  13. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  14. function __construct()
  15. {
  16. $this->__handlePropertyHandler(__CLASS__);
  17. }
  18. #[Inject]
  19. protected RequestInterface $request;
  20. public static function uploadFile(UploadedFile $uploadedFile, array $acceptExt, string $fileType = 'image')
  21. {
  22. $ext = $uploadedFile->getExtension();
  23. // var_dump($uploadedFile->getFilename());
  24. // var_dump($uploadedFile->getPath());
  25. // var_dump($uploadedFile->getFilename());
  26. // var_dump($uploadedFile->getExtension());
  27. // var_dump($uploadedFile->getBasename());
  28. // var_dump($uploadedFile->getPathname());
  29. // var_dump($uploadedFile->getPerms());
  30. // var_dump($uploadedFile->getRealPath());
  31. // var_dump($uploadedFile->getFileInfo());
  32. // var_dump($uploadedFile->getPathInfo());
  33. // var_dump($uploadedFile->getClientOriginalName());
  34. if (!in_array($ext, $acceptExt)) {
  35. return Result::error('文件名后缀不允许');
  36. }
  37. //图片检测安全
  38. if ($fileType == 'image') {
  39. $res = self::checkMimeType($uploadedFile, $ext);
  40. if ($res == false) {
  41. return Result::error('文件安全检测未通过');
  42. }
  43. }
  44. $date = date('Ymd');
  45. $filePath = $fileType . DIRECTORY_SEPARATOR . $date;
  46. $allDir = 'public' . DIRECTORY_SEPARATOR . $filePath;
  47. if (!is_dir($allDir)) {
  48. if (!mkdir($allDir, 0755, true)) {
  49. return Result::error('创建文件夹失败');
  50. }
  51. }
  52. $fileName = time() . mt_rand(1, 1000000) . '.' . $ext;
  53. $finalPath = BASE_PATH . DIRECTORY_SEPARATOR . $allDir . DIRECTORY_SEPARATOR . $fileName;
  54. $showPath = $filePath . DIRECTORY_SEPARATOR . $fileName;
  55. $uploadedFile->moveTo($finalPath);
  56. return ['id' => uniqid(), 'src' => $showPath, 'fileName' => $fileName, 'fileType' => $ext];
  57. }
  58. //检测文件是否合法
  59. public static function checkMimeType(UploadedFile $uploadedFile, string $ext = '')
  60. {
  61. try {
  62. $filePath = $uploadedFile->getRealPath();
  63. $fileMimeType = mime_content_type($filePath);
  64. $mimeTypes = MimeTypes::getImage();
  65. $isExist = array_key_exists($fileMimeType, $mimeTypes);
  66. if (!$isExist) {
  67. return Result::error('非允许mime types类型');
  68. }
  69. list($width, $height, $type, $attr) = getimagesize($filePath, $ext);
  70. if ($width <= 0 || $height <= 0) {
  71. return false;
  72. } else {
  73. return true;
  74. }
  75. } catch (\Exception $e) {
  76. return false;
  77. }
  78. }
  79. public function imgUrl(string $str)
  80. {
  81. $scheme = $this->request->getUri()->getScheme() ?? 'http';
  82. $host = $this->request->getUri()->getHost() ?? '127.0.0.1';
  83. $port = $this->request->getUri()->getPort() ?? '';
  84. $url = '';
  85. if ($port == 80 || $port == 443 || empty($port)) {
  86. $url = "{$scheme}://{$host}/{$str}";
  87. } else {
  88. $url = "{$scheme}://{$host}:{$port}/{$str}";
  89. }
  90. return $url;
  91. }
  92. /**
  93. * 获取用户IP
  94. * @return mixed|string
  95. */
  96. public function userIp()
  97. {
  98. $Ip = $this->request->getHeader('x-forwarded-for')[0] ?? $this->request->getHeader('x-real-ip')[0] ?? $this->request->getServerParams()['remote_addr'] ?? '0.0.0.0';
  99. return $Ip;
  100. }
  101. }