Browse Source

SEO优化

moshaorui 5 months ago
parent
commit
93d6eaae85

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

@@ -74,8 +74,6 @@ class BaseProductController extends AdminController
         return Show::make($id, BaseProduct::with(['baseProductCategory','images']), function (Show $show) {
             $show->field('id');
             $show->field('title');
-            $show->field('keywords');
-            $show->field('description');
             $show->field('sku');
             $show->field('base_product_category.name',admin_trans_label('category_name'));
             $show->field('issuance_date');
@@ -106,6 +104,9 @@ class BaseProductController extends AdminController
             $show->field('enabled')->using(config('dictionary.enabled'));
             $show->field('created_at');
             $show->field('updated_at');
+            $show->field('seo_title');
+            $show->field('seo_keywords');
+            $show->field('seo_description');
         });
     }
 
@@ -123,8 +124,6 @@ class BaseProductController extends AdminController
                 ->options(BaseProductCategory::selectOptions())
                 ->required();
             $form->text('title')->required();
-            $form->text('keywords');
-            $form->textarea('description');
             $form->text('sku')->required();
             $form->date('issuance_date');
             $form->table('parameters',admin_trans_label('parameter_name'), function (Form\NestedForm $table) {
@@ -152,8 +151,20 @@ class BaseProductController extends AdminController
             $form->editor('content');
             $form->switch('is_pinned')->default(0);
             $form->switch('enabled')->default(1);
-            //插入JS
+
+            $form->radio('visibility')->when(1,function (Form $form) {
+                $form->text('seo_title');
+                $form->text('seo_keywords');
+                $form->textarea('seo_description');
+            })->options(config('dictionary.visibility'))->default(1)->value(1);
+            //隐藏字段
+            $form->ignore(['visibility']);
+            //插入参数联动JS
             $this->addParametersJs();
+            //新建时插入JS
+            $form->creating(function (Form $form) {
+                CommonHelper::seoReplace('title',false);
+            });
             //保存前回调
             $form->saving(function ($form) {
                 //检查sku是否重复

+ 1 - 1
app/Distributor/Controllers/DistVideoCategoryController.php

@@ -3,7 +3,7 @@
 namespace App\Distributor\Controllers;
 
 use App\Distributor\Repositories\DistVideo;
-use App\distributor\Repositories\DistVideoCategory;
+use App\Distributor\Repositories\DistVideoCategory;
 use Dcat\Admin\Form;
 use Dcat\Admin\Grid;
 use Dcat\Admin\Show;

+ 6 - 3
app/Distributor/Forms/ImportProduct.php

@@ -48,24 +48,27 @@ class ImportProduct extends Form
 // 获取 base_product_image 表中与当前 base_product 相关的图片记录
                 $baseProductImages = BaseProductImage::where('product_id', $baseProduct->id)->get();
 
+
+
                 // 创建新的 DistProduct 记录
                 $distProduct = DistProduct::create([
                     'category_id' => $categoryId,
                     'title' => $baseProduct->title,
-                    'keywords' => $baseProduct->keyword, // 注意这里是 'keywords' 而不是 'keyword'
-                    'description' => $baseProduct->description,
                     'sku' => $baseProduct->sku, // 假设 $baseProduct 也有 sku 字段
                     'issuance_date' => $baseProduct->issuance_date, // 假设 $baseProduct 也有 issuance_date 字段
                     'order' => $baseProduct->order, // 假设 $baseProduct 也有 order 字段
                     'enabled' => $baseProduct->enabled, // 假设 $baseProduct 也有 enabled 字段
                     'content' => $baseProduct->content, // 假设 $baseProduct 也有 content 字段
                     'parameters' => $baseProduct->parameters, // 假设 $baseProduct 也有 parameters 字段
+                    'seo_title'=> $baseProduct->seo_title,
+                    'seo_keywords' => $baseProduct->seo_keywords,
+                    'seo_description' => $baseProduct->seo_description,
                     'is_pinned' => 0, // 假设 $baseProduct 也有 is_pinned 字段
                     'dist_id' => getDistributorId(),
                     'created_at' => now(), // 自动填充创建时间
                     'updated_at' => now(), // 自动填充更新时间
                 ]);
-
+                DistProduct::where('id', $distProduct->id)->update(['slug' => $distProduct->id]);
                 // 遍历 base_product_image 表中的记录,并插入到 dist_product_image 表中
                 foreach ($baseProductImages as $baseImage) {
                     DistProductImage::create([

+ 15 - 2
app/Libraries/CommonHelper.php

@@ -97,7 +97,19 @@ JS
 
     public static function seoReplace($titleName = 'title',$modelName = 'pages')
     {
-        Admin::script(
+        if ($titleName) {
+            Admin::script(
+<<<JS
+    $('input[name="title"]').on('input', function() {
+        // 将 title 的值赋给 seo_title
+        $('input[name="seo_title"]').val($(this).val());
+    });
+JS
+            );
+        }
+        //ajax 生成slug
+        if ($modelName) {
+            Admin::script(
 <<<JS
     $('input[name="title"]').on('input', function() {
         // 将 title 的值赋给 seo_title
@@ -119,7 +131,8 @@ JS
         });
     });
 JS
-        );
+            );
+        }
 
     }
 

+ 4 - 0
app/Models/DistProduct.php

@@ -40,6 +40,10 @@ class DistProduct extends Model
         'updated_at',
         'is_pinned',
         'dist_id',
+        'seo_title',
+        'seo_keywords',
+        'seo_description',
+        'slug',
     ];