添加本地生活购物车功能

This commit is contained in:
wangxiaowei 2024-10-28 14:53:43 +08:00
parent 888b4e3c99
commit 881fded453
4 changed files with 128 additions and 8 deletions

@ -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));
}
}

@ -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];
}
}

@ -0,0 +1,40 @@
<?php
namespace app\api\validate;
use app\common\basics\Validate;
use app\common\model\LocalStore\LocalStoreGoodsCart;
class LocalStoreGoodsCartValidate extends Validate
{
protected $rule = [
'cart_id' => '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;
}
}

@ -0,0 +1,10 @@
<?php
namespace app\common\model\localstore;
use app\common\basics\Models;
class LocalStoreGoodsCart extends Models
{
}