You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. class Cart extends BaseModel
  4. {
  5. //产品模型
  6. /**
  7. * 关联到模型的数据表
  8. *
  9. * @var string
  10. */
  11. protected $table = 'cart';
  12. /**
  13. * 表明模型是否应该被打上时间戳
  14. * 默认情况下,Eloquent 期望 created_at 和updated_at 已经存在于数据表中,如果你不想要这些 Laravel 自动管理的数据列,在模型类中设置 $timestamps 属性为 false
  15. *
  16. * @var bool
  17. */
  18. public $timestamps = false;
  19. //protected $guarded = []; //$guarded包含你不想被赋值的字段数组。
  20. //protected $fillable = ['name']; //定义哪些字段是可以进行赋值的,与$guarded相反
  21. /**
  22. * The connection name for the model.
  23. * 默认情况下,所有的 Eloquent 模型使用应用配置中的默认数据库连接,如果你想要为模型指定不同的连接,可以通过 $connection 属性来设置
  24. * @var string
  25. */
  26. //protected $connection = 'connection-name';
  27. //购物车商品类型
  28. const CART_GENERAL_GOODS = 0; // 普通商品
  29. const CART_GROUP_BUY_GOODS = 1; // 团购商品
  30. const CART_AUCTION_GOODS = 2; // 拍卖商品
  31. const CART_SNATCH_GOODS = 3; // 夺宝奇兵
  32. const CART_EXCHANGE_GOODS = 4; // 积分商城
  33. //获取列表
  34. public static function getList($uid)
  35. {
  36. $goods = self::join('goods', 'goods.id', '=', 'cart.goods_id')
  37. ->where('cart.user_id', $uid)
  38. ->where('goods.status', 0)
  39. ->select('cart.*')
  40. ->get();
  41. if($goods)
  42. {
  43. foreach ($goods as $key => $value)
  44. {
  45. //订货数量大于0
  46. if ($value->goods_number > 0)
  47. {
  48. $goods->goods_price = $goods_price = Goods::get_final_price($value->goods_id);
  49. //更新购物车中的商品数量
  50. self::where('id', $value->id)->update(array('goods_price' => $goods_price));
  51. }
  52. }
  53. }
  54. return $goods->toArray();
  55. }
  56. /**
  57. * 添加商品到购物车
  58. *
  59. * @access public
  60. * @param integer $goods_id 商品编号
  61. * @param integer $num 商品数量
  62. * @param json $property 规格值对应的id json数组
  63. * @return boolean
  64. */
  65. public static function cartAdd(array $attributes)
  66. {
  67. extract($attributes);
  68. //获取商品信息
  69. $good = Goods::where(['goods_id' => $goods_id, 'status' => 0])->first();
  70. if (!$good)
  71. {
  72. return '商品不存在';
  73. }
  74. if (isset($property) && json_decode($property,true))
  75. {
  76. $property = json_decode($property,true);
  77. }
  78. else
  79. {
  80. $property = [];
  81. }
  82. }
  83. public static function getOne($id)
  84. {
  85. $where['id'] = $id;
  86. $goods = self::where($where)->first()->toArray();
  87. return $goods;
  88. }
  89. public static function add(array $data)
  90. {
  91. if ($id = self::insertGetId($data))
  92. {
  93. return $id;
  94. }
  95. return false;
  96. }
  97. public static function modify($where, array $data)
  98. {
  99. if (self::where($where)->update($data))
  100. {
  101. return true;
  102. }
  103. return false;
  104. }
  105. //删除一条记录
  106. public static function remove($id)
  107. {
  108. if (!self::whereIn('id', explode(',', $id))->delete())
  109. {
  110. return false;
  111. }
  112. return true;
  113. }
  114. /**
  115. * 清空购物车
  116. *
  117. * @param int $type 类型:默认普通商品
  118. */
  119. public static function clearCart($user_id)
  120. {
  121. self::where('user_id',$user_id)->delete();
  122. return true;
  123. }
  124. //购物车总价格
  125. public static function TotalPrice($user_id)
  126. {
  127. $goods = self::where('user_id',$user_id)->get();
  128. $total = 0;
  129. foreach ($goods as $k => $v)
  130. {
  131. $total += ($v['goods_number'] * $v['goods_price']);
  132. }
  133. return (float)$total;
  134. }
  135. //购物车商品总数量
  136. public static function TotalGoodsCount($user_id)
  137. {
  138. return self::where('user_id',$user_id)->sum('goods_number');
  139. }
  140. }