Explorar el Código

增加购物车逻辑,首页逻辑,商品逻辑

林海涛 hace 1 año
padre
commit
07155ff75a

+ 12 - 2
app/common/model/shops/ShopCart.php

@@ -15,7 +15,17 @@ class ShopCart extends BaseModel
 {
     
     protected $name = 'shop_cart';
-    
 
-    
+
+    public function goodSpecsInventory()
+    {
+        return $this->hasOne(ShopGoodSpecsInventory::class,  'id','goods_specs_inventory_id');
+
+    }
+
+    public function goods()
+    {
+        return $this->hasOne(ShopGoods::class, 'id', 'shop_goods_id');
+
+    }
 }

+ 27 - 1
app/common/model/shops/ShopGoods.php

@@ -3,7 +3,8 @@ namespace app\common\model\shops;
 
 
 use app\common\model\BaseModel;
-
+use app\common\model\dict\DictData;
+use think\facade\Cache;
 
 
 /**
@@ -33,4 +34,29 @@ class ShopGoods extends BaseModel
         return $this->hasMany(ShopGoodSpecsInventory::class, 'shop_goods_id', 'id');
 
     }
+
+    public function getDeliveryTypeTextAttr($value,$data):string
+    {
+        $default = Cache::get('DELIVERY_TYPE');
+        if (!$default) {
+            $status = DictData::whereIn('type_value', 'delivery_type')->column('name','value');
+            Cache::set('DELIVERY_TYPE', json_encode($status,true),5);
+        } else {
+            $status = json_decode($default,true);
+        }
+        return $status[$data['delivery_type']] ?? '';
+    }
+
+    public function getShopGoodsTypeTextAttr($value,$data):string
+    {
+        $default = Cache::get('SHOP_GOODS_TYPE');
+        if (!$default) {
+            $status = DictData::whereIn('type_value', 'shop_goods_type')->column('name','value');
+            Cache::set('SHOP_GOODS_TYPE', json_encode($status,true),5);
+        } else {
+            $status = json_decode($default,true);
+        }
+        return $status[$data['shop_goods_type']] ?? '';
+    }
+
 }

+ 27 - 0
app/workerapi/controller/shops/CartController.php

@@ -2,11 +2,38 @@
 namespace app\workerapi\controller\shops;
 
 use app\workerapi\controller\BaseApiController;
+use app\workerapi\lists\shops\ShopCartLists;
+use app\workerapi\logic\shops\ShopCartLogic;
+use app\workerapi\validate\shops\ShopCartValidate;
 
 /**
  * 商城购物车
  */
 class CartController extends BaseApiController
 {
+    public function addCart()
+    {
+        $params = (new ShopCartValidate())->post()->goCheck('add',['worker_id'=>$this->userId]);
+        $result = ShopCartLogic::add($params);
+        if(false === $result ){
+            return $this->fail(ShopCartLogic::getError() ?: '系统错误');
+        }
+        return $this->success('加入购物车成功', [], 1, 0);
+    }
 
+    public function cartList()
+    {
+
+        return $this->dataLists(new ShopCartLists());
+    }
+
+    public function delCart()
+    {
+        $params = (new ShopCartValidate())->post()->goCheck('delete',['worker_id'=>$this->userId]);
+        $result = ShopCartLogic::delete($params);
+        if(false === $result ){
+            return $this->fail(ShopCartLogic::getError() ?: '系统错误');
+        }
+        return $this->success('删除购物车成功', [], 1, 0);
+    }
 }

+ 13 - 0
app/workerapi/controller/shops/GoodsController.php

@@ -2,11 +2,24 @@
 namespace app\workerapi\controller\shops;
 
 use app\workerapi\controller\BaseApiController;
+use app\workerapi\lists\shops\ShopGoodLists;
+use app\workerapi\logic\shops\ShopGoodsLogic;
+use app\workerapi\validate\shops\ShopGoodsValidate;
 
 /**
  * 商城商品
  */
 class GoodsController extends BaseApiController
 {
+    public function goodsList()
+    {
+        return $this->dataLists(new ShopGoodLists());
+    }
 
+
+    public function detail(){
+        $params = (new ShopGoodsValidate())->goCheck('detail');
+        $result = ShopGoodsLogic::detail($params);
+        return $this->data($result);
+    }
 }

+ 5 - 0
app/workerapi/controller/shops/HomeController.php

@@ -2,11 +2,16 @@
 namespace app\workerapi\controller\shops;
 
 use app\workerapi\controller\BaseApiController;
+use app\workerapi\lists\shops\ShopCategoryLists;
 
 /**
  * 商城首页
  */
 class HomeController extends BaseApiController
 {
+        public function shopCategoryList(){
+            return $this->dataLists(new ShopCategoryLists());
+        }
+
 
 }

+ 78 - 0
app/workerapi/lists/shops/ShopCartLists.php

@@ -0,0 +1,78 @@
+<?php
+namespace app\workerapi\lists\shops;
+
+
+use app\common\lists\ListsSearchInterface;
+use app\common\model\shops\ShopCart;
+use app\workerapi\lists\BaseWorkerDataLists;
+use think\db\Query;
+
+/**
+ * ShopCartLists列表
+ * Class ShopCartLists
+ * @package app\api\lists\goods_category
+ */
+class ShopCartLists extends BaseWorkerDataLists implements ListsSearchInterface
+{
+
+
+    /**
+     *
+     * @return array[]
+     */
+    public function setSearch(): array
+    {
+        return [
+            '=' => ['shop_goods_id', 'goods_specs_inventory_id'],
+
+        ];
+    }
+
+    public function querySearch():array
+    {
+        $where = [];
+        $where[] = ['worker_id', '=', $this->userId];
+        return $where;
+    }
+
+    /**
+     * @notes 获取列表
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @author whitef
+     * @date 2024/07/07 18:23
+     */
+    public function lists(): array
+    {
+        $lists = ShopCart::with([
+            'goods' => function(Query $query){
+                $query->field(['id','goods_name','delivery_type','shop_goods_type','goods_image','goods_status','specs_type','custom_attribute_items','company_name',])->append(['delivery_type_text','shop_goods_type_text']);
+            },
+            'goodSpecsInventory' => function(Query $query) {
+                $query->field(['id','remaining_inventory','specs','service_fee','service_total']);
+            }
+        ])
+            ->where($this->searchWhere)
+            ->where($this->querySearch())
+            ->field(['id', 'shop_goods_id' ,'goods_specs_inventory_id', 'number'])
+            ->select()
+            ->toArray();
+
+        return $lists;
+    }
+
+
+    /**
+     * @notes 获取数量
+     * @return int
+     * @author whitef
+     * @date 2024/07/07 18:23
+     */
+    public function count(): int
+    {
+        return ShopCart::where($this->searchWhere) ->where($this->querySearch())->count();
+    }
+
+}

+ 65 - 0
app/workerapi/lists/shops/ShopCategoryLists.php

@@ -0,0 +1,65 @@
+<?php
+namespace app\workerapi\lists\shops;
+
+
+use app\common\lists\ListsSearchInterface;
+use app\common\model\shops\ShopCategory;
+use app\workerapi\lists\BaseWorkerDataLists;
+
+/**
+ * GoodsCategory列表
+ * Class GoodsCategoryLists
+ * @package app\api\lists\goods_category
+ */
+class ShopCategoryLists extends BaseWorkerDataLists implements ListsSearchInterface
+{
+
+
+    /**
+     * @notes 设置搜索条件
+     * @return \string[][]
+     * @author likeadmin
+     * @date 2024/07/07 18:23
+     */
+    public function setSearch(): array
+    {
+        return [
+            '=' => ['category_type', 'name', 'is_goods', 'status'],
+
+        ];
+    }
+
+
+    /**
+     * @notes 获取列表
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @author whitef
+     * @date 2024/07/07 18:23
+     */
+    public function lists(): array
+    {
+        $lists = ShopCategory::where($this->searchWhere)
+            ->field(['id', 'pid' ,'picture', 'name'])
+            ->order(['weigh' => 'desc'])
+            ->select()
+            ->toArray();
+
+        return $lists;
+    }
+
+
+    /**
+     * @notes 获取数量
+     * @return int
+     * @author whitef
+     * @date 2024/07/07 18:23
+     */
+    public function count(): int
+    {
+        return ShopCategory::where($this->searchWhere)->count();
+    }
+
+}

+ 55 - 0
app/workerapi/lists/shops/ShopGoodLists.php

@@ -0,0 +1,55 @@
+<?php
+
+namespace app\workerapi\lists\shops;
+
+use app\common\enum\YesNoEnum;
+use app\common\lists\ListsSearchInterface;
+use app\common\model\shops\ShopGoods;
+use app\workerapi\lists\BaseWorkerDataLists;
+use think\db\Query;
+
+class ShopGoodLists extends BaseWorkerDataLists implements ListsSearchInterface
+{
+    /**
+     * @notes 设置搜索条件
+     * @return \string[][]
+     * @author likeadmin
+     * @date 2024/07/07 18:23
+     */
+    public function setSearch(): array
+    {
+        return [
+            '=' => ['goods_category_id', 'goods_status','is_recommend'],
+            '%like%' => ['goods_name'],
+        ];
+    }
+
+    public function lists(): array
+    {
+        $lists = ShopGoods::with(['goodSpecsInventory'=>function(Query $query){
+            $query->field(['id','shop_goods_id','remaining_inventory','service_fee','service_total']);
+        }])
+            ->where($this->searchWhere)
+            ->field(['id', 'delivery_type' ,'shop_goods_type', 'goods_name','goods_category_id','company_name','goods_image','goods_banners'])
+            ->when(isset($this->params['is_recommend']) && $this->params['is_recommend'],function (Query $query){
+                $query->order('is_recommend','desc');
+            })
+            ->where('goods_status',YesNoEnum::YES)
+            ->append(['delivery_type_text','shop_goods_type_text'])
+            ->order('id','desc')
+            ->select()
+            ->each(function($item){
+                $item['remaining_inventory'] =(int) $item->goodSpecsInventory()->sum('remaining_inventory');
+                $item['service_fee'] = $item->goodSpecsInventory()->min('service_fee');
+                unset($item['goodSpecsInventory']);
+            })
+            ->toArray();
+
+        return $lists;
+    }
+
+    public function count(): int
+    {
+        return ShopGoods::where($this->searchWhere)->where('goods_status',YesNoEnum::YES)->count();
+    }
+}

+ 83 - 0
app/workerapi/logic/shops/ShopCartLogic.php

@@ -0,0 +1,83 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+namespace app\workerapi\logic\shops;
+
+
+use app\common\model\shops\ShopCart;
+use app\common\model\shops\ShopGoods;
+use app\common\logic\BaseLogic;
+use app\common\model\shops\ShopGoodSpecsInventory;
+use think\db\Query;
+use think\facade\Db;
+
+
+/**
+ * ShopGoods逻辑
+ * Class ShopGoodsLogic
+ * @package app\adminapi\logic\shops
+ */
+class ShopCartLogic extends BaseLogic
+{
+
+
+    /**
+     * @notes 获取详情
+     * @param $params
+     * @return array
+     * @author likeadmin
+     * @date 2024/08/04 11:07
+     */
+    public static function add($params)
+    {
+        Db::startTrans();
+        try{
+            Db::commit();
+            $goodsModel = ShopGoods::findOrEmpty($params['shop_goods_id']);
+            if($goodsModel->isEmpty()){
+                throw new \Exception('商品不存在');
+            }
+            $goodSpecInventoryModel = ShopGoodSpecsInventory::where(['id'=>$params['goods_specs_inventory_id'],'shop_goods_id'=>$params['shop_goods_id']])->findOrEmpty();
+            if($goodSpecInventoryModel->isEmpty()){
+                throw new \Exception('规格不存在');
+            }
+           ShopCart::create([
+                'shop_goods_id' => $params['shop_goods_id'],
+                'goods_specs_inventory_id' => $params['goods_specs_inventory_id'],
+                'number' => $params['number'],
+                'worker_id' => $params['worker_id'],
+
+            ]);
+            Db::commit();
+            return true;
+        }catch(\Exception $e){
+            Db::rollback();
+            self::setError($e->getMessage());
+            return false;
+        }
+    }
+
+
+    public static function delete($params){
+        try{
+            ShopCart::whereIn('id',$params['ids'])->where('worker_id',$params['worker_id'])->delete();
+            Db::commit();
+            return true;
+        }catch (\Exception $e){
+            Db::rollback();
+            self::setError($e->getMessage());
+            return false;
+        }
+    }
+}

+ 47 - 0
app/workerapi/logic/shops/ShopGoodsLogic.php

@@ -0,0 +1,47 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+namespace app\workerapi\logic\shops;
+
+
+use app\common\model\shops\ShopGoods;
+use app\common\logic\BaseLogic;
+use app\common\model\shops\ShopGoodSpecsInventory;
+use think\db\Query;
+use think\facade\Db;
+
+
+/**
+ * ShopGoods逻辑
+ * Class ShopGoodsLogic
+ * @package app\adminapi\logic\shops
+ */
+class ShopGoodsLogic extends BaseLogic
+{
+
+
+    /**
+     * @notes 获取详情
+     * @param $params
+     * @return array
+     * @author likeadmin
+     * @date 2024/08/04 11:07
+     */
+    public static function detail($params): array
+    {
+        return ShopGoods::with(['goodSpecsInventory'=>function(Query $query){
+            $query->field(['id','shop_goods_id','remaining_inventory','specs','service_fee','service_total']);
+            }])->hidden(['create_time','update_time'])->findOrEmpty($params['id'])->toArray();
+    }
+}

+ 38 - 0
app/workerapi/validate/shops/ShopCartValidate.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace app\workerapi\validate\shops;
+
+use app\common\validate\BaseValidate;
+
+class ShopCartValidate  extends BaseValidate
+{
+
+    protected $rule = [
+        'shop_goods_id' => 'require',
+        'goods_specs_inventory_id' => 'require',
+        'number' => 'require|min:1',
+        'ids' => 'require|array',
+    ];
+
+    protected $field = [
+        'shop_goods_id' => '商品id',
+        'goods_specs_inventory_id' => '规格ID',
+        'number' => '数量',
+    ];
+
+    public function sceneAdd()
+    {
+        return $this->only(['shop_goods_id', 'goods_specs_inventory_id', 'number']);
+    }
+
+    /**
+     * @notes 删除场景
+     * @return ShopGoodsValidate
+     * @author likeadmin
+     * @date 2024/08/04 11:07
+     */
+    public function sceneDelete()
+    {
+        return $this->only(['ids']);
+    }
+}

+ 86 - 0
app/workerapi/validate/shops/ShopGoodsValidate.php

@@ -0,0 +1,86 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+namespace app\workerapi\validate\shops;
+
+
+use app\common\validate\BaseValidate;
+
+
+/**
+ * ShopGoods验证器
+ * Class ShopGoodsValidate
+ * @package app\adminapi\validate\shops
+ */
+class ShopGoodsValidate extends BaseValidate
+{
+
+     /**
+      * 设置校验规则
+      * @var string[]
+      */
+    protected $rule = [
+        'id' => 'require',
+        'delivery_type' => 'require',
+        'shop_goods_type' => 'require',
+        'goods_category_ids' => 'require',
+        'goods_name' => 'require',
+        'goods_image' => 'require',
+        'goods_status' => 'require',
+        'goodSpecsInventory' => 'require|array',
+    ];
+
+
+    /**
+     * 参数描述
+     * @var string[]
+     */
+    protected $field = [
+        'id' => 'id',
+        'delivery_type' => '配送类型',
+        'shop_goods_type' => '商品类别',
+        'goods_category_ids' => '服务类目',
+//        'goods_category_id' => '服务产品',
+        'goods_name' => '商品名称',
+        'goods_image' => '商品图片',
+        'goods_status' => '商品状态',
+        'goodSpecsInventory' => '规格库存'
+    ];
+
+
+
+    /**
+     * @notes 删除场景
+     * @return ShopGoodsValidate
+     * @author likeadmin
+     * @date 2024/08/04 11:07
+     */
+    public function sceneDelete()
+    {
+        return $this->only(['id']);
+    }
+
+
+    /**
+     * @notes 详情场景
+     * @return ShopGoodsValidate
+     * @author likeadmin
+     * @date 2024/08/04 11:07
+     */
+    public function sceneDetail()
+    {
+        return $this->only(['id']);
+    }
+
+}