moshaorui hace 5 meses
padre
commit
d225ebeacd

+ 83 - 21
app/Admin/Controllers/BaseProductController.php

@@ -3,11 +3,14 @@
 namespace App\Admin\Controllers;
 
 use App\Admin\Repositories\BaseProduct;
+use Dcat\Admin\Admin;
 use Dcat\Admin\Form;
+use Dcat\Admin\Form\NestedForm;
 use Dcat\Admin\Grid;
 use Dcat\Admin\Show;
 use Dcat\Admin\Http\Controllers\AdminController;
-use App\Models\BaseProductCategory;
+use App\Admin\Repositories\BaseProductCategory;
+use Illuminate\Http\Request;
 class BaseProductController extends AdminController
 {
 
@@ -24,7 +27,7 @@ class BaseProductController extends AdminController
             $grid->column('title');
             $grid->column('sku');
             $grid->column('baseProductCategory.name','Category Name');
-            $grid->column('publish_date');
+            $grid->column('issuance_date');
             $grid->column('order')->orderable();
             $grid->column('enabled')->switch();
             $grid->column('created_at');
@@ -34,14 +37,17 @@ class BaseProductController extends AdminController
                 $filter->equal('sku');
                 $filter->like('title');
                 $filter->equal('category_id','Category')->select(BaseProductCategory::selectOptions());
+                $filter->equal('enabled', 'enabled')->select([
+                    1 => 'Yes',
+                    0 => 'No',
+                ]);
             });
             //排序
            $grid->model()->orderBy("order",'asc')->orderBy('id', 'desc');
             // 禁用查看按钮
-            $grid->disableViewButton();
-            $grid->showQuickEditButton();
-            $grid->enableDialogCreate();
-            $grid->disableEditButton();
+            //$grid->showQuickEditButton();
+            //$grid->enableDialogCreate();
+            //$grid->disableEditButton();
         });
     }
 
@@ -63,7 +69,7 @@ class BaseProductController extends AdminController
             $show->field('description');
             $show->field('sku');
             $show->field('baseProductCategory.name','Category Name');
-            $show->field('publish_date');
+            $show->field('issuance_date');
             $show->field('order');
             $show->field('enabled');
             $show->field('content');
@@ -81,24 +87,80 @@ class BaseProductController extends AdminController
     protected function form()
     {
         return Form::make(new BaseProduct(), function (Form $form) {
-            $form->display('id');
-            $form->text('title');
-            $form->text('keywords');
-            $form->text('description');
-            $form->text('sku');
-
             $form->select('category_id', 'Category Name')
-                ->options(BaseProductCategory::all()->pluck('name', 'id'))
+                ->options(BaseProductCategory::selectOptions())
                 ->required();
+//            $form->select('city');
+//            $form->text('title')->required();
+//            $form->text('keywords');
+//            $form->text('description');
+//            $form->text('sku')->required();
+//            $form->date('issuance_date');
+//            $form->table('parameters','Parameters', function (Form\NestedForm $table) {
+//                $table->text('key')->required();
+//                $table->text('value')->required();
+//            });
+//            $form->editor('content');
+//            $form->number('order');
+//            $form->switch('enabled')->default(1);
+
+            $form->table('parameters','Parameters', function (Form\NestedForm $table) {
+                $table->text('key')->required();
+                $table->text('value')->required();
+            })->default([
+                ['key' => 'color', 'value' => 'white'],
+            ]);
+            //插入JS
+            $this->addParametersJs();
+        });
 
-            $form->text('publish_date');
-            $form->text('order');
-            $form->text('enabled');
-            $form->text('content');
-            $form->text('parameters');
+    }
+
+    /*
+     * 以json型式返回产品参数
+     */
+    public static function parameter(Request $request)
+    {
+        $id = $request->query('q');
+        $content = BaseProductCategory::getParameter($id);
+        return $content;
+    }
 
-            $form->display('created_at');
-            $form->display('updated_at');
+    private function addParametersJs()
+    {
+        //插入JS
+        Admin::script(
+            <<<JS
+var fill_param = function (key,val) {
+    lastForm = $(".has-many-table-parameters-form:last");
+    lastForm.find('input').eq(0).val(key);
+    lastForm.find('input').eq(1).val(val);
+}
+$('select[name="category_id"]').on('change', function() {
+    var category_id = $(this).val();
+    // 清空现有的表格行
+    $('.has-many-table-parameters-form').remove();
+    if (category_id > 0) {
+        $.ajax({
+            url: '/prime-control/base-product/parameter',      // 请求的 URL
+            data: { q: category_id},
+            type: 'GET',              // GET 请求
+            success: function(data) { // 成功时执行的代码
+                // 动态添加新数据到表格
+                $.each(data, function(index, item) {
+                    $(".has-many-table-parameters").find(".add").click();
+                    fill_param(item.key,item.value);
+                    console.log(item);
+                });
+            },
+            error: function(error) {  // 错误时执行的代码
+                console.log('error:', error);
+            }
         });
     }
+
+});
+JS
+        );
+    }
 }

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

@@ -60,7 +60,6 @@ class BaseProductParameterController extends AdminController
 
         return Form::make(new BaseProductParameter(), function (Form $form) {
             $form->display('id');
-            $form->switch('enabled')->default(1);
             $form->text('name')->required();
             $form->table('content','Parameters', function (NestedForm $table) {
                 $table->text('key')->required();
@@ -68,6 +67,7 @@ class BaseProductParameterController extends AdminController
             })->default([
                 ['key' => 'color', 'value' => 'white'],
             ]);
+            $form->switch('enabled')->default(1);
         });
     }
 }

+ 2 - 0
app/Admin/Repositories/BaseProduct.php

@@ -13,4 +13,6 @@ class BaseProduct extends EloquentRepository
      * @var string
      */
     protected $eloquentClass = Model::class;
+
+
 }

+ 12 - 0
app/Admin/Repositories/BaseProductCategory.php

@@ -3,6 +3,7 @@
 namespace App\Admin\Repositories;
 
 use App\Models\BaseProductCategory as Model;
+use App\Models\BaseProductParameter;
 use Dcat\Admin\Repositories\EloquentRepository;
 use Dcat\Admin\Traits\HasDateTimeFormatter;
 use Dcat\Admin\Traits\ModelTree;
@@ -18,6 +19,8 @@ class BaseProductCategory extends EloquentRepository
     protected $eloquentClass = Model::class;
 
 
+
+
     // 调用模型方法
     public static function selectOptions(\Closure $closure = null)
     {
@@ -26,4 +29,13 @@ class BaseProductCategory extends EloquentRepository
     }
 
 
+    public static function getParameter($categoryId)
+    {
+        $categoryRow =  Model::Class::find($categoryId);
+        if ($categoryRow->baseProductParameter) {
+            return $categoryRow->baseProductParameter->content;
+        } else {
+            return "[]";
+        }
+    }
 }

+ 5 - 0
app/Admin/routes.php

@@ -11,9 +11,14 @@ Route::group([
     'namespace'  => config('admin.route.namespace'),
     'middleware' => config('admin.route.middleware'),
 ], function (Router $router) {
+    //主页
     $router->get('/', 'HomeController@index');
     $router->resource('test', 'TestController');
+    //产品
+    $router->get('base-product/parameter', 'BaseProductController@parameter');
     $router->resource('base-product', 'BaseProductController');
+    //分类
     $router->resource('product-category', 'BaseProductCategoryController');
+    //参数
     $router->resource('product-parameter', 'BaseProductParameterController');
 });

+ 6 - 0
app/Models/BaseProduct.php

@@ -11,6 +11,12 @@ class BaseProduct extends Model
 	use HasDateTimeFormatter;
     protected $table = 'base_product';
 
+    protected $casts = [
+        'created_at' => 'datetime:Y-m-d H:i:s',
+        'updated_at' => 'datetime:Y-m-d H:i:s',
+        'parameters' => 'json', // 将 attributes 字段转换为数组
+    ];
+
     public function baseProductCategory()
     {
         return $this->hasOne(BaseProductCategory::class,'id','category_id');

+ 1 - 1
config/admin.php

@@ -67,7 +67,7 @@ return [
     'route' => [
         'domain' => env('ADMIN_ROUTE_DOMAIN'),
 
-        'prefix' => env('ADMIN_ROUTE_PREFIX', 'admin'),
+        'prefix' => env('ADMIN_ROUTE_PREFIX', 'prime-control'),
 
         'namespace' => 'App\\Admin\\Controllers',
 

+ 6 - 0
resources/views/admin/BaseProduct/create.blade.php

@@ -0,0 +1,6 @@
+<script>
+// 确保 DOM 完全加载后再执行
+$(document).ready(function() {
+    alert("test");
+});
+</script>