| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- declare (strict_types = 1);
- namespace app\model\api;
- use think\Model;
- /**
- * @mixin \think\Model
- */
- class StoreCart extends Model
- {
- protected $name = 'store_cart';
- /**
- * 获取购物车列表
- * @param int $uid
- * @return array
- */
- public function getCartList($uid)
- {
- $list = $this->alias('c')
- ->field('c.*,p.title,p.image,p.price as product_price')
- ->leftJoin('store_product p', 'p.id = c.product_id')
- ->where('c.uid', $uid)
- ->select()
- ->toArray();
- return $list;
- }
- /**
- * 添加购物车
- * @param array $data
- * @return int|false 成功返回购物车ID,失败返回false
- */
- public function addToCart($data)
- {
- // 检查是否已存在
- $exist = $this->where('uid', $data['uid'])
- ->where('product_id', $data['product_id'])
- ->where('product_attr_unique', $data['product_attr_unique'] ?? '')
- ->find();
- if ($exist) {
- // 更新数量
- $result = $this->where('id', $exist['id'])->inc('cart_num', $data['cart_num'])->update();
- return $result ? $exist['id'] : false;
- } else {
- // 新增
- $result = $this->insert($data);
- return $result ? $this->getLastInsID() : false;
- }
- }
- /**
- * 修改购物车数量
- * @param int $id
- * @param int $cartNum
- * @param int $uid
- * @return bool
- */
- public function updateCartNum($id, $cartNum, $uid)
- {
- return $this->where('id', $id)->where('uid', $uid)->update(['cart_num' => $cartNum]);
- }
- /**
- * 删除购物车
- * @param array $ids
- * @param int $uid
- * @return bool
- */
- public function deleteCart($ids, $uid)
- {
- return $this->where('uid', $uid)->whereIn('id', $ids)->delete();
- }
- }
|