moshaorui hace 5 meses
padre
commit
5a45d89f4c

+ 1 - 5
app/Admin/Controllers/BaseProductController.php

@@ -51,10 +51,6 @@ class BaseProductController extends AdminController
             });
             //排序
            $grid->model()->orderBy("order",'asc')->orderBy('id', 'desc');
-            // 禁用查看按钮
-            //$grid->showQuickEditButton();
-            //$grid->enableDialogCreate();
-            //$grid->disableEditButton();
         });
     }
 
@@ -137,6 +133,7 @@ class BaseProductController extends AdminController
             $form->multipleImage('images', 'images')
                 ->sortable() // 可拖动排序
                 ->removable() // 可移除图片
+                ->autoUpload() // 自动上传
                 ->customFormat(function () {
                     // 数据格式化为数组['1.jpg','2.jpg']
                     return array_column($this->images, 'image_url');
@@ -160,7 +157,6 @@ class BaseProductController extends AdminController
                 BaseProductImage::saveProductImages($productId, $images);
             });
         });
-
     }
 
     /*

+ 9 - 17
app/Admin/Repositories/BaseProduct.php

@@ -16,22 +16,14 @@ class BaseProduct extends EloquentRepository
     protected $eloquentClass = Model::class;
 
     /*
-     * 重写保存方法
+     * 重写删除方法
      */
-//    public function store(Form $form)
-//    {
-//        exit;
-//        $form->ignore(['images']);
-//        return parent::store($form);
-//    }
-//
-//    public function update(Form $form)
-//    {
-//        echo ' bb---------';
-//        $form->prepare('images');
-//        echo ' ---updates------';
-//        var_dump($form->updates());
-//        exit;
-//        return parent::store($form);
-//    }
+    public function delete(Form $form, array $originalData)
+    {
+        collect(explode(',', $form->getKey()))->filter()->each(function ($id) {
+            Model::find($id)->images()->delete();
+            Model::find($id)->delete();
+        });
+        return true;
+    }
 }

+ 3 - 0
app/Admin/Repositories/BaseProductImage.php

@@ -23,6 +23,9 @@ class BaseProductImage extends EloquentRepository
 
     public static function saveProductImages($productId, $imageUrls)
     {
+        if (empty($productId) || empty($imageUrl)) {
+            return false;
+        }
         // 准备数据集合
         $data = [];
         foreach ($imageUrls as $imageUrl) {

+ 13 - 3
app/Models/BaseProduct.php

@@ -4,14 +4,24 @@ namespace App\Models;
 
 use Dcat\Admin\Traits\HasDateTimeFormatter;
 
+use Dcat\Admin\Traits\ModelTree;
 use Illuminate\Database\Eloquent\Model;
+use Spatie\EloquentSortable\Sortable;
+use Spatie\EloquentSortable\SortableTrait;
 
-class BaseProduct extends Model
+class BaseProduct extends Model implements Sortable
 {
-	use HasDateTimeFormatter;
+    use HasDateTimeFormatter;
+    use SortableTrait;
+
     protected $table = 'base_product';
 
-    protected  $fillable = ['id', 'title', 'keywords', 'description', 'sku', 'category_id', 'issuance_date', 'order', 'enabled', 'content', 'parameters', 'created_at', 'updated_at'];
+    // 可选:你可以在这里自定义排序配置
+    public $sortable = [
+        'order_column_name' => 'order',  // 排序字段
+        'sort_when_creating' => false,    // 创建时自动排序
+    ];
+
     protected $casts = [
         'created_at' => 'datetime:Y-m-d H:i:s',
         'updated_at' => 'datetime:Y-m-d H:i:s',

+ 3 - 1
app/Models/BaseProductParameter.php

@@ -2,12 +2,14 @@
 
 namespace App\Models;
 
+
 use Dcat\Admin\Traits\HasDateTimeFormatter;
 
 use Dcat\Admin\Traits\ModelTree;
 use Illuminate\Database\Eloquent\Model;
 use Spatie\EloquentSortable\Sortable;
 use Spatie\EloquentSortable\SortableTrait;
+use Illuminate\Database\Eloquent\Builder;
 
 class BaseProductParameter extends Model implements Sortable
 {
@@ -21,7 +23,7 @@ class BaseProductParameter extends Model implements Sortable
         'content' => 'json', // 将 attributes 字段转换为数组
     ];
 
-    protected $fillable = ['name', 'order'];
+    //protected $fillable = ['name', 'order'];
 
     // 可选:你可以在这里自定义排序配置
     public $sortable = [