StoreCart.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\api;
  4. use think\Model;
  5. /**
  6. * @mixin \think\Model
  7. */
  8. class StoreCart extends Model
  9. {
  10. protected $name = 'store_cart';
  11. /**
  12. * 获取购物车列表
  13. * @param int $uid
  14. * @return array
  15. */
  16. public function getCartList($uid)
  17. {
  18. $list = $this->alias('c')
  19. ->field('c.*,p.title,p.image,p.price as product_price')
  20. ->leftJoin('store_product p', 'p.id = c.product_id')
  21. ->where('c.uid', $uid)
  22. ->select()
  23. ->toArray();
  24. return $list;
  25. }
  26. /**
  27. * 添加购物车
  28. * @param array $data
  29. * @return bool
  30. */
  31. public function addToCart($data)
  32. {
  33. // 检查是否已存在
  34. $exist = $this->where('uid', $data['uid'])
  35. ->where('product_id', $data['product_id'])
  36. ->where('cart_num', $data['cart_num'])
  37. ->find();
  38. if ($exist) {
  39. // 更新数量
  40. return $this->where('id', $exist['id'])->inc('cart_num', $data['cart_num'])->update();
  41. } else {
  42. // 新增
  43. return $this->insert($data);
  44. }
  45. }
  46. /**
  47. * 修改购物车数量
  48. * @param int $id
  49. * @param int $cartNum
  50. * @param int $uid
  51. * @return bool
  52. */
  53. public function updateCartNum($id, $cartNum, $uid)
  54. {
  55. return $this->where('id', $id)->where('uid', $uid)->update(['cart_num' => $cartNum]);
  56. }
  57. /**
  58. * 删除购物车
  59. * @param array $ids
  60. * @param int $uid
  61. * @return bool
  62. */
  63. public function deleteCart($ids, $uid)
  64. {
  65. return $this->where('uid', $uid)->whereIn('id', $ids)->delete();
  66. }
  67. }