Przeglądaj źródła

如果购物车中的商品被删除,将导致错误

stanley-king 8 lat temu
rodzic
commit
2d418b9e7f
2 zmienionych plików z 53 dodań i 0 usunięć
  1. 21 0
      helper/algorithm.php
  2. 32 0
      mobile/control/cart.php

+ 21 - 0
helper/algorithm.php

@@ -170,4 +170,25 @@ class algorithm
 
         return self::copy($ar2,$pos2,$result);
     }
+
+    public static function not_in($src,$avables)
+    {
+        if(empty($src)) return array();
+        if(empty($avables)) return $src;
+
+        $src = array_unique($src);
+        $avables = array_unique($avables);
+        sort($src);
+        sort($avables);
+
+        $result = [];
+        foreach ($src as $val)
+        {
+            if(algorithm::binary_search($avables,$val) == false) {
+                $result[] = $val;
+            }
+        }
+
+        return $result;
+    }
 }

+ 32 - 0
mobile/control/cart.php

@@ -7,6 +7,8 @@ defined('InShopNC') or exit('Access Invalid!');
 
 require_once (BASE_ROOT_PATH . '/helper/goods_helper.php');
 require_once (BASE_ROOT_PATH . '/helper/activity_helper.php');
+require_once(BASE_ROOT_PATH . '/helper/algorithm.php');
+
 
 class cart_item
 {
@@ -105,6 +107,15 @@ class cartControl extends mobileControl
             $summary_list = array_merge($summary_list,$related_summary['summary']);
         }
 
+        //有可能购物车中的商品被删除....
+        $avables_goods = [];
+        foreach ($summary_list as $summay) {
+            $avables_goods[] = $summay['goods_id'];
+        }
+
+        $err_goods = algorithm::not_in($goods_ids,$avables_goods);
+        $cart_infos = $this->remove_err($cart_infos,$err_goods);
+
         if($minest_cartid == 0) {
             $ret['free_info'] = $this->full_send();
         }
@@ -120,6 +131,27 @@ class cartControl extends mobileControl
         return self::outsuccess($ret);
     }
 
+    private function remove_err($cart_infos,$err_goods)
+    {
+        if(empty($err_goods)) return $cart_infos;
+
+        $result = [];
+        foreach ($cart_infos as $cart)
+        {
+            if($cart['bl_id'] > 0) {
+                $result[] = $cart;
+            }
+            else
+            {
+                if(algorithm::binary_search($cart['goods_id'],$err_goods) == false) {
+                    $result[] = $cart;
+                }
+            }
+        }
+
+        return $result;
+    }
+
     private function full_send()
     {
         $result = [];