| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?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\Http\UploadedFile;
- use Intervention\Image\Facades\Image;
- use Ramsey\Uuid\Uuid;
- class WorkOrderImage extends Model
- {
- use ModelLogChanging;
- protected $fillable = [
- 'work_order_id',
- 'order_issue_type_id', // 工单异常类型
- 'work_order_detail_id', // 工单详情
- 'type', // 类型 ------------------------
- // 1: 外包装图片
- // 2: 内物破碎图片
- // 3: 交易截图
- // 4: 退款成功截图 ( 需带有付款时间,快递单号,内物详情和实付款的交易截图)
- // ------------------------
- 'number', // 编号
- 'tag', // 当前状态
- ];
- static public $enums = [
- 'type' => [
- '' => 0,
- '外包装图片' => 1,
- '内物破碎图片' => 2,
- '交易截图' => 3,
- '退款成功截图' => 4,
- ],
- 'tag' => [
- '创建' => 0,
- '完成' => 1,
- '标记' => 2,
- ],
- ];
- function __construct(array $attributes = [])
- {
- foreach (self::$enums as &$enum) {
- $enum = $enum + array_flip($enum);
- }
- parent::__construct($attributes);
- }
- public function getTypeAttribute($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 getTagAttribute($value)
- {
- if($value == 0) return self::$enums['tag'][$value];
- if (!$value) return '';
- return self::$enums['tag'][$value];
- }
- public function setTagAttribute($value)
- {
- if (!$value) return;
- if (is_numeric($value)) {
- $this->attributes['tag'] = $value;
- } else {
- $this->attributes['tag'] = self::$enums['tag'][$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',$this->getTable());
- }
- public function saveFile(UploadedFile $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;
- $result = move_uploaded_file($tmpFile, $pathName);
- if(!$result) return false;
- $this->uploadFile()->create(
- [ 'url' => '/files/workOrder/'.$fileName, 'type' => $fileSuffix,'table_name'=>$this->getTable()]
- );
- 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.DIRECTORY_SEPARATOR;
- return "";
- }
- 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);
- }
- }
|