添加购物车商品操作

This commit is contained in:
lewis 2025-07-23 10:51:11 +08:00
parent 4e17620cbe
commit 86dd75b3cf
3 changed files with 71 additions and 3 deletions

View File

@ -45,9 +45,33 @@ class CartController extends BaseLikeAdminController
return $this->data(['count' => $data]);
}
public function change()
public function productList(CartLogic $cartLogic)
{
$params['keyword'] = $this->request->get('keyword');
$params['uid'] = $this->request->user->id;
$params['page'] = $this->request->page();
$params['limit'] = $this->request->limit();
$data = $cartLogic->productList($params);
return $this->data($data);
}
public function changeNumber(CartLogic $cartLogic)
{
$params['uid'] = $this->request->user->id;
$params['id'] = $this->request->post('id');
$params['nums'] = $this->request->post('nums');
$cartLogic->changeNumber($params);
return $this->success('修改成功', [], 1, 1);
}
public function changePeopleNumber(CartLogic $cartLogic)
{
$params['uid'] = $this->request->user->id;
$params['people_number'] = $this->request->post('people_number');
if ($cartLogic->changePeopleNumber($params)) {
return $this->success('修改成功', [], 1, 1);
}
return $this->fail('修改失败');
}
}

View File

@ -21,8 +21,7 @@ class CartLogic extends BaseLogic
$dishesIds = Dishes::whereLike('name', "%{$params['keyword']}%")->column('id');
$query->whereIn('dishes_id', $dishesIds);
}
$data = $query->page($params['page'], $params['limit'])->select()->toArray();
return $data;
return $query->page($params['page'], $params['limit'])->select()->toArray();
}
public function count($params)
@ -49,6 +48,9 @@ class CartLogic extends BaseLogic
Db::startTrans();
try {
$cart = new Cart();
if (empty($params['people_number'])) {
$params['people_number'] = 1;
}
if (!$cart->save($params)) {
throw new \Exception('添加购物车失败');
}
@ -78,4 +80,41 @@ class CartLogic extends BaseLogic
}
}
public function productList($params)
{
$query = CartProduct::alias('t1')
->with(['dishes', 'product', 'unit'])
->join('cart t2', 't1.cart_id = t2.id')
->where('t2.uid', $params['uid'])
->where('t2.buy_now', 0)
->where('t2.paid', 0);
if (!empty($params['keyword'])) {
$dishesIds = Dishes::whereLike('name', "%{$params['keyword']}%")->column('id');
$query->whereIn('t2.dishes_id', $dishesIds);
}
return $query->page($params['page'], $params['limit'])->select()->toArray();
}
public function changePeopleNumber($params)
{
Db::startTrans();
try {
$cartWhere = ['uid' => $params['uid'], 'paid' => 0, 'buy_now' => 0];
Cart::update(['people_number' => $params['people_number']], $cartWhere);
$cartIds = Cart::where($cartWhere)->column('id');
CartProduct::whereIn('cart_id', $cartIds)->where('uid', $params['uid'])->update(['people_number' => $params['people_number']]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
return false;
}
}
public function changeNumber($params)
{
CartProduct::whereIn('id', $params['id'])->where('uid', $params['uid'])->update(['nums' => $params['nums']]);
return true;
}
}

View File

@ -11,6 +11,11 @@ class CartProduct extends BaseModel
protected $name = 'cart_product';
protected $deleteTime = 'delete_time';
public function dishes()
{
return $this->hasOne(Dishes::class, 'id', 'dishes_id')->bind(['dishes_name' => 'name']);
}
public function product()
{
return $this->hasOne(Product::class, 'id', 'product_id')->bind(['product_name' => 'name', 'image', 'product_type']);