Selaa lähdekoodia

组件添加图片

ajun 4 vuotta sitten
vanhempi
commit
acb7fc1375

+ 44 - 0
app/Http/Controllers/PrintPartImageController.php

@@ -0,0 +1,44 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\PrintPartImage;
+use App\Services\PrintPartImageService;
+use App\UploadFile;
+use Illuminate\Http\Request;
+
+class PrintPartImageController extends Controller
+{
+
+    public function index(Request $request)
+    {
+        $printPartImages = PrintPartImage::query()->with('file')->get();
+        return view('print.image.index', compact('printPartImages'));
+    }
+
+    public function saveFileApi(Request $request, PrintPartImageService $service): array
+    {
+        $file = $request->file('file');
+        /** @var PrintPartImage $printPartImage */
+        $printPartImage = PrintPartImage::query()->with('file')->where('name', $request['name'])->first();
+        if (!$printPartImage->file) return ['success' => false, 'message' => '该需求已有描述图片'];
+        return $service->saveFile($printPartImage, $file);
+    }
+
+    public function updateFileApi(Request $request, PrintPartImageService $service): array
+    {
+        $file = $request->file('file');
+        /** @var PrintPartImage $printPartImage */
+        $printPartImage = PrintPartImage::query()->with('file')->where('name', $request['name'])->first();
+        return $service->saveFile($printPartImage, $file);
+    }
+
+    public function destroyApi($id, PrintPartImageService $service): array
+    {
+        $item = PrintPartImage::query()->find($id);
+        UploadFile::query()->where(['table_name' => (new PrintPartImage())->getTable(), 'table_id' => $item['id']])->delete();
+        $item->delete();
+        return ['success' => true];
+    }
+
+}

+ 25 - 0
app/PrintPartImage.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+use App\Traits\ModelLogChanging;
+use Illuminate\Database\Eloquent\Relations\HasOne;
+
+class PrintPartImage extends Model
+{
+    use ModelLogChanging;
+
+    protected $fillable = ['name'];
+
+    public function file(): HasOne
+    {
+        return $this->hasOne(UploadFile::class,'table_id','id')->where('table_name','print_part_image');
+    }
+
+    public function saveFile($file){
+        $fileSuffix=strtolower($file->getClientOriginalExtension());
+        return UploadFile::query()->updateOrCreate(['table_name' => $this->getTable(), 'table_id' => $this['id'], 'url' => '/files/partImage/'.$this['name'], 'type' => $fileSuffix]);
+    }
+}

+ 39 - 0
app/Services/PrintPartImageService.php

@@ -0,0 +1,39 @@
+<?php
+
+namespace App\Services;
+
+use App\Traits\ServiceAppAop;
+use App\PrintPartImage;
+
+class PrintPartImageService
+{
+    use ServiceAppAop;
+    protected $modelClass=PrintPartImage::class;
+
+    public function  saveFile(PrintPartImage $model,$file): array
+    {
+        $tmpFil = $file->getRealPath();
+        if (!$file) return ['success' => false, 'message' => '上传图片为找到'];
+        if (!$file->isValid()) return ['success' => false, 'message' => '找不到上传图片'];
+        if (!is_uploaded_file($tmpFil)) return ['success' => false, 'message' => '文件错误'];
+        if ($file->getSize() > 5 * 1024 * 1024) return ['success' => false, 'message' => '文件不能大于5MB'];
+
+        $dirPath = storage_path('app\public\files\partImage');
+        if (!file_exists($dirPath)) {
+            mkdir($dirPath);
+        }
+        $fileSuffix = $file->getClientOriginalExtension();
+
+        $filePath = storage_path('app\public\files\partImage\\' . $model['name']  . $fileSuffix);
+
+        $result = move_uploaded_file($tmpFil,$filePath);
+
+        if ($result == false) return ['success' => false, 'data' => '文件上传失败'];
+
+        $model->saveFile($file);
+        $model->load('file');
+
+        return ['success' => true, 'data' => '文件上传成功'];
+    }
+
+}