xieruidong 2 năm trước cách đây
mục cha
commit
55edb03475

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

@@ -96,7 +96,7 @@ class Goods extends Backend
     public function edit($ids = null)
     {
         if($this->request->isGet()){
-            $row=$this->model->with(['sku'=>function($q){},'service','size','sku','detail'])->findOrFail($ids)->toArray();
+            $row=$this->model->with(['service','size','sku','detail'])->findOrFail($ids)->toArray();
             foreach ($row['sku'] as &$_sku){
                 $_sku['detail']=[];
                 $_sku['specValues']=$_sku['name'];
@@ -216,7 +216,7 @@ class Goods extends Backend
         $skuId=[];
         $detailId=[];
         foreach ($data['sku'] as $key=>$_sku){
-            $sku=Arr::only($_sku,['amount_cost','amount_kill_ladder','amount_ladder','num_stock','num_stock_kill']);
+            $sku=Arr::only($_sku,['amount_cost','amount_kill_ladder','amount_ladder','num_stock','num_stock_kill','temp_ids']);
             $sku['name']=$_sku['specValues'];
             $sku['goods_id']=$goods['id'];
             if(!empty($sku['id'])){

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

@@ -47,4 +47,7 @@ class Goods extends \app\common\model\Goods
     public function category(){
         return $this->belongsTo(Category::class)->setEagerlyType(0);
     }
+    public function sku(){
+        return $this->hasMany(GoodsSku::class);
+    }
 }

+ 1 - 1
application/admin/model/GoodsSku.php

@@ -11,7 +11,7 @@ use think\Model;
  */
 class GoodsSku Extends \app\common\model\GoodsSku
 {
-    public function getAmountAttr($amount)
+    public function getAmountAttr($amount,$model)
     {
         return $amount;
     }

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

@@ -108,6 +108,7 @@ class Goods Extends Model
             $goods->detail()->delete();
             $goods->service()->delete();
             $goods->sku()->delete();
+            $goods->size()->delete();
             $goods->binds()->delete();
             GoodsBind::where('bind_goods_id',$goods['id'])->delete();
             GoodsCart::where('goods_id',$goods['id'])->delete();

+ 18 - 8
public/assets/js/backend/goods.js

@@ -251,6 +251,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                     }
                 },
                 mounted(){
+                    console.log(this.form)
                     Controller.api.bindevent();
                 },
                 filters:{
@@ -321,7 +322,8 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                                 }
                             }
                             spec.names.push({
-                                name:value
+                                name:value,
+                                temp_id:Date.now().toString(),
                             })
                             this.buildSku()
                         })
@@ -354,7 +356,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                      */
                     setTableList(){
                         let newList=[];
-                        this.getRowList(null,0,newList)
+                        this.getRowList(null,0,newList,null)
                         this.form.sku=newList;
                     },
                     /**
@@ -362,28 +364,36 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                      * @param specValues 多规格值通过逗号拼接组成每一行唯一值
                      * @param specIndex 规格索引
                      * @param newList 新表格数据
+                     * @param temp_ids
                      */
-                    getRowList(specValues,specIndex,newList){
+                    getRowList(specValues,specIndex,newList,temp_ids){
                         if(specIndex<this.form.size.length){
                             this.form.size[specIndex].names.forEach((t)=>{
                                 let newValues=(specValues===null?t.name:(specValues+","+t.name))
+                                let new_temp_ids=(temp_ids===null?t.temp_id:(temp_ids+","+t.temp_id))
                                 //当前规格值,一直往下规格值循环完
-                                this.getRowList(newValues,specIndex+1,newList)
+                                this.getRowList(newValues,specIndex+1,newList,new_temp_ids)
                             })
                             //当添加新规格(第二个规格的时候)没有规格值时,默认一个空值
                             if(this.form.size[specIndex].names.length===0 && specValues!==null){
                                 let newValues=specValues+","
-                                this.getRowList(newValues,specIndex+1,newList)
+                                let new_temp_ids=temp_ids+","
+                                this.getRowList(newValues,specIndex+1,newList,new_temp_ids)
                             }
                         }
                         //当最后规格的规格值循环到最后后添加行数据
                         else{
                             //判断表格行数据是否已经存在,存在直接添加不创建新行
                             let row = this.form.sku.find((r)=>{
-                                return r.specValues.split(',').sort().toString()===specValues.split(',').sort().toString()
+                                //return r.specValues.split(',').sort().toString()===specValues.split(',').sort().toString()
+                                let eq=r.temp_ids.split(',').sort().toString() === temp_ids.split(',').sort().toString()
+                                return eq
                             });
-                            console.log('old',row)
                             if(row){
+                                specValues.split(',').forEach((c,index)=>{
+                                    row["sku-"+index]=c
+                                })
+                                row.specValues=specValues
                                 newList.push({...row})
                             }
                             else{
@@ -391,6 +401,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                                 row = {
                                     size:specIndex,
                                     specValues:specValues,
+                                    temp_ids:temp_ids,
                                     num_stock:0,
                                     num_stock_kill:0,
                                     amount_cost:0,
@@ -415,7 +426,6 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                                         }
                                     ],
                                 }
-                                //console.log('row',row)
                                 //通过规格值循环出规格值列数
                                 specValues.split(',').forEach((c,index)=>{
                                     row["sku-"+index]=c