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. '退款成功截图' => 4,
  25. ],
  26. ];
  27. function __construct(array $attributes = [])
  28. {
  29. foreach (self::$enums as &$enum) {
  30. $enum = $enum + array_flip($enum);
  31. }
  32. parent::__construct($attributes);
  33. }
  34. public function getStatusAttribute($value)
  35. {
  36. if (!$value) return '';
  37. return self::$enums['type'][$value];
  38. }
  39. public function setTypeAttribute($value)
  40. {
  41. if (!$value) return ;
  42. if (is_numeric($value)) {
  43. $this->attributes['type'] = $value;
  44. } else {
  45. $this->attributes['type'] = self::$enums['type'][$value];
  46. }
  47. }
  48. public function workOrder(): BelongsTo
  49. {
  50. return $this->belongsTo(WorkOrder::class);
  51. }
  52. public function uploadFile(): HasOne
  53. {
  54. return $this->hasOne(UploadFile::class,'table_id','id')->where('table_name',$this->getTable());
  55. }
  56. public function saveFile(UploadedFile $image): bool
  57. {
  58. if (!$this->checkFile($image)) return false;
  59. $tmpFile = $image->getRealPath();
  60. $fileSuffix = $image->getClientOriginalExtension();
  61. $dirPath = $this->getStorageDirPath();
  62. $fileName = date('ymd') . '-' . Uuid::uuid1();
  63. $pathName = $dirPath . $fileName .'.' . $fileSuffix;
  64. $result = move_uploaded_file($tmpFile, $pathName);
  65. if(!$result) return false;
  66. $this->uploadFile()->create(
  67. [ 'url' => '/files/workOrder/'.$fileName, 'type' => $fileSuffix,'table_name'=>$this->getTable()]
  68. );
  69. return true;
  70. }
  71. public function checkFile($image): bool
  72. {
  73. $tmpFile = $image->getRealPath();
  74. $fileSuffix = $image->getClientOriginalExtension();
  75. if (!is_uploaded_file($tmpFile)) return false;
  76. if ($image->getSize() > 5 * 1024 * 1024) return false;
  77. if (!in_array($fileSuffix,[ 'gif','image','jpeg','jpg','png','svg'])) return false;
  78. return true;
  79. }
  80. public function getStorageDirPath(): string
  81. {
  82. $path = ['app','public','files','workOrder'];
  83. $path = join(DIRECTORY_SEPARATOR,$path);
  84. $dirPath = storage_path($path);
  85. if (!file_exists($dirPath)) {
  86. mkdir($dirPath);
  87. }
  88. return $dirPath.DIRECTORY_SEPARATOR;
  89. }
  90. public function updateFile($image)
  91. {
  92. $this->deleteStorageFile();
  93. $this->uploadFile()->delete();
  94. $this->saveFile($image);
  95. }
  96. public function deleteStorageFile()
  97. {
  98. $path = $this->getPathName();
  99. if (!$path) return;
  100. if (file_exists($path)){
  101. unlink($path);
  102. }
  103. }
  104. public function getPathName(): ?string
  105. {
  106. if (!$this->uploadFile) return null;
  107. $names = [];
  108. preg_match("/([\w]+)$/", $this->uploadFile->url,$names);
  109. $path = ['app','public','files','workOrder' , $names[0],".{$this->uploadFile->type}"];
  110. $path = join(DIRECTORY_SEPARATOR,$path);
  111. return storage_path($path);
  112. }
  113. }