Przeglądaj źródła

Merge remote-tracking branch 'origin/master'

igb 5 miesięcy temu
rodzic
commit
d7b2975575

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

@@ -41,7 +41,7 @@ class BaseProductController extends AdminController
             $grid->column('images')->display(function ($images) {
                 $images = $images->toArray();
                 $dataImages = array_column($images, 'image_url');
-                return CommonHelper::displayImage($dataImages,150);
+                return CommonHelper::displayImage($dataImages,100);
             });
             $grid->column('order')->orderable();
             $grid->column('is_pinned')->switch();
@@ -164,6 +164,13 @@ class BaseProductController extends AdminController
                 if ($count > 0) {
                     return $form->response()->error('sku already exists');
                 }
+                //保存前回调删除图片
+                if (!$form->isCreating()) {
+                    //清空图片
+                    $id = $form->getKey();
+                    $baseProductImage = new BaseProductImage();
+                    $baseProductImage->model()->where('product_id', $id)->delete();
+                }
             });
         });
     }

+ 1 - 1
app/Admin/Controllers/BaseVideoController.php

@@ -40,7 +40,7 @@ class BaseVideoController extends AdminController
             $grid->column('cover_image')->display(function ($image) {
                 // 开始生成 HTML
                 $dataImages = [$image];
-                return CommonHelper::displayImage($dataImages,150);
+                return CommonHelper::displayImage($dataImages,100);
             });
             $grid->column('order')->orderable();
             $grid->column('is_pinned')->switch();

+ 1 - 1
app/Admin/Repositories/BaseProduct.php

@@ -21,7 +21,7 @@ class BaseProduct extends EloquentRepository
     public function delete(Form $form, array $originalData)
     {
         collect(explode(',', $form->getKey()))->filter()->each(function ($id) {
-            Model::find($id)->images()->delete();
+            Model::find($id)->images()->delete();//把图片也删了
             Model::find($id)->delete();
         });
         return true;

+ 12 - 7
app/Libraries/CommonHelper.php

@@ -10,17 +10,22 @@ class CommonHelper
      * $images 格式:['image.jpg','image2.jpg']
      * 返回显示的HTML显示图片
      */
-    public static function displayImage($images,$size=150)
+    public static function displayImage($images,$boxSize=100,$imgSize=300)
     {
-        //默认用等比例缩放
-        $process = "?x-oss-process=image/resize,h_{$size},m_lfit";
-        $html = '<div style="display: flex; flex-wrap: wrap; gap: 5px;">';
-        foreach ($images as $image) {
-            $html .= "<div style='flex: 1 0 {$size}px; max-width: {$size}px; max-height: {$size}px; padding: 5px; border: 1px solid #ddd; border-radius: 5px; background-color: #f9f9f9; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); display: flex; align-items: center; justify-content: center;'>
+        if (empty($images) || $images[0] == null) {
+            $html = "";
+        } else {
+            //默认用等比例缩放
+            $process = "?x-oss-process=image/resize,h_{$imgSize},m_lfit";
+            $html = '<div style="display: flex; flex-wrap: wrap; gap: 5px;">';
+            foreach ($images as $image) {
+                $html .= "<div style='flex: 1 0 {$boxSize}px; width: {$boxSize}px; height: {$boxSize}px; padding: 5px; border: 1px solid #ddd; border-radius: 5px; background-color: #f9f9f9; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); display: flex; align-items: center; justify-content: center;'>
                     <img  data-action='preview-img'   src='" . self::ossUrl($image).$process . "' style='max-width: 100%; max-height: 100%; object-fit: contain;'>
                   </div>";
+            }
+            $html .= '</div>';
         }
-        $html .= '</div>';
+
         return $html;
     }
 

+ 1 - 1
app/Models/BaseProduct.php

@@ -37,7 +37,7 @@ class BaseProduct extends Model implements Sortable
     // 一对多关联
     public function images()
     {
-        return $this->hasMany(BaseProductImage::class, 'product_id');
+        return $this->hasMany(BaseProductImage::class, 'product_id','id');
     }
 
 }

+ 2 - 0
app/Models/BaseProductImage.php

@@ -23,4 +23,6 @@ class BaseProductImage extends Model
     {
         return $this->belongsTo(BaseProduct::class, 'id');
     }
+
+
 }