xieruidong 2 年之前
父節點
當前提交
47d4c9a043

+ 4 - 2
application/admin/controller/Goods.php

@@ -25,6 +25,7 @@ class Goods extends Backend
         parent::_initialize();
         $this->model = new \app\admin\model\Goods;
         $this->assign('status',\app\admin\model\Goods::getStatus());
+        $this->assign('category',\app\common\model\Category::mall());
 
     }
 
@@ -46,7 +47,7 @@ class Goods extends Backend
     public function index()
     {
         //当前是否为关联查询
-        $this->relationSearch = false;
+        $this->relationSearch = true;
         //设置过滤方法
         $this->request->filter(['strip_tags', 'trim']);
         if ($this->request->isAjax()) {
@@ -57,7 +58,7 @@ class Goods extends Backend
             list($where, $sort, $order, $offset, $limit) = $this->buildparams();
 
             $list = $this->model
-                    
+                    ->with(['category'])
                     ->where($where)
                     ->order($sort, $order)
                     ->paginate($limit);
@@ -98,6 +99,7 @@ class Goods extends Backend
         $data['logo']=array_filter(explode(',',$data['logo']??''));
         $this->validate($data,[
             'name|商品名称'=>['require'],
+            'category_id|商品分类'=>['require'],
             'brand|品牌'=>['require'],
             'amount|售价'=>['require','float','gt:0'],
             'is_kill'=>['require'],

+ 4 - 0
application/admin/model/Goods.php

@@ -2,6 +2,7 @@
 
 namespace app\admin\model;
 
+use app\common\model\Category;
 use think\Model;
 
 
@@ -37,4 +38,7 @@ class Goods extends \app\common\model\Goods
     }
 
 
+    public function category(){
+        return $this->belongsTo(Category::class)->setEagerlyType(0);
+    }
 }

+ 6 - 0
application/admin/view/goods/add.html

@@ -12,6 +12,12 @@
         </div>
     </div>
     <div class="form-group">
+        <label class="control-label col-xs-12 col-sm-2">{:__('分类')}:</label>
+        <div class="col-xs-12 col-sm-8">
+            {:build_select('row[category_id]',array_column($category,'name','id'),$row['category_id']??0,['required'=>true])}
+        </div>
+    </div>
+    <div class="form-group">
         <label class="control-label col-xs-12 col-sm-2">{:__('Brand')}:</label>
         <div class="col-xs-12 col-sm-8">
             <input id="c-brand" class="form-control" name="row[brand]" type="text" data-rule="required" value="{$row.brand|default=''}">

+ 1 - 0
application/admin/view/goods/index.html

@@ -34,4 +34,5 @@
 </div>
 <script>
     var goodsStatus={:json_encode($status)};
+    var category={:json_encode(array_column($category,'name','id'))};
 </script>

+ 4 - 0
application/common/model/Category.php

@@ -82,4 +82,8 @@ class Category extends Model
         })->order('weigh', 'desc')->select())->toArray();
         return $list;
     }
+
+    public static function mall(){
+        return self::getCategoryArray('mall');
+    }
 }

+ 3 - 0
application/common/model/Goods.php

@@ -27,6 +27,9 @@ class Goods Extends Model
     public function service(){
         return $this->hasMany(GoodsService::class);
     }
+    public function category(){
+        return $this->belongsTo(Category::class);
+    }
 
     public static $status=[
         1=>'正常',

+ 2 - 1
public/assets/js/backend/goods.js

@@ -28,6 +28,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                         {checkbox: true},
                         {field: 'id', title: __('Id')},
                         {field: 'name', title: __('Name')},
+                        {field: 'category.name', title: __('分类'),formatter: Table.api.formatter.label,operate: "like"},
                         {field: 'brand', title: __('Brand')},
                         {field: 'amount', title: __('售价'), operate:'BETWEEN'},
                         {field: 'amount_kill', title: __('Amount_kill'), operate:'BETWEEN'},
@@ -43,7 +44,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                 onLoadSuccess(){
                     setTimeout(()=>{
                         $('.btn-editone').attr('data-area','["100%","100%"]')
-                    },1000)
+                    },100)
                 }
             });