| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace App;
- use Illuminate\Database\Eloquent\Model;
- use App\Traits\ModelLogChanging;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasOne;
- use Illuminate\Support\Str;
- use Intervention\Image\Facades\Image;
- use Ramsey\Uuid\Uuid;
- class WorkOrderImage extends Model
- {
- use ModelLogChanging;
- protected $fillable = [
- 'work_order_id',
- 'type',
- 'number',
- ];
- static public $enums = [
- 'type' => [
- '外包装图片' => 0,
- '内物破碎图片' => 1,
- '交易截图' => 2,
- '退款成功截图' => 2,
- ],
- ];
- function __construct(array $attributes = [])
- {
- foreach (self::$enums as &$enum) {
- $enum = $enum + array_flip($enum);
- }
- parent::__construct($attributes);
- }
- public function getStatusAttribute($value)
- {
- if (!$value) return '';
- return self::$enums['type'][$value];
- }
- public function setTypeAttribute($value)
- {
- if (!$value) return ;
- if (is_numeric($value)) {
- $this->attributes['type'] = $value;
- } else {
- $this->attributes['type'] = self::$enums['type'][$value];
- }
- }
- public function workOrder(): BelongsTo
- {
- return $this->belongsTo(WorkOrder::class);
- }
- public function uploadFile(): HasOne
- {
- return $this->hasOne(UploadFile::class,'table_id','id')->where('table_name','work_order_image');
- }
- public function saveFile($image): bool
- {
- if (!$this->checkFile($image)) return false;
- $tmpFile = $image->getRealPath();
- $fileSuffix = $image->getClientOriginalExtension();
- $dirPath = $this->getStorageDirPath();
- $fileName = date('ymd') . '-' . Uuid::uuid1();
- $pathName = $dirPath . $fileName . $fileSuffix;
- if ($image->height() > $image->width()) {
- $image->heighten(250)->save($pathName);
- } else {
- $image->widen(250)->save($pathName);
- }
- $result = move_uploaded_file($tmpFile, $pathName);
- $img = Image::make($result);
- $img->heighten(250)->save($pathName);
- $this->uploadFile()->create(
- ['table_name' => $this->getTable(), 'table_id' => $this['id'], 'url' => '/files/workOrder/'.$fileName, 'type' => $fileSuffix]
- );
- // UploadFile::query()->create(['table_name' => $this->getTable(), 'table_id' => $this['id'], 'url' => '/files/workOrder/'.$fileName, 'type' => $fileSuffix]);
- return true;
- }
- public function checkFile($image): bool
- {
- $tmpFile = $image->getRealPath();
- $fileSuffix = $image->getClientOriginalExtension();
- if (!is_uploaded_file($tmpFile)) return false;
- if ($image->getSize() > 5 * 1024 * 1024) return false;
- if (!in_array($fileSuffix,[ 'gif','image','jpeg','jpg','png','svg'])) return false;
- return true;
- }
- public function getStorageDirPath(): string
- {
- $path = ['app','public','files','workOrder'];
- $path = join(DIRECTORY_SEPARATOR,$path);
- $dirPath = storage_path($path);
- if (!file_exists($dirPath)) {
- mkdir($dirPath);
- }
- return $dirPath;
- }
- public function updateFile($image)
- {
- $this->deleteStorageFile();
- $this->uploadFile()->delete();
- $this->saveFile($image);
- }
- public function deleteStorageFile()
- {
- $path = $this->getPathName();
- if (!$path) return;
- if (file_exists($path)){
- unlink($path);
- }
- }
- public function getPathName(): ?string
- {
- if (!$this->uploadFile) return null;
- $names = [];
- preg_match("/([\w]+)$/", $this->uploadFile->url,$names);
- $path = ['app','public','files','workOrder' , $names[0],".{$this->uploadFile->type}"];
- $path = join(DIRECTORY_SEPARATOR,$path);
- return storage_path($path);
- }
- }
|