App_Tools_CommonService.proxy.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. if (!in_array($ext, $acceptExt)) {
  24. return Result::error('文件名后缀不允许');
  25. }
  26. //图片检测安全
  27. if ($fileType == 'image') {
  28. $res = self::checkMimeType($uploadedFile, $ext);
  29. if ($res == false) {
  30. return Result::error('文件安全检测未通过');
  31. }
  32. }
  33. $date = date('Ymd');
  34. $filePath = $fileType . DIRECTORY_SEPARATOR . $date;
  35. $allDir = 'public' . DIRECTORY_SEPARATOR . $filePath;
  36. if (!is_dir($allDir)) {
  37. if (!mkdir($allDir, 0755, true)) {
  38. return Result::error('创建文件夹失败');
  39. }
  40. }
  41. $fileName = time() . mt_rand(1, 1000000) . '.' . $ext;
  42. $finalPath = BASE_PATH . DIRECTORY_SEPARATOR . $allDir . DIRECTORY_SEPARATOR . $fileName;
  43. $showPath = $filePath . DIRECTORY_SEPARATOR . $fileName;
  44. $uploadedFile->moveTo($finalPath);
  45. return ['id' => uniqid(), 'src' => $showPath, 'fileName' => $fileName, 'fileType' => $ext];
  46. }
  47. //检测文件是否合法
  48. public static function checkMimeType(UploadedFile $uploadedFile, string $ext = '')
  49. {
  50. try {
  51. $filePath = $uploadedFile->getRealPath();
  52. $fileMimeType = mime_content_type($filePath);
  53. $mimeTypes = MimeTypes::getImage();
  54. $isExist = array_key_exists($fileMimeType, $mimeTypes);
  55. if (!$isExist) {
  56. return Result::error('非允许mime types类型');
  57. }
  58. list($width, $height, $type, $attr) = getimagesize($filePath, $ext);
  59. if ($width <= 0 || $height <= 0) {
  60. return false;
  61. } else {
  62. return true;
  63. }
  64. } catch (\Exception $e) {
  65. return false;
  66. }
  67. }
  68. public function imgUrl(string $str)
  69. {
  70. $scheme = $this->request->getUri()->getScheme() ?? 'http';
  71. $host = $this->request->getUri()->getHost() ?? '127.0.0.1';
  72. $port = $this->request->getUri()->getPort() ?? '';
  73. $url = '';
  74. if ($port == 80 || $port == 443 || empty($port)) {
  75. $url = "{$scheme}://{$host}/{$str}";
  76. } else {
  77. $url = "{$scheme}://{$host}:{$port}/{$str}";
  78. }
  79. return $url;
  80. }
  81. /**
  82. * 获取用户IP
  83. * @return mixed|string
  84. */
  85. public function userIp()
  86. {
  87. $Ip = $this->request->getHeader('x-forwarded-for')[0] ?? $this->request->getHeader('x-real-ip')[0] ?? $this->request->getServerParams()['remote_addr'] ?? '0.0.0.0';
  88. return $Ip;
  89. }
  90. }