WorkOrderImage.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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',$this->getTable());
  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. $this->uploadFile()->create(
  66. [ 'url' => '/files/workOrder/'.$fileName, 'type' => $fileSuffix,'table_name'=>$this->getTable()]
  67. );
  68. return true;
  69. }
  70. public function checkFile($image): bool
  71. {
  72. $tmpFile = $image->getRealPath();
  73. $fileSuffix = $image->getClientOriginalExtension();
  74. if (!is_uploaded_file($tmpFile)) return false;
  75. if ($image->getSize() > 5 * 1024 * 1024) return false;
  76. if (!in_array($fileSuffix,[ 'gif','image','jpeg','jpg','png','svg'])) return false;
  77. return true;
  78. }
  79. public function getStorageDirPath(): string
  80. {
  81. $path = ['app','public','files','workOrder'];
  82. $path = join(DIRECTORY_SEPARATOR,$path);
  83. $dirPath = storage_path($path);
  84. if (!file_exists($dirPath)) {
  85. mkdir($dirPath);
  86. }
  87. return $dirPath.DIRECTORY_SEPARATOR;
  88. }
  89. public function updateFile($image)
  90. {
  91. $this->deleteStorageFile();
  92. $this->uploadFile()->delete();
  93. $this->saveFile($image);
  94. }
  95. public function deleteStorageFile()
  96. {
  97. $path = $this->getPathName();
  98. if (!$path) return;
  99. if (file_exists($path)){
  100. unlink($path);
  101. }
  102. }
  103. public function getPathName(): ?string
  104. {
  105. if (!$this->uploadFile) return null;
  106. $names = [];
  107. preg_match("/([\w]+)$/", $this->uploadFile->url,$names);
  108. $path = ['app','public','files','workOrder' , $names[0],".{$this->uploadFile->type}"];
  109. $path = join(DIRECTORY_SEPARATOR,$path);
  110. return storage_path($path);
  111. }
  112. }