获取本地生活购物车商品

This commit is contained in:
wangxiaowei 2024-10-29 15:37:34 +08:00
parent 195b3572fb
commit dd9c1e0db4
4 changed files with 110 additions and 60 deletions

@ -2,16 +2,17 @@
namespace app\api\controller;
use app\api\logic\LocalStoreGoodsCartLogic;
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
{
public $like_not_need_login = ['lists', 'addCart', 'num'];
public $like_not_need_login = ['lists', 'addCart', 'getCartGoods', 'num'];
// 商品列表
public function lists()
@ -105,7 +106,7 @@ class LocalStoreGoods extends Api
(new LocalStoreGoodsCartValidate())->goCheck('add', ['user_id' => $this->user_id]);
try {
$result = LocalStoreGoodsLogic::addCart($form, $this->user_id);
$result = LocalStoreGoodsCartLogic::addCart($form, $this->user_id);
if ($result) {
return JsonServer::success('操作成功');
}
@ -118,13 +119,13 @@ class LocalStoreGoods extends Api
public function getCartGoods()
{
$form = $this->request->post();
LocalStoreGoodsCartLogic::getCartGoods($form, $this->user_id);
}
// 获取购物车数量
public function num()
{
$form = $this->request->post();
return JsonServer::success('', LocalStoreGoodsLogic::cartNum($form, $this->user_id));
return JsonServer::success('', LocalStoreGoodsCartLogic::cartNum($form, $this->user_id));
}
}

@ -0,0 +1,82 @@
<?php
namespace app\api\logic;
use app\common\basics\Logic;
use app\common\model\localstore\LocalStoreGoods;
use app\common\model\localstore\LocalStoreGoodsCart;
use app\common\model\localstore\LocalStoreGoodsItem;
use think\Exception;
use think\facade\Db;
class LocalStoreGoodsCartLogic extends Logic
{
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();
// 验证商品库存
$item = LocalStoreGoodsItem::where(['lstore_id' => $post['lstore_id'], 'goods_id' => $post['lsg_id']])->field('stock')->find();
// 普通商品
if ($post['item_id'] == 0 && $post['goods_num'] > 999) {
throw new \Exception('商品库存不足');
}
if ($post['item_id'] > 0 && ($post['goods_num'] + $cart['goods_num']) > $item['stock']) {
throw new \Exception('商品库存不足');
}
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 getCartGoods($post, $user_id)
{
$cart = LocalStoreGoodsCart::alias('c')
->join('local_store_goods g', 'c.lsg_id = g.id')
->where(['c.user_id' => $user_id, 'c.lstore_id' => $post['lstore_id'], 'c.is_delete' => 0, 'g.state' => 1, 'g.audit_status' => 1, 'g.delete_time' => 0])
->field('c.id, c.item_id, c.goods_num, g.id as lsg_id, g.title, g.price')
->append(['pic', 'item'])
->select();
}
public static function cartNum($post, $user_id)
{
$num = LocalStoreGoodsCart::alias('c')
->join('local_store_goods g', 'c.lsg_id = g.id')
->where(['c.user_id' => $user_id, 'c.lstore_id' => $post['lstore_id'], 'c.is_delete' => 0, 'g.state' => 1, 'g.audit_status' => 1, 'g.delete_time' => 0])
->sum('c.goods_num');
return ['num' => $num ?? 0];
}
}

@ -380,59 +380,4 @@ 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();
// 验证商品库存
$item = LocalStoreGoodsItem::where(['lstore_id' => $post['lstore_id'], 'goods_id' => $post['lsg_id']])->field('stock')->find();
// 普通商品
if ($post['item_id'] == 0 && $post['goods_num'] > 999) {
throw new \Exception('商品库存不足');
}
if ($post['item_id'] > 0 && ($post['goods_num'] + $cart['goods_num']) > $item['stock']) {
throw new \Exception('商品库存不足');
}
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];
}
}

@ -6,5 +6,27 @@ use app\common\basics\Models;
class LocalStoreGoodsCart extends Models
{
public function goods()
{
return $this->hasOne(LocalStoreGoods::class, 'id', 'lsg_id');
}
// 商品规格为空,获取封面图
public function getPicAttr($value, $data)
{
if ($data['item_id'] == 0) {
$pic = LocalStoreGoodsPic::where('lsg_id', $data['lsg_id'])->field('url')->find();
if ($pic) {
return $pic['url'];
}
}
}
// 获取带有商品规格的属性
public function getItemAttr($value, $data)
{
if ($data['item_id'] > 0) {
return LocalStoreGoodsItem::where('id', $data['item_id'])->field('image, spec_value_str, price')->find()->toArray();
}
}
}