/** * 从购物车中删除某个商品 */ public function Remove($f3) { // 首先做参数合法性验证 $validator = new Validator($f3->get('GET')); $goods_id = $validator->required('商品ID不能为空')->digits('商品ID非法')->min(1, true, '商品ID非法')->validate('goods_id'); $specListStr = $validator->validate('specListStr'); if (!$this->validate($validator)) { goto out; } $cartBasicService = new CartBasicService(); $cartBasicService->loadFromStorage(); // 加载购物车的数据 $cartContext =& $cartBasicService->getCartContextRef(); $cartContext->removeGoods($goods_id, $specListStr); $cartBasicService->syncStorage(); // 重新保存数据 out: // ajax 操作 if ($f3->get('GET[isAjax]')) { Ajax::header(); if ($validator->hasErrors()) { $errorMessage = implode('|', $this->flashMessageArray); echo Ajax::buildResult(-1, $errorMessage, null); return; } echo Ajax::buildResult(null, null, null); return; } // 网页操作 RouteHelper::reRoute($this, '/Cart/Show'); }
public function post($f3) { // 这里操作加入购物车 // 首先做参数合法性验证 $validator = new Validator($f3->get('POST')); $goods_id = $validator->required('商品id不能为空')->digits('商品id非法')->min(1, true, '商品id非法')->validate('goods_id'); $goodsChooseSpecListStr = $validator->validate('goods_choose_speclist'); $goodsChooseBuyCount = $validator->required('商品最少购买 1 件')->digits('商品最少购买 1 件')->min(1, true, '商品最少购买 1 件')->validate('goods_choose_buycount'); if (!$this->validate($validator)) { goto out_fail; } // 查询商品信息 $goodsBasicService = new GoodsBasicService(); $goodsInfo = $goodsBasicService->loadGoodsById($goods_id); // 商品不存在,不允许显示等 if ($goodsInfo->isEmpty() || !$goodsInfo->is_on_sale || !Utils::isTagExist(PluginHelper::SYSTEM_MOBILE, $goodsInfo['system_tag_list'])) { $this->addFlashMessage('商品不存在或者不能购买'); goto out_fail; } $cartBasicService = new CartBasicService(); // 购物车服务 $cartBasicService->loadFromStorage(); // 恢复购物车中已有的数据 $cartContext =& $cartBasicService->getCartContextRef(); // 取得 cartContext // 商品多组合选择 if (!empty($goodsChooseSpecListStr)) { // 检查商品 Spec 是否合法 $goodsSpecService = new GoodsSpecService(); $goodsSpecService->initWithJson($goodsInfo['goods_spec']); $goodsSpecDataArray = $goodsSpecService->getGoodsSpecDataArray($goodsChooseSpecListStr); if (!$goodsSpecDataArray) { $this->addFlashMessage('商品选择[' . $goodsChooseSpecListStr . ']非法'); goto out_fail; } // 检查商品库存 if ($goodsChooseBuyCount > @$goodsSpecDataArray['goods_number']) { $this->addFlashMessage($goodsChooseSpecListStr . '库存不足,只剩 ' . @$goodsSpecDataArray['goods_number'] . ' 件'); goto out_fail; } // 商品加入到购物车 $cartContext->addGoods($goods_id, $goodsChooseBuyCount, $goodsChooseSpecListStr, @$goodsSpecDataArray['goods_sn']); } else { // 检查库存 if ($goodsChooseBuyCount > $goodsInfo['goods_number']) { $this->addFlashMessage('库存不足,剩余 ' . $goodsInfo['goods_number'] . ' 件'); goto out_fail; } // 普通简单选择,加入购物车 $cartContext->addGoods($goods_id, $goodsChooseBuyCount); } $cartBasicService->syncStorage(); // 保存购物车的数据 out: Ajax::header(); echo Ajax::buildResult(null, null, null); return; out_fail: // 失败,返回出错信息 $errorMessage = implode('|', $this->flashMessageArray); Ajax::header(); echo Ajax::buildResult(-1, $errorMessage, null); }
/** * 把订单加载到购物车 */ public function Cart($f3) { // 参数验证 $validator = new Validator($f3->get('GET')); $order_id = $validator->required('订单ID非法')->digits('订单ID非法')->min(1, true, '订单ID非法')->validate('order_id'); if (!$this->validate($validator)) { goto out_fail; } $userInfo = AuthHelper::getAuthUser(); // 查询订单 $orderBasicService = new OrderBasicService(); $orderInfo = $orderBasicService->loadOrderInfoById($order_id, 10); // 缓存 10 秒钟 if ($orderInfo->isEmpty() || $userInfo['user_id'] != $orderInfo['user_id'] || OrderBasicService::OS_UNCONFIRMED != $orderInfo['order_status'] || !$this->verifyOrderSystem($orderInfo)) { $this->addFlashMessage('订单ID非法'); goto out_fail; } //加载订单 $cartBasicService = new CartBasicService(); // 加载订单到购物车里 if (!$cartBasicService->loadFromOrderInfo($order_id)) { $this->addFlashMessage('订单加载失败'); goto out_fail; } $cartContext =& $cartBasicService->getCartContextRef(); if ($cartContext->isEmpty()) { $this->addFlashMessage('订单为空,不能支付'); goto out_fail; } // 保存购物车 $cartBasicService->syncStorage(); RouteHelper::reRoute($this, '/Cart/Show'); return; // 成功从这里返回 out_fail: // 从这里退出 RouteHelper::reRoute($this, '/My/Order'); }