xieruidong 2 年之前
父节点
当前提交
f1cac49773
共有 2 个文件被更改,包括 93 次插入3 次删除
  1. 16 3
      application/admin/controller/Goods.php
  2. 77 0
      application/common/model/Goods.php

+ 16 - 3
application/admin/controller/Goods.php

@@ -3,6 +3,7 @@
 namespace app\admin\controller;
 
 use app\common\controller\Backend;
+use think\Db;
 
 /**
  * 商品列管理
@@ -99,14 +100,14 @@ class Goods extends Backend
         }
         foreach ($data['detail'] as $item){
             $this->validate($item,[
-                'name|规格名'=>['require'],
-                'num_stock|规格库存'=>['require','gt:0'],
+                'name|规格参数名'=>['require'],
+                'value|规格参数值'=>['require','gt:0'],
             ]);
         }
         foreach ($data['service'] as $item){
             $this->validate($item,[
                 'name|售后服务参数名'=>['require'],
-                'num_stock|售后服务参数值'=>['require','gt:0'],
+                'value|售后服务参数值'=>['require','gt:0'],
             ]);
         }
         return $data;
@@ -114,5 +115,17 @@ class Goods extends Backend
 
     protected function makeAdd(){
         $data=$this->makeVal();
+        if(isset($data['id'])){
+            $goods=$this->model->findOrFail($data['id']);
+        }else{
+            $goods=new $this->model;
+        }
+        Db::startTrans();
+        foreach ($data as $key=>$value){
+            if(!in_array($key,$goods::$read)){
+                $goods[$key]=$value;
+            }
+        }
+        $goods->allowField(true)->save();
     }
 }

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

@@ -9,6 +9,12 @@ use think\Model;
  */
 class Goods Extends Model
 {
+    protected $type=[
+        'logo'=>'json',
+    ];
+    public static $read=[
+        'num_sell'
+    ];
     // 开启自动写入时间戳字段
     protected $autoWriteTimestamp = true;
 
@@ -21,4 +27,75 @@ class Goods Extends Model
     public function service(){
         return $this->hasMany(GoodsService::class);
     }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+    protected static function init()
+    {
+        self::beforeWrite(function (self $goods){
+            if(!empty($goods['is_hot']) && $goods['is_hot']==1){
+                $goods['hot_at']=time();
+            }
+            if(!empty($goods['is_kill']) && $goods['is_kill']==1){
+                $goods['kill_at']=time();
+            }
+        });
+    }
 }