修改本地生活购物车商品数量操作

This commit is contained in:
wangxiaowei 2024-10-30 16:35:45 +08:00
parent e621324b26
commit 2ae5cbead4
4 changed files with 72 additions and 3 deletions

@ -4,6 +4,7 @@ namespace app\api\controller;
use app\api\logic\LocalStoreGoodsCartLogic;
use app\api\logic\LocalStoreGoodsLogic;
use app\api\validate\CartValidate;
use app\api\validate\LocalStoreGoodsCartValidate;
use app\api\validate\LocalStoreGoodsValidate;
use app\common\basics\Api;
@ -12,7 +13,7 @@ use app\common\server\JsonServer;
class LocalStoreGoods extends Api
{
public $like_not_need_login = ['lists', 'addCart', 'getCartGoods', 'num'];
public $like_not_need_login = ['lists', 'addCart', 'getCartGoods', 'num', 'modifyCart'];
// 商品列表
public function lists()
@ -128,4 +129,19 @@ class LocalStoreGoods extends Api
$form = $this->request->post();
return JsonServer::success('', LocalStoreGoodsCartLogic::cartNum($form, $this->user_id));
}
public function modifyCart()
{
$form = $this->request->post();
(new LocalStoreGoodsCartValidate())->goCheck('change', ['user_id' => $this->user_id]);
try {
$result = LocalStoreGoodsCartLogic::modifyCart($form, $this->user_id);;
if ($result) {
return JsonServer::success('操作成功');
}
} catch (\Exception $e) {
return JsonServer::error($e->getMessage());
}
}
}

@ -91,4 +91,49 @@ class LocalStoreGoodsCartLogic extends Logic
->sum('c.goods_num');
return ['num' => $num ?? 0];
}
public static function modifyCart($post, $user_id)
{
Db::startTrans();
try {
if ($post['cart_id']) {
$cart = LocalStoreGoodsCart::getSelfCart($user_id, $post['cart_id']);
if (!empty($cart)) {
if ($cart['item_id'] == 0) {
// 单规格商品
$item = LocalStoreGoods::where('id', $cart['lsg_id'])->field('price')->find();
if ($cart['goods_num'] + $post['goods_num'] > 999) {
throw new \Exception('商品数量超出限制');
}
} else {
// 多规格商品
$item = LocalStoreGoodsItem::where('id', $cart['item_id'])->field('price, stock')->find();
if ($cart['goods_num'] + $post['goods_num'] > $item['stock']) {
throw new \Exception('商品数量超出限制');
}
}
$price = number_format($post['num'] * $item['price'], 2, '.', '');
$goods_num = $cart['goods_num'] + $post['goods_num'];
LocalStoreGoodsCart::where('id', $post['cart_id'])->update([
'goods_num' => $goods_num
]);
} else {
throw new \Exception('商品信息不存在');
}
} else {
throw new \Exception('ID不存在');
}
Db::commit();
return [
'total_amount' => $price
];
} catch (Exception $e) {
Db::rollback();
self::$error = $e->getMessage();
return false;
}
}
}

@ -3,8 +3,7 @@
namespace app\api\validate;
use app\common\basics\Validate;
use app\common\model\LocalStore\LocalStoreGoodsCart;
use app\common\model\localstore\LocalStoreGoodsCart;
class LocalStoreGoodsCartValidate extends Validate
{

@ -29,4 +29,13 @@ class LocalStoreGoodsCart extends Models
return LocalStoreGoodsItem::where('id', $data['item_id'])->field('image, spec_value_str, price')->find()->toArray();
}
}
public static function getSelfCart($user_id, $cart_id = 0)
{
if ($cart_id > 0) {
return self::where(['id' => $cart_id, 'user_id' => $user_id, 'is_delete' => 0])->find();
} else {
return self::where(['user_id' => $user_id, 'is_delete' => 0])->select();
}
}
}