Parcourir la source

modify cart module

stanley-king il y a 9 ans
Parent
commit
152d7697b8

+ 0 - 3
data/logic/buy_1.logic.php

@@ -17,13 +17,10 @@ class buy_1Logic {
 
         //优惠套装
         $this->_getBundlingCartList($cart_list);
-
         //抢购
         $this->getGroupbuyCartList($cart_list);
-
         //限时折扣
         $this->getXianshiCartList($cart_list);
-
         //赠品
         $this->_getGiftCartList($cart_list);
 

+ 30 - 4
data/model/cart.model.php

@@ -69,7 +69,22 @@ class cartModel extends Model
      */
     public function getCartInfo($condition = array(), $field = '*')
     {
-        return $this->table('cart')->field($field)->where($condition)->find();
+        if($_SESSION['is_login'] == 1) {
+            return $this->table('cart')->field($field)->where($condition)->find();
+        }
+        else if(is)
+        {
+            $carts = $_SESSION['carts'];
+            if(!is_array($carts)) {
+                return array();
+            }
+            if(empty($condition)) {
+                return $carts;
+            } else {
+                $cart_id = $condition['cart_id'];
+                return $carts[$cart_id]; // session 中的购物车没有buyer_id
+            }
+        }
     }
 
     /**
@@ -235,10 +250,19 @@ class cartModel extends Model
      */
     public function editCart($data, $condition)
     {
-        $result = $this->table('cart')->where($condition)->update($data);
-        if ($result) {
-            $this->getCartNum('db', array('buyer_id' => $condition['buyer_id']));
+
+        if(!is_mobile()) 
+        {
+            $result = $this->table('cart')->where($condition)->update($data);
+            if ($result) {
+                $this->getCartNum('db', array('buyer_id' => $condition['buyer_id']));
+            }
         }
+
+                    if($_SESSION['is_login'] == 1) 
+        {
+        } else if (is_)
+
         return $result;
     }
 
@@ -339,6 +363,8 @@ class cartModel extends Model
             setNcCookie('cart', '', -3600);
         } else if ($type == 'db') {
             //数据库暂无浅清空操作
+        } else if ($type == 'Session') {
+            $_SESSION['carts'] = array();
         }
     }
 

+ 0 - 1
helper/session.php

@@ -64,7 +64,6 @@ class session
 
     public function destroy() {
         $this->fdestroy = true;
-
         //session_destroy();//会触发destroy 和 close 函数
     }
 

+ 182 - 11
mobile/control/cart.php

@@ -33,6 +33,9 @@ class cartControl extends mobileControl
 
         //购物车列表 [得到最新商品属性及促销信息]
         $cart_list = $logic_buy_1->getGoodsCartList($cart_list);
+        $ret = array_chunk($cart_list,$this->page_size());
+        $pages = count($ret);
+        $cart_list = $ret[$this->page_no() - 1];
 
         //购物车商品以店铺ID分组显示,并计算商品小计,店铺小计与总价由JS计算得出
         $store_cart_list = array();
@@ -51,18 +54,82 @@ class cartControl extends mobileControl
 
         if(in_array($_SESSION['client_type'],array('android', 'ios','ajax'))) {
             $data = $this->format_cart($store_cart_list,$store_list,$mansong_rule_list,$free_freight_list);
-            self::outsuccess($data);
+            self::outsuccess(array('carts' => $data,'mobile_page' => $pages));
         } else {
             $data = $this->format_cart($store_cart_list,$store_list,$mansong_rule_list,$free_freight_list);
-            self::outsuccess($data);
-            //self::outsuccess($store_cart_list,$store_list,$mansong_rule_list,$free_freight_list);
+            self::outsuccess(array('carts' => $data,'mobile_page' => $pages));
+        }
+    }
+
+    private function remove_goods(&$goods_info)
+    {
+        static $ex_goodsfield = array("buyer_id","store_id","store_name","gc_id","transport_id","goods_vat","goods_storage","goods_storage_alarm",
+            "is_fcode","promotions_id",);
+        static $ex_xsfield = array("xianshi_goods_id","xianshi_id","goods_id","store_id","goods_name","goods_price","goods_image","goods_url",
+            "image_url","xianshi_recommend");
+        static $ex_gbuyfield = array("groupbuy_id","goods_id","goods_commonid","goods_name","store_id","store_name","goods_price","recommended","views",
+               "class_id","s_class_id","is_vr","vr_city_id","vr_area_id","vr_mall_id","vr_class_id","vr_s_class_id","groupbuy_url","goods_url","start_time_text",
+               "end_time_text","groupbuy_state_text","reviewable","cancelable","state_flag","button_text","count_down_text","count_down");
+
+
+        if(is_array($goods_info))
+        {
+            $store_id = $goods_info['store_id'];
+            foreach($ex_goodsfield as $key)
+            {
+                if(array_key_exists($key,$goods_info)) {
+                    unset($goods_info[$key]);
+                }
+            }
+
+            if(array_key_exists('xianshi_info',$goods_info))
+            {
+                $xs_info = &$goods_info['xianshi_info'];
+                if(is_array($xs_info))
+                {
+                    foreach($ex_xsfield as $key)
+                    {
+                        if(array_key_exists($key,$xs_info)) {
+                            unset($xs_info[$key]);
+                        }
+                    }
+                }
+            }
+
+            if(array_key_exists('groupbuy_info',$goods_info))
+            {
+                $gbuy_info = &$goods_info['groupbuy_info'];
+                if(is_array($gbuy_info))
+                {
+                    foreach($ex_gbuyfield as $key)
+                    {
+                        if(array_key_exists($key,$gbuy_info)) {
+                            unset($gbuy_info[$key]);
+                        }
+                    }
+                    if(array_key_exists("state",$gbuy_info))
+                    {
+                        $state = intval($gbuy_info["state"]);
+                        if($state == 20) {
+                            $gbuy_info["state"] = true;
+                        }
+                    }
+                    if(array_key_exists("groupbuy_image",$gbuy_info)) {
+                        $gbuy_info['groupbuy_image'] = cthumb($gbuy_info['groupbuy_image'], $store_id);
+                    }
+                    if(array_key_exists("groupbuy_image1",$gbuy_info)) {
+                        $gbuy_info['groupbuy_image1'] = cthumb($gbuy_info['groupbuy_image1'], $store_id);
+                    }
+                }
+            }
         }
     }
 
     private function format_cart($store_cart_list,$store_list,$mansong_rule_list,$free_freight_list)
     {
         $ret = array();
-        foreach($store_cart_list as $key => $val) {
+        foreach($store_cart_list as $key => $val)
+        {
             $store_goods = array();
             $store_goods['store_info'] = $store_list[$key];
             $store_goods['free_freight'] = $free_freight_list[$key];
@@ -70,6 +137,7 @@ class cartControl extends mobileControl
 
             $store_goods['goods_info'] = array();
             foreach($val as $index => $goods) {
+                $this->remove_goods($goods);
                 array_push($store_goods['goods_info'],$goods);
             }
 
@@ -254,11 +322,6 @@ class cartControl extends mobileControl
 
     public function cart_editOp()
     {
-        $token = trim($_GET['key']);
-        if (false == $this->checkToken($token)) {
-            return joutput_error($this->err_code);
-        }
-
         $cart_list = explode(',', urldecode($_POST['cart_list']));
         $new_cart_list = array();
         if (is_array($cart_list)) {
@@ -308,6 +371,116 @@ class cartControl extends mobileControl
     }
 
     /**
+     * 购物车更新商品数量
+     */
+
+    public function editOp()
+    {
+        $cart_list = explode(',', urldecode($_POST['cart_list']));
+        if (!is_array($cart_list)) {
+            return self::outerr(errcode::ErrCart, '修改购物车列表参数格式错误');
+        }
+
+        $new_cart_list = array();
+        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];
+                }
+            }
+        }
+
+        foreach($new_cart_list as $cart_id => $quantity)
+        {
+            if($quantity == 0) {
+                $this->do_edit(intval(abs($cart_id)),intval(abs($quantity)));
+            } else {
+
+            }
+        }
+    }
+
+    public function do_edit($cart_id,$quantity)
+    {
+        $model_cart = Model('cart');
+        $model_goods= Model('goods');
+        $logic_buy_1 = logic('buy_1');
+
+        //存放返回信息
+        $return = array();
+        $cart_info = $model_cart->getCartInfo(array('cart_id'=>$cart_id,'buyer_id'=>$_SESSION['member_id']));
+        if ($cart_info['bl_id'] == '0')
+        {
+            //普通商品
+            $goods_id = intval($cart_info['goods_id']);
+            $goods_info	= $logic_buy_1->getGoodsOnlineInfo($goods_id,$quantity);
+            if(empty($goods_info))
+            {
+                if($_SESSION['is_login'] == 1) {
+                    QueueClient::push('delCart', array('buyer_id'=>$_SESSION['member_id'],'cart_ids'=>array($cart_id)));
+                }
+                return array('cart_id' => $cart_id, 'quantity' => $quantity, 'err_info' => '商品已被下架');
+            }
+
+            //抢购
+            $logic_buy_1->getGroupbuyInfo($goods_info);
+            //限时折扣
+            $logic_buy_1->getXianshiInfo($goods_info,$quantity);
+            $quantity = $goods_info['goods_num'];
+
+            if(intval($goods_info['goods_storage']) < $quantity) {
+                $model_cart->editCart(array('goods_num'=>$goods_info['goods_storage']),array('cart_id'=>$cart_id,'buyer_id'=>$_SESSION['member_id']));
+                return array('cart_id' => $cart_id, 'quantity' => $goods_info['goods_storage'], 'err_info' => '库存不足,已将您购物车更新为库存数量.');
+            }
+        }
+        else
+        {
+            //优惠套装商品
+            $model_bl = Model('p_bundling');
+            $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'];
+            }
+            $goods_list = $model_goods->getGoodsOnlineListAndPromotionByIdArray($goods_id_array);
+
+            //如果其中有商品下架,删除
+            if (count($goods_list) != count($goods_id_array)) {
+                QueueClient::push('delCart', array('buyer_id'=>$_SESSION['member_id'],'cart_ids'=>array($cart_id)));
+                return array('cart_id' => $cart_id, 'quantity' => 0, 'err_info' => '该优惠套装已经无效,建议您购买单个商品.');
+            }
+
+            //如果有商品库存不足,更新购买数量到目前最大库存
+            foreach ($goods_list as $goods_info)
+            {
+                if ($quantity > $goods_info['goods_storage']) {
+                    $model_cart->editCart(array('goods_num'=>$goods_info['goods_storage']),array('cart_id'=>$cart_id,'buyer_id'=>$_SESSION['member_id']));
+                    return array('cart_id' => $cart_id, 'quantity' => $goods_info['goods_storage'], 'err_info' => '该优惠套装部分商品库存不足,建议您降低购买数量或购买库存足够的单个商品.');
+                    break;
+                }
+            }
+            $goods_info['goods_price'] = $cart_info['goods_price'];
+        }
+
+        $data = array();
+        $data['goods_num'] = $quantity;
+        $data['goods_price'] = $goods_info['goods_price'];
+
+        if($_SESSION['is_login'] == 1) {
+            $update = $model_cart->editCart($data,array('cart_id'=>$cart_id,'buyer_id'=>$_SESSION['member_id']));
+        } else {
+            $update = $model_cart->editCart($data,array('cart_id'=>$cart_id));
+        }
+
+        if ($update) {
+            return true;
+        } else {
+            return array('cart_id' => $cart_id, 'quantity' => 0, 'err_info' => '更新购物车失败.');
+        }
+    }
+
+    /**
      * 购物车删除
      */
     public function cart_delOp()
@@ -318,9 +491,7 @@ class cartControl extends mobileControl
         }
 
         $cart_id = intval($_POST['cart_id']);
-
         $model_cart = Model('cart');
-
         if ($cart_id > 0) {
             $condition = array();
             $condition['buyer_id'] = $this->member_info['member_id'];

+ 16 - 5
mobile/control/control.php

@@ -16,27 +16,38 @@ class mobileControl
     protected $client_type_array = array('android', 'wap', 'wechat', 'ios','ajax','web');
     
     //列表默认分页数
+    //protected $page_size = 5;
     protected $page = 5;
+    protected $cur_page = 1;
 
     //任务开始时间
     private static $startime = 0;
 
     public function __construct()
     {
-        self::$startime = microtime(true);
+        self::$startime = microtime();
 
         Language::read('mobile');
         //分页数处理
-        $page = intval($_GET['page']);
-        if ($page > 0) {
-            $this->page = $page;
+        if (is_numeric($_GET['page']) && intval(trim($_GET['page'])) > 0) {
+            $this->page = intval(trim($_GET['page']));
+        }
+        if (is_numeric($_GET['curpage']) && intval(trim($_GET['curpage'])) > 0) {
+            $this->cur_page = intval(trim($_GET['curpage']));
         }
 
         $this->check_app_type();
     }
 
+    protected function page_size() {
+        return $this->page;
+    }
+    protected function page_no() {
+        return $this->cur_page;
+    }
+
     private static function eclipse_time() {
-        return (microtime(true) - self::$startime);
+        return (microtime() - self::$startime);
     }
 
     protected function check_app_type()

+ 0 - 1
mobile/control/member_buy.php

@@ -14,7 +14,6 @@ defined('InShopNC') or exit('Access Invalid!');
 
 class member_buyControl extends mbMemberControl
 {
-
     public function __construct()
     {
         parent::__construct();

+ 10 - 0
run.php

@@ -19,6 +19,16 @@ function help_out()
 
 }
 
+
+$time = -microtime();
+//$hash = 0;
+//for ($i=0; $i < rand(1000,4000); ++$i) {
+//    $hash ^= md5(substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, rand(1,10)));
+//}
+$time += microtime();
+$y = sprintf('%f', $time);
+
+
 $y = strtotime('2016-12-12 01:01:01');
 
 $y = urldecode('%E4%B8%AD%E9%80%9A%E5%BF%AB%E9%80%92');