123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <?php
- /**
- * 我的购物车
- *
- *
- *
- *
-
- */
- //use Shopnc\Tpl;
- defined('InShopNC') or exit('Access Invalid!');
- class member_cartControl extends mobileMemberControl
- {
- public function __construct() {
- parent::__construct();
- }
- /**
- * 购物车列表
- */
- public function cart_listOp() {
- $model_cart = Model('cart');
- $model_goods = Model('goods');
- $condition = array('buyer_id' => $this->member_info['member_id']);
- $cart_list = $model_cart->listCart('db', $condition);
- $sum = 0;
- foreach ($cart_list as $key => $value)
- {
- $goods_id = $value['goods_id'];
- $goods_promotion = $model_goods->getGoodsInfoAndPromotionById($goods_id);
- $goos_storage = $model_goods->getGoodsStorageById($goods_id);
- $promotion_type = '';
- $promotion_price = '0.00';
- if(!empty( $goods_promotion['groupbuy_info'])){
- $promotion_type = '抢购';
- $promotion_price = $goods_promotion['groupbuy_info']['groupbuy_price'];
- }
- if(!empty( $goods_promotion['xianshi_info'])){
- $promotion_type = '限时折扣';
- $promotion_price = $goods_promotion['xianshi_info']['xianshi_price'];
- }
- $goods_spec = unserialize($goods_promotion['goods_spec']);
- $goods_spec_array = array();
- foreach ($goods_spec as $val) {
- array_push($goods_spec_array, $val);
- }
- $cart_list[$key]['goos_storage'] = $goos_storage;
- $cart_list[$key]['goods_spec'] = implode(',', $goods_spec_array);
- $cart_list[$key]['promotion_type'] = $promotion_type;
- $cart_list[$key]['promotion_price'] = $promotion_price;
- $cart_list[$key]['goods_image_url'] = cthumb($value['goods_image'], $value['store_id']);
- $cart_list[$key]['goods_sum'] = ncPriceFormat($value['goods_price'] * $value['goods_num']);
- $sum += $cart_list[$key]['goods_sum'];
- }
- joutput_data(array('cart_list' => $cart_list, 'sum' => ncPriceFormat($sum)));
- }
- /**
- * 购物车添加
- */
- public function cart_addOp()
- {
- $goods_id = intval($_POST['goods_id']);
- $quantity = intval($_POST['quantity']);
- if($goods_id <= 0 || $quantity <= 0) {
- joutput_error(errcode::ErrCart, '参数错误');
- }
- $model_goods = Model('goods');
- $model_cart = Model('cart');
- $logic_buy_1 = Logic('buy_1');
- $goods_info = $model_goods->getGoodsOnlineInfoAndPromotionById($goods_id);
- //验证是否可以购买
- if(empty($goods_info)) {
- joutput_error(errcode::ErrCart, '商品已下架或不存在');
- }
- //抢购
- $logic_buy_1->getGroupbuyInfo($goods_info);
-
- //限时折扣
- $logic_buy_1->getXianshiInfo($goods_info,$quantity);
- if ($goods_info['store_id'] == $this->member_info['store_id']) {
- joutput_error(errcode::ErrCart, '不能购买自己发布的商品');
- }
- if(intval($goods_info['goods_storage']) < 1 || intval($goods_info['goods_storage']) < $quantity) {
- joutput_error(errcode::ErrCart, '库存不足');
- }
- $param = array();
- $param['buyer_id'] = $this->member_info['member_id'];
- $param['store_id'] = $goods_info['store_id'];
- $param['goods_id'] = $goods_info['goods_id'];
- $param['goods_name'] = $goods_info['goods_name'];
- $param['goods_price'] = $goods_info['goods_price'];
- $param['goods_image'] = $goods_info['goods_image'];
- $param['store_name'] = $goods_info['store_name'];
- $result = $model_cart->addCart($param, 'db', $quantity);
- if($result) {
- joutput_data(array('result' => '1'));
- } else {
- joutput_error(errcode::ErrCart, '收藏失败');
- }
- }
- public function cart_editOp()
- {
- $cart_list = explode(',', urldecode($_POST['cart_list']));
- $new_cart_list= array();
- if (is_array($cart_list))
- {
- foreach ($cart_list as $value)
- {
- if (preg_match_all('/^(\d{1,10})\|(\d{1,6})$/', $value, $match)) {
- if (intval($match[2][0]) > 0) {
- $new_cart_list[$match[1][0]] = $match[2][0];
- }
- }
- }
- $model_cart = Model('cart');
- $condition = array('buyer_id' => $this->member_info['member_id']);
- $original_cart_list = $model_cart->listCart('db', $condition);
- $quantity_error = array();
- foreach ($original_cart_list as $key => $value)
- {
- if(array_key_exists($value['cart_id'], $new_cart_list))
- {
- $quantity = intval($new_cart_list[$value['cart_id']]);
- if(intval($value['goods_num']) !== $quantity)
- {
- if(!$this->_check_goods_storage($value, $quantity, $this->member_info['member_id'])) {
- array_push($quantity_error, array('cart_id' => $value['cart_id'], 'quantity' => $quantity));
- }
- else
- {
- $data = array();
- $data['goods_num'] = $quantity;
- $update = $model_cart->editCart($data, array('cart_id'=> $value['cart_id']));
- if(!$update) {
- joutput_error(errcode::ErrCart, '购物车修改失败');
- return;
- }
- }
- }
- }
- else
- {
- $delete = $model_cart->delCart('db', array('cart_id'=>$value['cart_id']));
- if(!$delete) {
- joutput_error(errcode::ErrCart, '购物车修改失败');
- return;
- }
- }
- }
- if(count($quantity_error) > 0) {
- joutput_data(array('result'=> '0', 'quantity_error' => $quantity_error));
- } else {
- joutput_data(array('result' => '1'));
- }
- }else{
- joutput_error(errcode::ErrCart, '购物车列表格式错误');
- }
- }
- /**
- * 购物车删除
- */
- public function cart_delOp()
- {
- $cart_id = intval($_POST['cart_id']);
- $model_cart = Model('cart');
- if($cart_id > 0) {
- $condition = array();
- $condition['buyer_id'] = $this->member_info['member_id'];
- $condition['cart_id'] = $cart_id;
- $model_cart->delCart('db', $condition);
- }
- joutput_data(array('result' => '1'));
- }
- /**
- * 更新购物车购买数量
- */
- public function cart_edit_quantityOp()
- {
- $cart_id = intval(abs($_POST['cart_id']));
- $quantity = intval(abs($_POST['quantity']));
- if(empty($cart_id) || empty($quantity)) {
- joutput_error(errcode::ErrCart, '参数错误');
- }
- $model_cart = Model('cart');
- $cart_info = $model_cart->getCartInfo(array('cart_id'=>$cart_id, 'buyer_id' => $this->member_info['member_id']));
- //检查是否为本人购物车
- if($cart_info['buyer_id'] != $this->member_info['member_id']) {
- joutput_error(errcode::ErrCart, '参数错误');
- }
- //检查库存是否充足
- if(!$this->_check_goods_storage($cart_info, $quantity, $this->member_info['member_id'])) {
- joutput_error(errcode::ErrCart, '库存不足');
- }
- $data = array();
- $data['goods_num'] = $quantity;
- $update = $model_cart->editCart($data, array('cart_id'=>$cart_id));
- if ($update) {
- $return = array();
- $return['quantity'] = $quantity;
- $return['goods_price'] = ncPriceFormat($cart_info['goods_price']);
- $return['total_price'] = ncPriceFormat($cart_info['goods_price'] * $quantity);
- joutput_data($return);
- } else {
- joutput_error(errcode::ErrCart, '修改失败');
- }
- }
- /**
- * 检查库存是否充足
- */
- private function _check_goods_storage($cart_info, $quantity, $member_id)
- {
- $model_goods= Model('goods');
- $model_bl = Model('p_bundling');
- $logic_buy_1 = Logic('buy_1');
- if ($cart_info['bl_id'] == '0') {
- //普通商品
- $goods_info = $model_goods->getGoodsOnlineInfoAndPromotionById($cart_info['goods_id']);
- //抢购
- $logic_buy_1->getGroupbuyInfo($goods_info);
- //限时折扣
- $logic_buy_1->getXianshiInfo($goods_info,$quantity);
- if(intval($goods_info['goods_storage']) < $quantity) {
- return false;
- }
- } else {
- //优惠套装商品
- $bl_goods_list = $model_bl->getBundlingGoodsList(array('bl_id' => $cart_info['bl_id']));
- $goods_id_array = array();
- foreach ($bl_goods_list as $goods) {
- $goods_id_array[] = $goods['goods_id'];
- }
- $bl_goods_list = $model_goods->getGoodsOnlineListAndPromotionByIdArray($goods_id_array);
- //如果有商品库存不足,更新购买数量到目前最大库存
- foreach ($bl_goods_list as $goods_info) {
- if (intval($goods_info['goods_storage']) < $quantity) {
- return false;
- }
- }
- }
- return true;
- }
- }
|