diff --git a/app/api/controller/LocalStoreGoods.php b/app/api/controller/LocalStoreGoods.php index 1a95782a..84d2df54 100755 --- a/app/api/controller/LocalStoreGoods.php +++ b/app/api/controller/LocalStoreGoods.php @@ -3,8 +3,10 @@ namespace app\api\controller; use app\api\logic\LocalStoreGoodsLogic; +use app\api\validate\LocalStoreGoodsCartValidate; use app\api\validate\LocalStoreGoodsValidate; use app\common\basics\Api; +use app\common\model\localstore\LocalStoreGoodsCart; use app\common\server\JsonServer; class LocalStoreGoods extends Api @@ -95,4 +97,27 @@ class LocalStoreGoods extends Api } return JsonServer::error('操作失败'); } + + // 将商品加入购物车 + public function addCart() + { + $form = $this->request->post(); + (new LocalStoreGoodsCartValidate())->goCheck('add', ['user_id' => $this->user_id]); + + try { + $result = LocalStoreGoodsLogic::addCart($form, $this->user_id); + if ($result) { + return JsonServer::success('操作成功'); + } + } catch (\Exception $e) { + return JsonServer::error($e->getMessage()); + } + } + + // 获取购物车数量 + public function num() + { + $form = $this->request->post(); + return JsonServer::success('', LocalStoreGoodsLogic::cartNum($form, $this->user_id)); + } } \ No newline at end of file diff --git a/app/api/logic/LocalStoreGoodsLogic.php b/app/api/logic/LocalStoreGoodsLogic.php index e5ca80d8..8bd5b172 100644 --- a/app/api/logic/LocalStoreGoodsLogic.php +++ b/app/api/logic/LocalStoreGoodsLogic.php @@ -3,13 +3,15 @@ namespace app\api\logic; use app\common\basics\Logic; +use app\common\enum\LocalStoreGoodsEnum; use app\common\model\localstore\LocalStoreGoods; -use app\common\model\localstore\LocalStoreGoodsPic; +use app\common\model\localstore\LocalStoreGoodsCart; use app\common\model\localstore\LocalStoreGoodsItem; +use app\common\model\localstore\LocalStoreGoodsPic; use app\common\model\localstore\LocalStoreGoodsSpec; use app\common\model\localstore\LocalStoreGoodsSpecValue; -use app\common\enum\LocalStoreGoodsEnum; use app\common\server\ConfigServer; +use think\Exception; use think\facade\Config; use think\facade\Db; @@ -84,9 +86,9 @@ class LocalStoreGoodsLogic extends Logic 'fail_goods_count' => $fail_goods_count, 'count' => $count, 'lists' => $list, - 'more' => is_more($count, $get['page_no'], $get['page_size']), - 'page_no' => $get['page_no'], - 'page_size' => $get['page_size'] + 'more' => is_more($count, $get['page_no'], $get['page_size']), + 'page_no' => $get['page_no'], + 'page_size' => $get['page_size'] ]; return $result; @@ -315,11 +317,11 @@ class LocalStoreGoodsLogic extends Logic public static function delGoods($form) { - $result = LocalStoreGoods::where([ + $result = LocalStoreGoods::where([ 'id' => $form['id'] - ])->update(['delete_time' => time()]); + ])->update(['delete_time' => time()]); - return $result; + return $result; } // 添加规格 @@ -378,4 +380,47 @@ class LocalStoreGoodsLogic extends Logic LocalStoreGoodsItem::insertAll($data); } } + + public static function addCart($post, $user_id) + { + Db::startTrans(); + try { + // 验证商品 + $goods = LocalStoreGoods::where(['id' => $post['lsg_id'], 'state' => 1, 'audit_status' => 1, 'delete_time' => 0])->find(); + if (empty($goods)) { + throw new \Exception('商品信息错误'); + } + + $cart = LocalStoreGoodsCart::where(['user_id' => $user_id, 'lstore_id' => $post['lstore_id'], 'lsg_id' => $post['lsg_id'], 'item_id' => $post['item_id'], 'is_delete' => 0])->find(); + if ($cart) { + // 如果存在于购物车,就增加数量 + LocalStoreGoodsCart::where(['user_id' => $user_id, 'lstore_id' => $post['lstore_id'], 'lsg_id' => $post['lsg_id'], 'item_id' => $post['item_id'], 'is_delete' => 0]) + ->update([ + 'goods_num' => $post['goods_num'] + $cart['goods_num'], + ]); + } else { + // 购物车创建商品 + LocalStoreGoodsCart::create([ + 'user_id' => $user_id, + 'lstore_id' => $post['lstore_id'], + 'lsg_id' => $post['lsg_id'], + 'item_id' => $post['item_id'], + 'goods_num' => $post['goods_num'], + 'create_time' => time() + ]); + } + Db::commit(); + return true; + } catch (Exception $e) { + Db::rollback(); + self::$error = $e->getMessage(); + return false; + } + } + + public static function cartNum($post, $user_id) + { + $num = LocalStoreGoodsCart::where(['user_id' => $user_id, 'lstore_id' => $post['lstore_id'], 'is_delete' => 0])->sum('goods_num'); + return ['num' => $num ?? 0]; + } } \ No newline at end of file diff --git a/app/api/validate/LocalStoreGoodsCartValidate.php b/app/api/validate/LocalStoreGoodsCartValidate.php new file mode 100755 index 00000000..befa9858 --- /dev/null +++ b/app/api/validate/LocalStoreGoodsCartValidate.php @@ -0,0 +1,40 @@ + 'require|checkCart', + 'goods_num' => 'require|integer|gt:0', + 'item_id' => 'require', + ]; + + protected $message = [ + 'item_id' => '请选择商品', + 'goods_num.require' => '商品数量不能为0', + 'goods_num.gt' => '商品数量需大于0', + 'goods_num.integer' => '商品数量需为整数', + 'cart_id.require' => '参数错误', + ]; + + protected $scene = [ + 'add' => ['item_id', 'goods_num'], + 'del' => ['cart_id'], + 'change' => ['cart_id', 'goods_num'], + ]; + + + protected function checkCart($value, $rule, $data) + { + $cart = LocalStoreGoodsCart::where(['id' => $value, 'user_id' => $data['user_id']])->find(); + if (!$cart) { + return '购物车不存在'; + } + return true; + } +} \ No newline at end of file diff --git a/app/common/model/localstore/LocalStoreGoodsCart.php b/app/common/model/localstore/LocalStoreGoodsCart.php new file mode 100644 index 00000000..d162bc4d --- /dev/null +++ b/app/common/model/localstore/LocalStoreGoodsCart.php @@ -0,0 +1,10 @@ +