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.

228 lines
6.8 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
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
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
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. use App\Common\ReturnData;
  4. class Cart extends BaseModel
  5. {
  6. //购物车模型
  7. /**
  8. * 关联到模型的数据表
  9. *
  10. * @var string
  11. */
  12. protected $table = 'cart';
  13. public $timestamps = false;
  14. //购物车商品类型
  15. const CART_GENERAL_GOODS = 0; // 普通商品
  16. const CART_GROUP_BUY_GOODS = 1; // 团购商品
  17. const CART_AUCTION_GOODS = 2; // 拍卖商品
  18. const CART_SNATCH_GOODS = 3; // 夺宝奇兵
  19. const CART_EXCHANGE_GOODS = 4; // 积分商城
  20. //获取列表
  21. public static function getList(array $param)
  22. {
  23. extract($param); //参数:limit,offset
  24. $model = self::join('goods', 'goods.id', '=', 'cart.goods_id')
  25. ->where('cart.user_id', $user_id)
  26. ->where('goods.status', Goods::STATUS)
  27. ->select('cart.*','goods.id as goods_id','goods.title','goods.sn','goods.price as goods_price','goods.market_price','goods.litpic','goods.goods_number as stock','goods.promote_start_date','goods.promote_price','goods.promote_end_date');
  28. $res['count'] = $model->count();
  29. $res['list'] = array();
  30. if($res['count']>0)
  31. {
  32. $res['list'] = $model->get();
  33. foreach ($res['list'] as $k => $v)
  34. {
  35. $res['list'][$k]->is_promote = 0;
  36. if(Goods::bargain_price($v->goods_price,$v->promote_start_date,$v->promote_end_date) > 0){$res['list'][$k]->is_promote = 1;}
  37. //订货数量大于0
  38. if ($v->goods_number > 0)
  39. {
  40. $res['list'][$k]->final_price = Goods::get_final_price($v->goods_id); //商品最终价格
  41. $res['list'][$k]->goods_detail_url = route('weixin_goods_detail',array('id'=>$v->goods_id));
  42. //更新购物车中的商品数量
  43. //self::where('id', $v->id)->update(array('price' => $goods_price));
  44. }
  45. }
  46. }
  47. else
  48. {
  49. return false;
  50. }
  51. return $res;
  52. }
  53. public static function getOne($where)
  54. {
  55. $goods = self::where($where)->first();
  56. return $goods;
  57. }
  58. public static function add(array $data)
  59. {
  60. if ($id = self::insertGetId($data))
  61. {
  62. return $id;
  63. }
  64. return false;
  65. }
  66. public static function modify($where, array $data)
  67. {
  68. if (self::where($where)->update($data))
  69. {
  70. return true;
  71. }
  72. return false;
  73. }
  74. //删除一条记录
  75. public static function remove($id,$user_id)
  76. {
  77. if(!is_array($id)){$id = explode(',', $id);}
  78. if (self::whereIn('id', $id)->where('user_id',$user_id)->delete() === false)
  79. {
  80. return false;
  81. }
  82. return true;
  83. }
  84. /**
  85. * 添加商品到购物车
  86. *
  87. * @access public
  88. * @param integer $goods_id 商品编号
  89. * @param integer $num 商品数量
  90. * @param json $property 规格值对应的id json数组
  91. * @return boolean
  92. */
  93. public static function cartAdd(array $attributes)
  94. {
  95. extract($attributes);
  96. //获取商品信息
  97. $goods = Goods::where(['id' => $goods_id, 'status' => Goods::STATUS])->first();
  98. if (!$goods)
  99. {
  100. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'商品不存在');
  101. }
  102. //判断库存 是否足够
  103. if($goods['goods_number']<$goods_number)
  104. {
  105. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'库存不足');
  106. }
  107. //判断购物车商品数
  108. if(Cart::where(['user_id'=>$user_id])->count() >= 20)
  109. {
  110. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'购物车商品最多20件');
  111. }
  112. //查看是否已经有购物车插入记录
  113. $where = array(
  114. 'user_id' => $user_id,
  115. 'goods_id' => $goods_id
  116. );
  117. $cart = Cart::where($where)->first();
  118. if($cart)
  119. {
  120. //更新购物车
  121. $updateArr = array(
  122. 'goods_number' => $goods_number,
  123. 'add_time' => time(),
  124. );
  125. self::where(array('id'=>$cart->id))->update($updateArr);
  126. $cart_id = $cart->id;
  127. }
  128. else
  129. {
  130. //添加购物车
  131. $cartInsert = array(
  132. 'user_id' => $user_id,
  133. 'goods_id' => $goods_id,
  134. 'goods_number' => $goods_number,
  135. 'add_time' => time(),
  136. );
  137. $cart_id = self::insertGetId($cartInsert);
  138. }
  139. return ReturnData::create(ReturnData::SUCCESS,$cart_id,'购物车添加成功');
  140. }
  141. /**
  142. * 清空购物车
  143. *
  144. * @param int $type 类型:默认普通商品
  145. */
  146. public static function clearCart($user_id)
  147. {
  148. self::where('user_id',$user_id)->delete();
  149. return true;
  150. }
  151. //购物车商品总数量
  152. public static function TotalGoodsCount($user_id)
  153. {
  154. return self::where('user_id',$user_id)->sum('goods_number');
  155. }
  156. //购物车结算商品列表
  157. public static function cartCheckoutGoodsList(array $param)
  158. {
  159. extract($param);
  160. $cartIds = explode("_",$ids);
  161. // 获取购物车列表
  162. $cartList = self::where(array('user_id'=>$user_id))->whereIn('id', $cartIds)->get();
  163. $total_price = 0;//总金额
  164. if(!empty($cartList))
  165. {
  166. $resultList = array();
  167. $checkArr = array();
  168. foreach($cartList as $k=>$v)
  169. {
  170. $goods = Goods::where(array('id'=>$v['goods_id']))->first();
  171. $cartList[$k]->is_promote = 0;
  172. if(Goods::bargain_price($goods->price,$goods->promote_start_date,$goods->promote_end_date) > 0){$cartList[$k]->is_promote = 1;}
  173. $cartList[$k]->final_price = Goods::get_final_price($v['goods_id']); //商品最终价格
  174. $cartList[$k]->goods_detail_url = route('weixin_goods_detail',array('id'=>$v['goods_id']));
  175. $cartList[$k]->title = $goods->title;
  176. $cartList[$k]->litpic = $goods->litpic;
  177. $total_price += $cartList[$k]->final_price*$tempInfo['price'];
  178. }
  179. }
  180. $res['list'] = $cartList;
  181. $res['list'] = $cartList;
  182. return ReturnData::create(ReturnData::SUCCESS,$res);
  183. }
  184. }