WorkOrderImage.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. 'order_issue_type_id', // 工单异常类型
  16. 'work_order_detail_id', // 工单详情
  17. 'type', // 类型 ------------------------
  18. // 1: 外包装图片
  19. // 2: 内物破碎图片
  20. // 3: 交易截图
  21. // 4: 退款成功截图 ( 需带有付款时间,快递单号,内物详情和实付款的交易截图)
  22. // ------------------------
  23. 'number', // 编号
  24. 'tag', // 当前状态
  25. ];
  26. static public $enums = [
  27. 'type' => [
  28. '' => 0,
  29. '外包装图片' => 1,
  30. '内物破碎图片' => 2,
  31. '交易截图' => 3,
  32. '退款成功截图' => 4,
  33. ],
  34. 'tag' => [
  35. '' => 0,
  36. '完成' => 1,
  37. '标记' => 2,
  38. ],
  39. ];
  40. function __construct(array $attributes = [])
  41. {
  42. foreach (self::$enums as &$enum) {
  43. $enum = $enum + array_flip($enum);
  44. }
  45. parent::__construct($attributes);
  46. }
  47. public function getTypeAttribute($value)
  48. {
  49. if (!$value) return '';
  50. return self::$enums['type'][$value];
  51. }
  52. public function setTypeAttribute($value)
  53. {
  54. if (!$value) return ;
  55. if (is_numeric($value)) {
  56. $this->attributes['type'] = $value;
  57. } else {
  58. $this->attributes['type'] = self::$enums['type'][$value];
  59. }
  60. }
  61. public function getStatusAttribute($value)
  62. {
  63. if (!$value) return '';
  64. return self::$enums['status'][$value];
  65. }
  66. public function setStatusAttribute($value)
  67. {
  68. if (!$value) return;
  69. if (is_numeric($value)) {
  70. $this->attributes['status'] = $value;
  71. } else {
  72. $this->attributes['status'] = self::$enums['status'][$value];
  73. }
  74. }
  75. public function workOrder(): BelongsTo
  76. {
  77. return $this->belongsTo(WorkOrder::class);
  78. }
  79. public function uploadFile(): HasOne
  80. {
  81. return $this->hasOne(UploadFile::class,'table_id','id')->where('table_name',$this->getTable());
  82. }
  83. public function saveFile(UploadedFile $image): bool
  84. {
  85. if (!$this->checkFile($image)) return false;
  86. $tmpFile = $image->getRealPath();
  87. $fileSuffix = $image->getClientOriginalExtension();
  88. $dirPath = $this->getStorageDirPath();
  89. $fileName = date('ymd') . '-' . Uuid::uuid1();
  90. $pathName = $dirPath . $fileName .'.' . $fileSuffix;
  91. $result = move_uploaded_file($tmpFile, $pathName);
  92. if(!$result) return false;
  93. $this->uploadFile()->create(
  94. [ 'url' => '/files/workOrder/'.$fileName, 'type' => $fileSuffix,'table_name'=>$this->getTable()]
  95. );
  96. return true;
  97. }
  98. public function checkFile($image): bool
  99. {
  100. $tmpFile = $image->getRealPath();
  101. $fileSuffix = $image->getClientOriginalExtension();
  102. if (!is_uploaded_file($tmpFile)) return false;
  103. // if ($image->getSize() > 5 * 1024 * 1024) return false;
  104. if (!in_array($fileSuffix,[ 'gif','image','jpeg','jpg','png','svg'])) return false;
  105. return true;
  106. }
  107. public function getStorageDirPath(): string
  108. {
  109. $path = ['app','public','files','workOrder'];
  110. $path = join(DIRECTORY_SEPARATOR,$path);
  111. $dirPath = storage_path($path);
  112. if (!file_exists($dirPath)) {
  113. mkdir($dirPath);
  114. }
  115. return $dirPath.DIRECTORY_SEPARATOR;
  116. }
  117. public function updateFile($image)
  118. {
  119. $this->deleteStorageFile();
  120. $this->uploadFile()->delete();
  121. $this->saveFile($image);
  122. }
  123. public function deleteStorageFile()
  124. {
  125. $path = $this->getPathName();
  126. if (!$path) return;
  127. if (file_exists($path)){
  128. unlink($path);
  129. }
  130. }
  131. public function getPathName(): ?string
  132. {
  133. if (!$this->uploadFile) return null;
  134. $names = [];
  135. preg_match("/([\w]+)$/", $this->uploadFile->url,$names);
  136. $path = ['app','public','files','workOrder' , $names[0],".{$this->uploadFile->type}"];
  137. $path = join(DIRECTORY_SEPARATOR,$path);
  138. return storage_path($path);
  139. }
  140. }