Browse Source

禁止删除oss图片

moshaorui 4 months ago
parent
commit
15a8d4a1f5

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

@@ -130,6 +130,7 @@ class BaseVideoController extends AdminController
                 ->options(BaseVideoCategory::selectOptions())
                 ->required();
             $form->image("cover_image")
+                ->retainable()//禁止删OSS图
                 ->autoUpload()
                 ->uniqueName()
                 ->accept(config('admin.upload.oss_image.accept'))

+ 1 - 0
app/Admin/Controllers/DistAppearanceController.php

@@ -117,6 +117,7 @@ class DistAppearanceController extends AdminController
             $form->text('title');
             $form->text('folder');
             $form->image("cover_image")
+                ->retainable()//禁止删OSS图
                 ->autoUpload()
                 ->uniqueName()
                 ->accept(config('admin.upload.oss_image.accept'))

+ 1 - 0
app/Admin/Controllers/DistSiteController.php

@@ -143,6 +143,7 @@ class DistSiteController extends AdminController
                 $form->textarea('seo_description')->rows(2)->width(9,3);
                 $form->select('appearance_id',admin_trans_field('appearance'))->width(9,3)->options(DistAppearance::selectOptions())->required();
                 $form->image("logo")
+                    ->retainable()//禁止删OSS图
                     ->autoUpload()
                     ->uniqueName()
                     ->accept(config('distributor.upload.oss_image.accept'))

+ 87 - 0
app/Console/Commands/ImportSpecificCategories.php

@@ -0,0 +1,87 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Carbon\Carbon;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Log;
+
+/*
+ * 导入产品分类
+ * 运行命令:php artisan import:specific-categories
+ */
+class ImportSpecificCategories extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'import:specific-categories';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        // 获取主类记录
+        $mainCategories = DB::table('album_path')
+            ->whereIn('name', ['功能类产品', '屏幕保护膜', '智能切膜机'])
+            ->where('parent_id', 0)
+            ->get();
+
+        // 获取所有需要导入的类的ID
+        $idsToImport = [];
+        foreach ($mainCategories as $main) {
+            $idsToImport[] = $main->id;
+            $this->getSubCategories($main->id, $idsToImport);
+        }
+
+        // 获取所有需要导入的类
+        $categoriesToImport = DB::table('album_path')
+            ->whereIn('id', $idsToImport)
+            ->get();
+
+        // 插入到目标表并记录ID映射
+        $idMapping = [];
+        $tableMapping = [];
+        foreach ($categoriesToImport as $category) {
+            $parentId = $category->parent_id == 0 ? 0 : ($idMapping[$category->parent_id] ?? 0);
+            $newId = DB::table('base_product_category')->insertGetId([
+                'name' => $category->name,
+                'parent_id' => $parentId,
+                'order' => 0,
+                'enabled' => 1,
+                'created_at' => Carbon::now(),
+                'updated_at' => Carbon::now(),
+            ]);
+            $idMapping[$category->id] = $newId;
+            $tableMapping[] = ['base_product_category'=>$newId, 'album_path_id'=>$category->id];
+        }
+        // 记录映射关系
+        Log::info('tableMapping: '.json_encode($tableMapping));
+        $this->info('Categories imported successfully!');
+    }
+
+    // 递归获取所有子类ID
+    private function getSubCategories($parentId, &$ids)
+    {
+        $subCategories = DB::table('album_path')
+            ->where('parent_id', $parentId)
+            ->get();
+        foreach ($subCategories as $sub) {
+            $ids[] = $sub->id;
+            $this->getSubCategories($sub->id, $ids);
+        }
+    }
+}

+ 129 - 0
app/Console/Commands/SyncAlbumContent.php

@@ -0,0 +1,129 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Carbon\Carbon;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Str;
+
+/**
+ * 导入相册内容到产品表
+ * php artisan sync:album-content
+ */
+class SyncAlbumContent extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'sync:album-content';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Sync album content to product tables';
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $tableMapping = json_decode('[{"base_product_category":11,"album_path_id":21},{"base_product_category":12,"album_path_id":22},{"base_product_category":13,"album_path_id":31},{"base_product_category":14,"album_path_id":35},{"base_product_category":15,"album_path_id":51},{"base_product_category":16,"album_path_id":64},{"base_product_category":17,"album_path_id":74},{"base_product_category":18,"album_path_id":79},{"base_product_category":19,"album_path_id":83},{"base_product_category":20,"album_path_id":114},{"base_product_category":21,"album_path_id":123},{"base_product_category":22,"album_path_id":141},{"base_product_category":23,"album_path_id":142},{"base_product_category":24,"album_path_id":143},{"base_product_category":25,"album_path_id":145},{"base_product_category":26,"album_path_id":151},{"base_product_category":27,"album_path_id":153},{"base_product_category":28,"album_path_id":154}]  ');
+
+        foreach ($tableMapping as $mapping) {
+            $mapping = (array)$mapping;
+            $albumContents = DB::table('album_content')
+                ->where('path_id', $mapping['album_path_id'])
+                ->get();
+
+            foreach ($albumContents as $albumContent) {
+                // Insert or update base_product
+                $baseProduct = DB::table('base_product')
+                    ->where('title', $albumContent->model)
+                    ->first();
+
+                $detail = json_decode($albumContent->detail);
+                $detail_cn = json_decode($albumContent->detail_cn);
+
+                $content = "";
+                if ($detail) {
+                    foreach ($detail as $key => $value) {
+                        $content .= '<img src="'. $value. '"">';
+                    }
+                }
+                if ($detail_cn) {
+                    foreach ($detail_cn as $key => $value) {
+                        $content .= '<img src="'. $value. '"">';
+                    }
+                }
+
+                $baseProductId = 0;
+                if (!$baseProduct) {
+                    $baseProduct = DB::table('base_product')
+                        ->insertGetId([
+                            'title' => $albumContent->model,
+                            'category_id' => $mapping['base_product_category'],
+                            'content' => $content,
+                            'created_at' => Carbon::now(),
+                            'updated_at' => Carbon::now(),
+                        ]);
+                    $baseProductId = $baseProduct;
+                } else {
+                    DB::table('base_product')
+                        ->where('id', $baseProduct->id)
+                        ->update([
+                            'title' => $albumContent->model,
+                            'category_id' => $mapping['base_product_category'],
+                            'content' => $content,
+                            'updated_at' => Carbon::now(),
+                        ]);
+                    $baseProductId = $baseProduct->id;
+                }
+//                var_dump($baseProduct->id);
+//                exit;
+
+                // Insert base_product_image
+                $photos = json_decode($albumContent->photo, true);
+                if (is_array($photos)) {
+                    foreach ($photos as $photo) {
+                        DB::table('base_product_image')
+                            ->insert([
+                                'product_id' => $baseProductId,
+                                'image_url' => $photo,
+                                'created_at' => Carbon::now(),
+                                'updated_at' => Carbon::now(),
+                            ]);
+                    }
+                }
+
+                // Insert base_video
+                $videos = json_decode($albumContent->video, true);
+                if (is_array($videos)) {
+                    foreach ($videos as $video) {
+                        if (isset($video['video_src']) && $video['video_src'] !== '0' && !empty($video['video_src'])) {
+                            DB::table('base_video')
+                                ->insert([
+                                    'title' => $video['video_title'] ?? null,
+                                    'category_id' => 1,
+                                    'video_url' => $video['video_src'],
+                                    'cover_image' => $video['cover'] ?? null,
+                                    'enabled' => 1,
+                                    'created_at' => Carbon::now(),
+                                    'updated_at' => Carbon::now(),
+                                ]);
+                        }
+                    }
+                }
+            }
+        }
+
+        $this->info('Sync completed successfully.');
+        return 0;
+    }
+}

+ 1 - 0
app/Distributor/Controllers/DistVideoController.php

@@ -141,6 +141,7 @@ class DistVideoController extends AdminDistController
                 ->options(DistVideoCategory::selectOptions())
                 ->required();
             $form->image("cover_image", admin_trans_label('cover_image'))
+                ->retainable()//禁止删OSS图
                 ->autoUpload()
                 ->uniqueName()
                 ->accept(config('distributor.upload.oss_image.accept'))

+ 1 - 0
app/Distributor/Controllers/SettingsController.php

@@ -81,6 +81,7 @@ class SettingsController extends Controller
             $form->text('seo_keywords')->value($distInfo->seo_keywords);
             $form->textarea('seo_description')->value($distInfo->seo_description);
             $form->image("logo")
+                ->retainable()//禁止删OSS图
                 ->autoUpload()
                 ->uniqueName()
                 ->accept(config('distributor.upload.oss_image.accept'))

+ 1 - 0
app/Distributor/Controllers/SiteBannerController.php

@@ -67,6 +67,7 @@ class SiteBannerController extends AdminDistController
             $form->text('title')->required();
             $form->text('subtitle');
             $form->image("image_url", admin_trans_label('images'))
+                ->retainable()//禁止删OSS图
                 ->autoUpload()
                 ->uniqueName()
                 ->accept(config('distributor.upload.oss_image.accept'))

+ 1 - 0
app/Distributor/Controllers/SitePagesController.php

@@ -166,6 +166,7 @@ class SitePagesController extends AdminDistController
                     return $value;
                 });
             $form->image("cover_image")
+                ->retainable()//禁止删OSS图
                 ->autoUpload()
                 ->uniqueName()
                 ->accept(config('distributor.upload.oss_image.accept'))

+ 7 - 3
app/Libraries/CommonHelper.php

@@ -68,12 +68,16 @@ class CommonHelper
     /*
      * 返回oss的url
      */
-    public static function ossUrl($image)
+    public static function ossUrl($imageUrl)
     {
+        if (strpos($imageUrl, 'http:') === 0 || strpos($imageUrl, 'https:') === 0) {
+            return  $imageUrl;
+        }
+
         if (env('OSS_DOMAIN')) {
-            return "http://".env('OSS_DOMAIN').'/'.$image;
+            return "http://".env('OSS_DOMAIN').'/'.$imageUrl;
         }
-        return "http://".env('OSS_BUCKET').'.'.env('OSS_ENDPOINT').'/'.$image;
+        return "http://".env('OSS_BUCKET').'.'.env('OSS_ENDPOINT').'/'.$imageUrl;
     }
 
     /*