WorkOrderImage.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Traits\ModelLogChanging;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. use Illuminate\Database\Eloquent\Relations\HasOne;
  7. use Illuminate\Http\UploadedFile;
  8. use Intervention\Image\Facades\Image;
  9. use Ramsey\Uuid\Uuid;
  10. class WorkOrderImage extends Model
  11. {
  12. use ModelLogChanging;
  13. protected $fillable = [
  14. 'work_order_id',
  15. 'type',
  16. 'number',
  17. ];
  18. static public $enums = [
  19. 'type' => [
  20. '外包装图片' => 0,
  21. '内物破碎图片' => 1,
  22. '交易截图' => 2,
  23. '退款成功截图' => 3,
  24. ],
  25. ];
  26. function __construct(array $attributes = [])
  27. {
  28. foreach (self::$enums as &$enum) {
  29. $enum = $enum + array_flip($enum);
  30. }
  31. parent::__construct($attributes);
  32. }
  33. public function getStatusAttribute($value)
  34. {
  35. if (!$value) return '';
  36. return self::$enums['type'][$value];
  37. }
  38. public function setTypeAttribute($value)
  39. {
  40. if (!$value) return ;
  41. if (is_numeric($value)) {
  42. $this->attributes['type'] = $value;
  43. } else {
  44. $this->attributes['type'] = self::$enums['type'][$value];
  45. }
  46. }
  47. public function workOrder(): BelongsTo
  48. {
  49. return $this->belongsTo(WorkOrder::class);
  50. }
  51. public function uploadFile(): HasOne
  52. {
  53. return $this->hasOne(UploadFile::class,'table_id','id')->where('table_name','work_order_image');
  54. }
  55. public function saveFile(UploadedFile $image): bool
  56. {
  57. if (!$this->checkFile($image)) return false;
  58. $tmpFile = $image->getRealPath();
  59. $fileSuffix = $image->getClientOriginalExtension();
  60. $dirPath = $this->getStorageDirPath();
  61. $fileName = date('ymd') . '-' . Uuid::uuid1();
  62. $pathName = $dirPath . $fileName .'.' . $fileSuffix;
  63. $result = move_uploaded_file($tmpFile, $pathName);
  64. if ($result) return false;
  65. $img = Image::make($pathName);
  66. $img->save($pathName);
  67. $this->uploadFile()->create(
  68. ['table_name' => $this->getTable(), 'table_id' => $this['id'], 'url' => '/files/workOrder/'.$fileName, 'type' => $fileSuffix]
  69. );
  70. return true;
  71. }
  72. public function checkFile($image): bool
  73. {
  74. $tmpFile = $image->getRealPath();
  75. $fileSuffix = $image->getClientOriginalExtension();
  76. if (!is_uploaded_file($tmpFile)) return false;
  77. if ($image->getSize() > 5 * 1024 * 1024) return false;
  78. if (!in_array($fileSuffix,[ 'gif','image','jpeg','jpg','png','svg'])) return false;
  79. return true;
  80. }
  81. public function getStorageDirPath(): string
  82. {
  83. $path = ['app','public','files','workOrder'];
  84. $path = join(DIRECTORY_SEPARATOR,$path);
  85. $dirPath = storage_path($path);
  86. if (!file_exists($dirPath)) {
  87. mkdir($dirPath);
  88. }
  89. return $dirPath.DIRECTORY_SEPARATOR;
  90. }
  91. public function updateFile($image)
  92. {
  93. $this->deleteStorageFile();
  94. $this->uploadFile()->delete();
  95. $this->saveFile($image);
  96. }
  97. public function deleteStorageFile()
  98. {
  99. $path = $this->getPathName();
  100. if (!$path) return;
  101. if (file_exists($path)){
  102. unlink($path);
  103. }
  104. }
  105. public function getPathName(): ?string
  106. {
  107. if (!$this->uploadFile) return null;
  108. $names = [];
  109. preg_match("/([\w]+)$/", $this->uploadFile->url,$names);
  110. $path = ['app','public','files','workOrder' , $names[0],".{$this->uploadFile->type}"];
  111. $path = join(DIRECTORY_SEPARATOR,$path);
  112. return storage_path($path);
  113. }
  114. }