ShopCartModel.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace app\common\model;
  3. use app\api\services\UserServices;
  4. use app\common\model\TimeModel;
  5. //use app\model\RedPoolModel;
  6. //use app\model\RedStorePoolModel;
  7. use app\common\model\UserDataModel;
  8. use think\Exception;
  9. use think\facade\Db;
  10. use think\Model;
  11. class ShopCartModel extends Model
  12. {
  13. protected $name = "shop_cart";
  14. // 购物车列表
  15. public function cartList($uid){
  16. $list = self::alias('c')
  17. ->where('c.uid', $uid)
  18. ->where('c.is_del', 0)
  19. ->leftJoin('shop_goods g', 'c.goods_id = g.goods_id')
  20. ->rightJoin('shop_goods_spec_relation r', 'c.goods_id = r.goods_id')
  21. ->field('c.id,c.goods_id,c.num,g.goods_name,g.goods_sn,g.goods_img,g.rebate_score,g.created_time,g.price,g.spec_name,r.spec_id')
  22. ->order('c.create_time desc')
  23. ->select()->toArray();
  24. return $list;
  25. }
  26. // 编辑购物车数量
  27. public function editCart($uid, $cart_id, $num){
  28. self::where('id', $cart_id)->where('uid', $uid)->save([
  29. 'num'=>$num
  30. ]);
  31. }
  32. // 添加到购物车
  33. public function addCart($uid, $goods_id, $num){
  34. if ($info = self::where('uid', $uid)->where('goods_id', $goods_id)->find()){
  35. self::where('id', $info['id'])->save(['create_time'=>sr_getcurtime(time()), 'num'=>$info['num']+$num]);
  36. }else{
  37. self::insert([
  38. 'goods_id'=>$goods_id,
  39. 'uid'=>$uid,
  40. 'create_time'=>sr_getcurtime(time()),
  41. 'num'=>$num
  42. ]);
  43. }
  44. }
  45. // 删除购物车
  46. public function delCart($uid, $cart_ids){
  47. self::where('id', 'in', $cart_ids)->delete();
  48. }
  49. }