Преглед изворни кода

增加企业端发布商品功能

林海涛 пре 1 година
родитељ
комит
5744f41bea

+ 1 - 1
app/adminapi/controller/goods_category/GoodsCategoryController.php

@@ -30,7 +30,7 @@ use app\adminapi\validate\goods_category\GoodsCategoryValidate;
 class GoodsCategoryController extends BaseAdminController
 {
 
-
+    public array $notNeedLogin = ['treeData'];
     /**
      * @notes 获取列表
      * @return \think\response\Json

+ 19 - 0
app/api/controller/FirmController.php

@@ -2,9 +2,12 @@
 
 namespace app\api\controller;
 
+use app\api\lists\FirmGoodLists;
 use app\api\lists\recharge\FirmOrderLists;
 use app\api\logic\FirmLogic;
+use app\api\logic\GoodsLogic;
 use app\api\validate\FirmRegisterValidate;
+use app\api\validate\GoodsValidate;
 
 class FirmController extends BaseApiController
 {
@@ -35,4 +38,20 @@ class FirmController extends BaseApiController
     {
 
     }
+
+    public function goodLists()
+    {
+        return $this->dataLists(new FirmGoodLists());
+    }
+
+    public function submitGood()
+    {
+
+        $params = (new GoodsValidate())->post()->goCheck('add');
+        $result = GoodsLogic::sync($params,$this->userId);
+        if (true === $result) {
+            return $this->success('已提交', [], 1, 1);
+        }
+        return $this->fail(FirmLogic::getError());
+    }
 }

+ 106 - 0
app/api/lists/FirmGoodLists.php

@@ -0,0 +1,106 @@
+<?php
+/**
+ * @author 林海涛
+ * @date 2024/7/15 下午3:41
+ */
+
+
+namespace app\api\lists;
+
+
+use app\common\model\goods\Goods;
+use app\common\lists\ListsSearchInterface;
+use app\common\model\goods_category\GoodsCategory;
+use app\common\service\FileService;
+
+
+/**
+ * Goods列表
+ * Class GoodsLists
+ * @package app\adminapi\listsgoods
+ */
+class FirmGoodLists extends BaseApiDataLists implements ListsSearchInterface
+{
+
+
+    /**
+     * @notes 设置搜索条件
+     * @return \string[][]
+     * @author likeadmin
+     * @date 2024/07/07 18:37
+     */
+    public function setSearch(): array
+    {
+        return [
+            '=' => ['goods_status'],
+            '%like%' => ['goods_name', 'goods_brand'],
+            'between' => ['service_total', 'service_fee'],
+        ];
+    }
+
+    public function queryWhere()
+    {
+        $where = [];
+        //$where[] = ['user_id',];
+        if (!empty($this->params['goods_category_id'])) {
+            $goodsCategoryId = end($this->params['goods_category_id']);
+            $goodsCategoryData = GoodsCategory::where(['status' => 1])->order(['pid' => 'asc', 'weigh' => 'desc', 'id' => 'desc'])
+                ->select()->toArray();
+            $ids = get_tree_ids($goodsCategoryData, $goodsCategoryId);
+            $ids[] = $goodsCategoryId;
+            $where[] = ['goods_category_id', 'in', $ids];
+        }
+        // 创建时间
+        if (!empty($this->params['create_time'])) {
+            $time = [strtotime($this->params['create_time'][0]), strtotime($this->params['create_time'][1])];
+            $where[] = ['create_time', 'between', $time];
+        }
+        //更新时间
+        if (!empty($this->params['update_time'])) {
+            $time = [strtotime($this->params['update_time'][0]), strtotime($this->params['update_time'][1])];
+            $where[] = ['update_time', 'between', $time];
+        }
+        return $where;
+    }
+
+    /**
+     * @notes 获取列表
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @author likeadmin
+     * @date 2024/07/07 18:37
+     */
+    public function lists(): array
+    {
+        $lists = Goods::order(['id' => 'desc'])
+            ->where($this->searchWhere)
+            ->where($this->queryWhere())
+            ->limit($this->limitOffset, $this->limitLength)
+            ->select()
+            ->toArray();
+        $goodsCategoryObj = GoodsCategory::where(['status' => 1])->order(['pid' => 'asc', 'weigh' => 'desc', 'id' => 'desc'])
+            ->select();
+        foreach ($lists as &$item) {
+            $item['goods_category_ids'] = array_map("intval", $item['goods_category_ids']);
+            $item['goods_category_ids_str'] = implode(' / ', $goodsCategoryObj->whereIn('id', $item['goods_category_ids'])->column('name'));
+            $item['goods_image'] = $item['goods_image'] ? FileService::getFileUrl($item['goods_image']) : null;
+            $item['goods_video'] = $item['goods_video'] ? FileService::getFileUrl($item['goods_video']) : null;
+        }
+        return $lists;
+    }
+
+
+    /**
+     * @notes 获取数量
+     * @return int
+     * @author likeadmin
+     * @date 2024/07/07 18:37
+     */
+    public function count(): int
+    {
+        return Goods::where($this->searchWhere)->where($this->queryWhere())->count();
+    }
+
+}

+ 52 - 0
app/api/logic/GoodsLogic.php

@@ -3,6 +3,8 @@ namespace app\api\logic;
 
 use app\common\logic\BaseLogic;
 use app\common\model\goods\Goods;
+use app\common\model\goods_category\GoodsCategory;
+use think\facade\Db;
 
 
 /**
@@ -38,4 +40,54 @@ class GoodsLogic extends BaseLogic
             ->order(['top_weight' => 'desc', 'id' => 'desc'])
             ->select()->toArray();
     }
+
+    public static function sync(array $params,int $userId): bool
+    {
+        Db::startTrans();
+        try {
+            if(isset($params['id'])){
+                $model = Goods::findOrEmpty($params['id']);
+                if($model->isEmpty()){
+                    throw new \Exception('数据异常');
+                }
+            } else{
+                $model = new Goods();
+            }
+            $model->goods_category_id =  end($params['goods_category_ids']);
+            $model->category_type = GoodsCategory::where('id', end($params['goods_category_ids']))->value('category_type');
+            $model->goods_category_ids =  $params['goods_category_ids'];
+            $model->goods_category_id = $params['goods_category_id'];
+            $model->goods_name = $params['goods_name'];
+            $model->goods_image = $params['goods_image'];
+            $model->goods_video = $params['goods_video'];
+            $model->goods_number = $params['goods_number'];
+            $model->good_unit = $params['good_unit'];
+            $model->goods_size = $params['goods_size'];
+            $model->goods_type = $params['goods_type'];
+            $model->goods_brand = $params['goods_brand'];
+            $model->install_guide = $params['install_guide'];
+            $model->goods_payment_type = $params['goods_payment_type'];
+            $model->base_service_fee = $params['base_service_fee'];
+            $model->service_total = $params['service_total'];
+            $model->service_fee = $params['service_fee'];
+            $model->service_image = $params['service_image'];
+            $model->warranty_period = $params['warranty_period'];
+            $model->fee_schedule = $params['fee_schedule'];
+            $model->goods_status = $params['goods_status'];
+            $model->is_recommend = $params['is_recommend'] ?:0;
+            $model->recommend_weight = $params['recommend_weight'] ?:0;
+            $model->is_top = $params['is_top'] ?:0;
+            $model->top_weight = $params['top_weight'] ?:0;
+            $model->is_hot = $params['is_hot'] ?:0;
+            $model->hot_weight = $params['hot_weight'] ?:0;
+            $model->user_id = $userId;
+            $model->save();
+            Db::commit();
+            return true;
+        } catch (\Exception $e) {
+            Db::rollback();
+            self::setError($e->getMessage());
+            return false;
+        }
+    }
 }

+ 97 - 0
app/api/validate/GoodsValidate.php

@@ -0,0 +1,97 @@
+<?php
+namespace app\api\validate;
+
+
+use app\common\validate\BaseValidate;
+
+
+/**
+ * Goods验证器
+ * Class GoodsValidate
+ * @package app\adminapi\validate\goods
+ */
+class GoodsValidate extends BaseValidate
+{
+
+     /**
+      * 设置校验规则
+      * @var string[]
+      */
+    protected $rule = [
+        'id' => 'require',
+        'goods_category_ids' => 'require',
+        'goods_name' => 'require',
+        'goods_number' => 'require',
+        'base_service_fee' => 'require',
+        'service_total' => 'require',
+        'service_fee' => 'require',
+        'goods_status' => 'require',
+
+    ];
+
+
+    /**
+     * 参数描述
+     * @var string[]
+     */
+    protected $field = [
+        'id' => 'id',
+        'goods_category_ids' => '服务类目',
+        'goods_name' => '商品名称',
+        'goods_number' => '商品数量',
+        'base_service_fee' => '基础服务费',
+        'service_total' => '服务原价',
+        'service_fee' => '服务价格',
+        'goods_status' => '商品状态',
+
+    ];
+
+
+    /**
+     * @notes 添加场景
+     * @return GoodsValidate
+     * @author likeadmin
+     * @date 2024/07/07 18:37
+     */
+    public function sceneAdd()
+    {
+        return $this->only(['goods_category_ids','goods_category_id','goods_name','goods_number','base_service_fee','service_total','service_fee','goods_status']);
+    }
+
+
+    /**
+     * @notes 编辑场景
+     * @return GoodsValidate
+     * @author likeadmin
+     * @date 2024/07/07 18:37
+     */
+    public function sceneEdit()
+    {
+        return $this->only(['id','goods_category_ids','goods_category_id','goods_name','goods_number','base_service_fee','service_total','service_fee','goods_status']);
+    }
+
+
+    /**
+     * @notes 删除场景
+     * @return GoodsValidate
+     * @author likeadmin
+     * @date 2024/07/07 18:37
+     */
+    public function sceneDelete()
+    {
+        return $this->only(['id']);
+    }
+
+
+    /**
+     * @notes 详情场景
+     * @return GoodsValidate
+     * @author likeadmin
+     * @date 2024/07/07 18:37
+     */
+    public function sceneDetail()
+    {
+        return $this->only(['id']);
+    }
+
+}