WorkOrderImage.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 getTagAttribute($value)
  62. {
  63. if($value == 0) return self::$enums['tag'][$value];
  64. if (!$value) return '';
  65. return self::$enums['tag'][$value];
  66. }
  67. public function setTagAttribute($value)
  68. {
  69. if (!$value) return;
  70. if (is_numeric($value)) {
  71. $this->attributes['tag'] = $value;
  72. } else {
  73. $this->attributes['tag'] = self::$enums['tag'][$value];
  74. }
  75. }
  76. public function workOrder(): BelongsTo
  77. {
  78. return $this->belongsTo(WorkOrder::class);
  79. }
  80. public function uploadFile(): HasOne
  81. {
  82. return $this->hasOne(UploadFile::class,'table_id','id')->where('table_name',$this->getTable());
  83. }
  84. public function saveFile(UploadedFile $image): bool
  85. {
  86. if (!$this->checkFile($image)) return false;
  87. $tmpFile = $image->getRealPath();
  88. $fileSuffix = $image->getClientOriginalExtension();
  89. $dirPath = $this->getStorageDirPath();
  90. $fileName = date('ymd') . '-' . Uuid::uuid1();
  91. $pathName = $dirPath . $fileName .'.' . $fileSuffix;
  92. $result = move_uploaded_file($tmpFile, $pathName);
  93. if(!$result) return false;
  94. $this->uploadFile()->create(
  95. [ 'url' => '/files/workOrder/'.$fileName, 'type' => $fileSuffix,'table_name'=>$this->getTable()]
  96. );
  97. return true;
  98. }
  99. public function checkFile($image): bool
  100. {
  101. $tmpFile = $image->getRealPath();
  102. $fileSuffix = $image->getClientOriginalExtension();
  103. if (!is_uploaded_file($tmpFile)) return false;
  104. // if ($image->getSize() > 5 * 1024 * 1024) return false;
  105. if (!in_array($fileSuffix,[ 'gif','image','jpeg','jpg','png','svg'])) return false;
  106. return true;
  107. }
  108. public function getStorageDirPath(): string
  109. {
  110. $path = ['app','public','files','workOrder'];
  111. $path = join(DIRECTORY_SEPARATOR,$path);
  112. $dirPath = storage_path($path);
  113. if (!file_exists($dirPath)) {
  114. mkdir($dirPath);
  115. }
  116. return $dirPath.DIRECTORY_SEPARATOR;
  117. }
  118. public function updateFile($image)
  119. {
  120. $this->deleteStorageFile();
  121. $this->uploadFile()->delete();
  122. $this->saveFile($image);
  123. }
  124. public function deleteStorageFile()
  125. {
  126. $path = $this->getPathName();
  127. if (!$path) return;
  128. if (file_exists($path)){
  129. unlink($path);
  130. }
  131. }
  132. public function getPathName(): ?string
  133. {
  134. if (!$this->uploadFile) return null;
  135. $names = [];
  136. preg_match("/([\w]+)$/", $this->uploadFile->url,$names);
  137. $path = ['app','public','files','workOrder' , $names[0],".{$this->uploadFile->type}"];
  138. $path = join(DIRECTORY_SEPARATOR,$path);
  139. return storage_path($path);
  140. }
  141. }