WorkOrderImage.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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\Support\Str;
  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. '退款成功截图' => 2,
  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($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. if ($image->height() > $image->width()) {
  64. $image->heighten(250)->save($pathName);
  65. } else {
  66. $image->widen(250)->save($pathName);
  67. }
  68. $result = move_uploaded_file($tmpFile, $pathName);
  69. $img = Image::make($result);
  70. $img->heighten(250)->save($pathName);
  71. $this->uploadFile()->create(
  72. ['table_name' => $this->getTable(), 'table_id' => $this['id'], 'url' => '/files/workOrder/'.$fileName, 'type' => $fileSuffix]
  73. );
  74. // UploadFile::query()->create(['table_name' => $this->getTable(), 'table_id' => $this['id'], 'url' => '/files/workOrder/'.$fileName, 'type' => $fileSuffix]);
  75. return true;
  76. }
  77. public function checkFile($image): bool
  78. {
  79. $tmpFile = $image->getRealPath();
  80. $fileSuffix = $image->getClientOriginalExtension();
  81. if (!is_uploaded_file($tmpFile)) return false;
  82. if ($image->getSize() > 5 * 1024 * 1024) return false;
  83. if (!in_array($fileSuffix,[ 'gif','image','jpeg','jpg','png','svg'])) return false;
  84. return true;
  85. }
  86. public function getStorageDirPath(): string
  87. {
  88. $path = ['app','public','files','workOrder'];
  89. $path = join(DIRECTORY_SEPARATOR,$path);
  90. $dirPath = storage_path($path);
  91. if (!file_exists($dirPath)) {
  92. mkdir($dirPath);
  93. }
  94. return $dirPath;
  95. }
  96. public function updateFile($image)
  97. {
  98. $this->deleteStorageFile();
  99. $this->uploadFile()->delete();
  100. $this->saveFile($image);
  101. }
  102. public function deleteStorageFile()
  103. {
  104. $path = $this->getPathName();
  105. if (!$path) return;
  106. if (file_exists($path)){
  107. unlink($path);
  108. }
  109. }
  110. public function getPathName(): ?string
  111. {
  112. if (!$this->uploadFile) return null;
  113. $names = [];
  114. preg_match("/([\w]+)$/", $this->uploadFile->url,$names);
  115. $path = ['app','public','files','workOrder' , $names[0],".{$this->uploadFile->type}"];
  116. $path = join(DIRECTORY_SEPARATOR,$path);
  117. return storage_path($path);
  118. }
  119. }