ソースを参照

企业商品添加接口优化

wufeng 2 年 前
コミット
67d92732a5

+ 1 - 3
app/admin/config/validate_rules.php

@@ -151,9 +151,7 @@ return [
     //添加
     'CompanyGoodAdd' => [
         'group_id|分组id' => 'require|number|gt:0',
-        'good_id|商品id' => 'require|number|gt:0',
-        'is_top|是否置顶' => 'require|number|in:' . CommonModel::$top_no . ',' . CommonModel::$top_yes,
-        'weight|权重' => 'number|egt:0',
+        'good_id|商品id' => 'require|array|max:100',
     ],
 
 

+ 3 - 3
app/admin/controller/CompanyGood.php

@@ -34,7 +34,7 @@ class CompanyGood extends BaseController
     //添加
     public function add()
     {
-        $param = $this->request->only(['group_id', 'good_id', 'is_top' => CommonModel::$top_no, 'weight' => 0], 'post');
+        $param = $this->request->only(['group_id', 'good_id'], 'post');
 
         $val = Validate::rule(Config::get('validate_rules.CompanyGoodAdd'));
 
@@ -53,8 +53,8 @@ class CompanyGood extends BaseController
     //编辑
     public function edit()
     {
-        $param = $this->request->only(['id','group_id', 'good_id', 'is_top' => CommonModel::$top_no, 'weight' => 0], 'post');
-        $val = Validate::rule(array_merge(Config::get('validate_rules.CompanyGoodAdd'),[
+        $param = $this->request->only(['id', 'group_id', 'good_id', 'is_top' => CommonModel::$top_no, 'weight' => 0], 'post');
+        $val = Validate::rule(array_merge(Config::get('validate_rules.CompanyGoodAdd'), [
             'id|公司商品id' => 'require|number|gt:0',
         ]));
 

+ 13 - 9
app/admin/logic/CompanyGoodLogic.php

@@ -52,22 +52,26 @@ class CompanyGoodLogic extends BaseLogic
             ->isEmpty();
         if ($rs) return json_show(CommonModel::$error_param, '该分组不存在');
 
-        $rs = GoodModel::field('id')
-            ->where(['id' => $data['good_id'], 'is_del' => CommonModel::$del_normal])
-            ->findOrEmpty()
-            ->isEmpty();
-        if ($rs) return json_show(CommonModel::$error_param, '该商品不存在');
+        $data['good_id'] = GoodModel::where('is_del', CommonModel::$del_normal)
+            ->whereIn('id', $data['good_id'])
+            ->column('id');
+        if (empty($data['good_id'])) return json_show(CommonModel::$error_param, '该商品不存在');
 
-        $rs = CompanyGoodModel::field('id')
-            ->where(['is_del' => CommonModel::$del_normal, 'group_id' => $data['group_id'], 'good_id' => $data['good_id']])
+        $rs = CompanyGoodModel::field('a.id,a.good_id,b.good_name,b.good_code')
+            ->alias('a')
+            ->leftJoin('good b', 'b.id=a.good_id AND b.is_del=' . CommonModel::$del_normal)
+            ->where(['a.is_del' => CommonModel::$del_normal, 'a.group_id' => $data['group_id']])
+            ->whereIn('a.good_id', $data['good_id'])
             ->findOrEmpty()
-            ->isEmpty();
-        if (!$rs) return json_show(CommonModel::$error_param, '该公司商品已存在');
+            ->toArray();
+        if (empty($rs)) return json_show(CommonModel::$error_layer, "【{$rs['good_code']}】【{$rs['good_name']}】该商品重复添加");
 
         $res = CompanyGoodModel::create((array_merge($data, [
             'code' => makeNo('CG'),
             'is_del' => CommonModel::$del_normal,
             'status' => CommonModel::$status_normal,
+            'is_top' => CommonModel::$top_no,
+            'weight' => 0,
             'creater' => self::$uname,
             'createrid' => self::$uid,
             'addtime' => date('Y-m-d H:i:s'),

+ 8 - 0
app/mobile/config/validate_rules.php

@@ -49,4 +49,12 @@ return [
         'code|编码'=>'require|length:18'
     ],
 
+    //【订单】
+    'orderAdd'=>[
+        'good_id|商品'=>'require|number|gt:0',
+        'addr_id|收货地址'=>'require|number|gt:0',
+        'num|购买数量'=>'require|number|gt:0',
+        'remark|'=>'max:255',
+    ],
+
 ];

+ 13 - 0
app/mobile/controller/Exchange.php

@@ -29,5 +29,18 @@ class Exchange extends BaseController
         return ExchangeLogic::goodInfo($code);
     }
 
+    //兑换商品下单
+    public function orderAdd(){
+
+        $param = $this->request->only(['good_id','addr_id','num','remark'],'post');
+
+        $val = Validate::rule(Config::get('validate_rules.orderAdd'));
+
+        if(!$val->check($param)) throw new ValidateException($val->getError());
+
+        return ExchangeLogic::orderAdd($param);
+
+    }
+
 
 }

+ 33 - 1
app/mobile/logic/ExchangeLogic.php

@@ -6,6 +6,8 @@ use app\model\CommonModel;
 use app\model\CompanyGoodModel;
 use app\model\GoodModel;
 use app\model\GroupModel;
+use think\Exception;
+use think\facade\Db;
 use think\response\Json;
 
 class ExchangeLogic extends BaseLogic
@@ -51,7 +53,7 @@ class ExchangeLogic extends BaseLogic
                 return json_decode($val, true);
             })
             ->withAttr('good_img', function ($val) {
-                return explode(',',$val);
+                return explode(',', $val);
             })
             ->findOrEmpty()
             ->toArray();
@@ -60,5 +62,35 @@ class ExchangeLogic extends BaseLogic
 
     }
 
+    //下单
+    public static function orderAdd(array $data = []): Json
+    {
+
+        Db::startTrans();
+
+        try {
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+            Db::commit();
+
+            return json_show(CommonModel::$success, '兑换商品成功');
+        } catch (Exception $exception) {
+            Db::rollback();
+            return json_show(CommonModel::$error_param, '兑换失败,' . $exception->getMessage());
+        }
+
+    }
 
 }

+ 3 - 0
app/model/CommonModel.php

@@ -12,6 +12,9 @@ class CommonModel
     public static $error_default = 1005;//业务错误
     public static $error_other = 1006;//其他错误
 
+    //特殊错误码
+    public static $error_layer = 1010;//弹出
+
     //是否删除
     public static $del_normal=0;//未删除
     public static $del_deleted=1;//已删除