Authored by hf

code review by fei.hong: do modify cache config to aliyun vpc

  1 +<?php
  2 +
  3 +use Action\WebAction;
  4 +use WebPlugin\Helpers;
  5 +use Shopping\CartModel;
  6 +use Product\ItemModel;
  7 +
  8 +/**
  9 + * 购物车相关的控制器
  10 + *
  11 + * @name IndexController
  12 + * @package Cart
  13 + * @copyright yoho.inc
  14 + * @version 1.0 (2016-2-17 15:43:19)
  15 + * @author fei.hong <fei.hong@yoho.cn>
  16 + */
  17 +class IndexController extends WebAction
  18 +{
  19 +
  20 + /**
  21 + * 通过当前用户审判是否跳到登录
  22 + *
  23 + * @param boolean $useSession (true:从服务端session中检查, false:从客户端cookie中检查)
  24 + * @pram string $refer 指定的来源地址
  25 + * @return void
  26 + */
  27 + protected function auditJumpLogin($useSession = true, $refer = '')
  28 + {
  29 + $uid = $this->getUid($useSession);
  30 + if (!$uid) {
  31 + if (empty($refer)) {
  32 + $refer = $this->server('HTTP_REFERER', SITE_MAIN);
  33 + }
  34 + $this->go(Helpers::url('/signin.html', array('refer' => $refer)));
  35 + }
  36 + }
  37 +
  38 + /**
  39 + * 我的购物车
  40 + */
  41 + public function indexAction()
  42 + {
  43 + $uid = $this->getUid(true);
  44 + $shoppingKey = Helpers::getShoppingKeyByCookie();
  45 +
  46 + // 显示一次并清除已删除的COOKIE记录
  47 + $cartDelList = $this->getCookie('cart-del-list');
  48 + if (!empty($cartDelList)) {
  49 + $this->setCookie('cart-del-list', '');
  50 + }
  51 +
  52 + $this->setTitle('购物车', true, ' | ');
  53 + $this->setSimpleHeader();
  54 + $this->_view->display('cart', array(
  55 + 'cartEnsurePage' => true,
  56 + 'cartEnsure' => CartModel::myCartData($uid, $shoppingKey, $cartDelList),
  57 + ));
  58 + }
  59 +
  60 + /**
  61 + * 购物车商品选择与取消
  62 + *
  63 + * @param string skuList 商品sku列表,json格式,如{"744403":1,"777777":3}
  64 + * @return json
  65 + */
  66 + public function selectAction()
  67 + {
  68 + $result = array();
  69 +
  70 + if ($this->isAjax()) {
  71 + $productId = $this->post('skuList', 0);
  72 + $uid = $this->getUid(true);
  73 + $shoppingKey = Helpers::getShoppingKeyByCookie();
  74 + $result = CartModel::selectGoods($uid, $productId, $shoppingKey);
  75 + }
  76 +
  77 + $this->echoJson($result);
  78 + }
  79 +
  80 + /**
  81 + * 修改购物车商品数量
  82 + *
  83 + * @param int sku
  84 + * @param int increaseNum
  85 + * @param int decreaseNum
  86 + * @return json
  87 + */
  88 + public function modifyAction()
  89 + {
  90 + $result = array();
  91 +
  92 + if ($this->isAjax()) {
  93 + $shoppingKey = Helpers::getShoppingKeyByCookie();
  94 +
  95 + $uid = $this->getUid(true);
  96 + $sku = $this->post('sku', 0);
  97 + $increaseNum = $this->post('increaseNum', null);
  98 + $decreaseNum = $this->post('decreaseNum', null);
  99 +
  100 + $result = CartModel::modifyProductNum($uid, $sku, $increaseNum, $decreaseNum, $shoppingKey);
  101 + if (!empty($result['code']) && $result['code'] == 200) {
  102 + $this->setShoppingCookie($uid);
  103 + }
  104 + }
  105 +
  106 + $this->echoJson($result);
  107 + }
  108 +
  109 + /**
  110 + * 移出购物车
  111 + *
  112 + * @param string skuList 商品sku列表,json格式,如{"744403":1,"777777":3}
  113 + * @return json
  114 + */
  115 + public function removeAction()
  116 + {
  117 + $result = array();
  118 +
  119 + if ($this->isAjax()) {
  120 + $skuList = $this->post('skuList', 0);
  121 + $uid = $this->getUid(true);
  122 + $shoppingKey = Helpers::getShoppingKeyByCookie();
  123 + $result = CartModel::removeFromCart($uid, $skuList, $shoppingKey);
  124 + if (!empty($result['code']) && $result['code'] == 200) {
  125 + $this->setShoppingCookie($uid);
  126 + }
  127 + }
  128 +
  129 + $this->echoJson($result);
  130 + }
  131 +
  132 + /**
  133 + * 移入收藏夹
  134 + *
  135 + * 支持批量移入收藏夹
  136 + *
  137 + * @param string 商品sku列表,json格式,如{"744403":1,"777777":3}
  138 + * @return json
  139 + */
  140 + public function favAction()
  141 + {
  142 + $result = array();
  143 +
  144 + if ($this->isAjax()) {
  145 + $skuList = $this->post('skuList');
  146 + $uid = $this->getUid(true);
  147 + $result = CartModel::addToFav($uid, $skuList);
  148 + if (!empty($result['code']) && $result['code'] == 200) {
  149 + $this->setShoppingCookie($uid);
  150 + }
  151 + }
  152 +
  153 + $this->echoJson($result);
  154 + }
  155 +
  156 + /**
  157 + * 检查是否收藏
  158 + *
  159 + * @param string sknList 商品productId列表,如["123123","123412"]
  160 + */
  161 + public function checkFavAction()
  162 + {
  163 + $result = array('code' => 200, 'message' => '是否收藏', 'data' => array());
  164 +
  165 + if ($this->isAjax()) {
  166 + $uid = $this->getUid(false);
  167 + $pidList = $this->post('pidList', '');
  168 + $result['data'] = CartModel::checkUserIsFav($uid, $pidList);
  169 + }
  170 +
  171 + $this->echoJson($result);
  172 + }
  173 +
  174 + /**
  175 + * 凑单商品异步请求
  176 + */
  177 + public function getTogetherProductAction()
  178 + {
  179 + $result = array('code' => 200, 'data' => array(), 'message' => '凑单商品');
  180 +
  181 + if ($this->isAjax()) {
  182 + $page = $this->get('page', 1);
  183 + $result = CartModel::getTogetherProduct($page);
  184 + }
  185 +
  186 + $this->echoJson($result);
  187 + }
  188 +
  189 + /**
  190 + * 最近浏览的商品异步请求
  191 + */
  192 + public function getHistroyProductAction()
  193 + {
  194 + $result = array('code' => 200, 'data' => array(), 'message' => '浏览记录');
  195 +
  196 + do {
  197 + if (!$this->isAjax()) {
  198 + break;
  199 + }
  200 +
  201 + $page = $this->get('page', 1);
  202 +// $uid = $this->getUid(false);
  203 +// if ($uid) {
  204 +// $udid = $this->getUdid();
  205 +// $result = CartModel::getBrowseProduct($uid, $udid, $page);
  206 +// break;
  207 +// }
  208 +
  209 + $sknList = $this->getCookie('_browseskn');
  210 + if (empty($sknList)) {
  211 + break;
  212 + }
  213 + $sknList = explode(',', rawurldecode($sknList));
  214 + $result = CartModel::getNamedBrowseProduct($page, 6, $sknList);
  215 + }
  216 + while (false);
  217 +
  218 + $this->echoJson($result);
  219 + }
  220 +
  221 + /**
  222 + * 确认订单
  223 + */
  224 + public function orderEnsureAction()
  225 + {
  226 + $type = $this->get('type', 1);
  227 + $refer = Helpers::url('/cart/index/orderEnsure', array('type' => $type));
  228 +
  229 + // 审判用户是否已登录
  230 + $this->auditJumpLogin(true, $refer);
  231 +
  232 + $this->setTitle('填写订单', true, ' | ');
  233 + $this->setSimpleHeader();
  234 +
  235 + $cartType = ($type == 2) ? 'advance' : 'ordinary';
  236 + $isAdvanceCart = ($type == 2) ? true : false;
  237 + $uid = $this->getUid(true);
  238 +
  239 + $orderEnsure = CartModel::cartPay($uid, $cartType, $isAdvanceCart);
  240 + if (empty($orderEnsure)) {
  241 + $this->go(Helpers::url('/shopping/cart'));
  242 + }
  243 +
  244 + $this->_view->display('order-ensure', array(
  245 + 'orderEnsurePage' => true,
  246 + 'orderEnsure' => $orderEnsure,
  247 + ));
  248 + }
  249 +
  250 + /**
  251 + * 异步获取地址信息
  252 + *
  253 + * @return json
  254 + */
  255 + public function getAddressAction()
  256 + {
  257 + $result = array('code' => 200, 'data' => array(), 'message' => '地址信息');
  258 +
  259 + if ($this->isAjax()) {
  260 + $uid = $this->getUid(true);
  261 + $result['data'] = CartModel::userAddressList($uid);
  262 + }
  263 +
  264 + $this->echoJson($result);
  265 + }
  266 +
  267 + /**
  268 + * 设置为默认的地址
  269 + *
  270 + * @param int id 地址ID
  271 + * @return json
  272 + */
  273 + public function setDefaultAddressAction()
  274 + {
  275 + $result = array();
  276 +
  277 + if ($this->isAjax()) {
  278 + $uid = $this->getUid(true);
  279 + $addressId = $this->post('id');
  280 + $result = CartModel::setDefaultAddress($uid, $addressId);
  281 + }
  282 +
  283 + $this->echoJson($result);
  284 + }
  285 +
  286 + /**
  287 + * 保存地址信息
  288 + *
  289 + * @param int $id 地址ID ,当修改操作的时候需要传,添加时候不需要传
  290 + * @param string $address 地址信息
  291 + * @param int $areaCode 城市码
  292 + * @param string $consignee 收货人
  293 + * @param string $email 邮箱地址
  294 + * @param string $mobile 手机号码
  295 + * @param string $zipCode 邮编
  296 + * @return json
  297 + */
  298 + public function saveAddressAction()
  299 + {
  300 + $result = array();
  301 +
  302 + if ($this->isAjax()) {
  303 + $uid = $this->getUid(true);
  304 + $id = $this->post('id', null);
  305 + $address = $this->post('address', '');
  306 + $areaCode = $this->post('areaCode', '');
  307 + $consignee = $this->post('consignee', '');
  308 + $email = $this->post('email', '');
  309 + $mobile = $this->post('mobile', '');
  310 + $zipCode = $this->post('zipCode', '');
  311 + $result = CartModel::saveAddressData($uid, $address, $areaCode, $consignee, $email, $id, $mobile, $zipCode);
  312 + }
  313 +
  314 + $this->echoJson($result);
  315 + }
  316 +
  317 + /**
  318 + * 删除地址
  319 + *
  320 + * @param int id 地址ID
  321 + */
  322 + public function delAddressAction()
  323 + {
  324 + $result = array();
  325 +
  326 + if ($this->isAjax()) {
  327 + $uid = $this->getUid(true);
  328 + $addressId = $this->post('id');
  329 + $result = CartModel::delAddress($uid, $addressId);
  330 + }
  331 +
  332 + $this->echoJson($result);
  333 + }
  334 +
  335 + /**
  336 + * 获取省市区县信息列表
  337 + *
  338 + * @param int id
  339 + * @return json
  340 + */
  341 + public function getAreaListAction()
  342 + {
  343 + $result = array('code' => 200, 'message' => '地区信息', 'data' => array());
  344 +
  345 + if ($this->isAjax()) {
  346 + $id = $this->get('id', 0);
  347 + $result['data'] = CartModel::getAreaList($id);
  348 + }
  349 +
  350 + $this->echoJson($result);
  351 + }
  352 +
  353 + /**
  354 + * 获取优惠券列表
  355 + *
  356 + * @return json
  357 + */
  358 + public function getCouponListAction()
  359 + {
  360 + $result = array('code' => 200, 'message' => '优惠券信息', 'data' => array());
  361 +
  362 + if ($this->isAjax()) {
  363 + $uid = $this->getUid(true);
  364 + $result['data'] = CartModel::getCouponList($uid);
  365 + }
  366 +
  367 + $this->echoJson($result);
  368 + }
  369 +
  370 + /**
  371 + * 购物车选择改变字段,重新运算订单数据
  372 + *
  373 + * @param string $cartType 购物车类型,ordinary表示普通, advance表示预售
  374 + * @param int $deliveryWay 配送方式,1表示普通快递,2表示顺丰速运
  375 + * @param int $paymentType 支付方式,1表示在线支付,2表示货到付款
  376 + * @param string $couponCode 优惠券码
  377 + * @param mixed $yohoCoin 使用的YOHO币数量
  378 + * @param int $redEnvelopes 红包
  379 + * @return json
  380 + */
  381 + public function orderComputeAction()
  382 + {
  383 + $result = array();
  384 +
  385 + if ($this->isAjax()) {
  386 + $cartType = $this->post('cartType', 'ordinary');
  387 + $deliveryWay = $this->post('deliveryWay', 1);
  388 + $paymentType = $this->post('paymentType', 1);
  389 + $couponCode = $this->post('couponCode', null);
  390 + $yohoCoin = $this->post('yohoCoin', null);
  391 + $redEnvelopes = $this->post('redEnvelopes', null);
  392 + $uid = $this->getUid(true);
  393 + $result = CartModel::orderCompute($uid, $cartType, $deliveryWay, $paymentType, $couponCode, $yohoCoin, $redEnvelopes);
  394 + }
  395 +
  396 + $this->echoJson($result);
  397 + }
  398 +
  399 + /**
  400 + * 确认结算订单
  401 + *
  402 + * @param int $addressId 地址ID
  403 + * @param int $cartType 购物车类型ID
  404 + * @param int $deliveryTimeId 寄送时间ID
  405 + * @param int $deliveryWayId 寄送方式ID
  406 + * @param string $invoiceTitle 发票抬头
  407 + * @param int $invoiceId 发票类型ID
  408 + * @param int $paymentId 支付方式ID
  409 + * @param int $paymentType 支付类型ID
  410 + * @param string $remark 留言
  411 + * @param string $couponCode 优惠券码
  412 + * @param mixed $yohoCoin 使用的YOHO币数量或为空
  413 + * @param int $isPreContact 送货前是否联系 true:是, false:否
  414 + * @param int $isPrintPrice 是否打印价格 true:是, false:否
  415 + * @param int $redEnvelopes 红包
  416 + * @return json
  417 + */
  418 + public function orderSubAction()
  419 + {
  420 + $result = array('code' => 400, 'data' => array(), 'message' => CartModel::ERROR_400_MESSAGE);
  421 +
  422 + do {
  423 + // 判断是否是AJAX请求
  424 + if (!$this->isAjax()) {
  425 + break;
  426 + }
  427 +
  428 + // 判断用户是否登录
  429 + $uid = $this->getUid(true);
  430 + if (!$uid) {
  431 + $result['message'] = '请先登录';
  432 + break;
  433 + }
  434 +
  435 + $addressId = $this->post('addressId', null);
  436 + $cartType = $this->post('cartType', 'ordinary'); // 默认普通购物车
  437 + $deliveryTimeId = $this->post('deliveryTimeId', 1); // 默认只工作日配送
  438 + $deliveryWayId = $this->post('deliveryWayId', 1); // 默认普通快递
  439 + $invoiceTitle = $this->post('invoiceTitle', null); // 发票抬头
  440 + $invoiceId = $this->post('invoiceId', null); // 发票类型
  441 + $paymentId = $this->post('paymentId', 15); // 支付ID
  442 + $paymentType = $this->post('paymentType', 1); // 默认在线支付
  443 + $remark = $this->post('remark', ''); // 备注信息
  444 + $couponCode = $this->post('couponCode', null); // 优惠码
  445 + $yohoCoin = $this->post('yohoCoin', 1); // YOHO币
  446 + $isPreContact = $this->post('isPreContact', false); // 送货前是否联系
  447 + $isPrintPrice = $this->post('isPrintPrice', true); // 是否打印价格
  448 + $redEnvelopes = $this->post('redEnvelopes', null);
  449 +
  450 + // 调用下单接口
  451 + $result = CartModel::orderSub($uid, $addressId, $cartType, $deliveryTimeId, $deliveryWayId, $invoiceTitle, $invoiceId,
  452 + $paymentId, $paymentType, $remark, $couponCode, $yohoCoin, $isPreContact, $isPrintPrice, $redEnvelopes);
  453 +
  454 + // 判断是否下单成功
  455 + if (empty($result['data']['order_code'])) {
  456 + break;
  457 + }
  458 +
  459 + // 跳转到支付的URL链接
  460 + $result['data']['payUrl'] = Helpers::url('/shopping/pay', array('ordercode' => $result['data']['order_code']));
  461 + }
  462 + while (false);
  463 +
  464 +// $result = CartModel::orderSub($uid, $addressId, $cartType, $deliveryTime, $deliveryWay, $invoiceTitle, $invoiceId, $paymentId, $paymentType, $remark, $couponCode, $yohoCoin);
  465 +//
  466 +// // 记录下单异常的数据
  467 +// if (empty($result)) {
  468 +// $message = 'uid:' . $uid . ',addressId:' . $addressId . ',cartType:' . $cartType . ',deliveryTime:' . $deliveryTime
  469 +// . ',deliveryWay:' . $deliveryWay . 'invoiceTitle:' . $invoiceTitle . ',invoiceId:' . $invoiceId . ',yohoCoin:' . $yohoCoin
  470 +// . ',paymentId:' . $paymentId . ',paymentType:' . $paymentType . ',remark:' . $remark . ',couponCode:' . $couponCode . "\n";
  471 +// error_log($message, 3, '/Data/logs/php/pc_error/order.' . date('Ym') . '.log');
  472 +// }
  473 +// // 返回数据
  474 +// else {
  475 +// // 提交成功清除Cookie
  476 +// $this->setCookie('order-info', null);
  477 +//
  478 +// $this->echoJson($result);
  479 +// }
  480 +//
  481 +// if ($uid && !empty($result['data'])) {
  482 +// try {
  483 +// UnionTrans::set($uid, $result['data']['order_code'], $result['data']['order_amount']);
  484 +// } catch (Exception $e) {
  485 +// // do nothing
  486 +// }
  487 +// }
  488 +
  489 + $this->echoJson($result);
  490 + }
  491 +
  492 + /**
  493 + * 加入购物车
  494 + *
  495 + * @param string productSku 商品的SKU
  496 + * @param int buyNumber 购买数量
  497 + * @param int promotionId 促销ID, 加价购有关
  498 + * @param int goodsType 商品类型,0表示普通商品,1表示加价购商品
  499 + * @param int isEdit 是否是编辑商品SKU,0表示不是编辑
  500 + * @return json
  501 + */
  502 + public function addAction()
  503 + {
  504 + $result = array();
  505 +
  506 + if ($this->isAjax()) {
  507 + $shoppingKey = Helpers::getShoppingKeyByCookie();
  508 + $productSku = $this->post('productSku');
  509 + $buyNumber = $this->post('buyNumber', 1);
  510 + $goodsType = $this->post('goodsType', 0);
  511 + $promotionId = $this->post('promotionId', 0);
  512 + $isEdit = $this->post('isEdit', 0);
  513 + $uid = $this->getUid(true);
  514 +
  515 + // 执行加入购物车操作
  516 + $result = CartModel::addToCart($productSku, $buyNumber, $goodsType, $isEdit, $promotionId, $uid, $shoppingKey);
  517 +
  518 + // 设置加入购物车凭证到客户端浏览器
  519 + if (empty($shoppingKey) && isset($result['data']['shopping_key'])) {
  520 + $this->setCookie('_SPK', $result['data']['shopping_key'], time() + 86400 * 360);
  521 + }
  522 + // 老站购物车需要的COOKIE
  523 + if (isset($result['data']['shopping_key'])) {
  524 + $this->setCookie('_g', json_encode(array(
  525 + '_k' => $result['data']['shopping_key'],
  526 + '_nac' => $result['data']['goods_count'],
  527 + '_ac' => 0,
  528 + '_r' => 1
  529 + )));
  530 + }
  531 + }
  532 +
  533 + $this->echoJson($result);
  534 + }
  535 +
  536 + /**
  537 + * 获取商品信息
  538 + */
  539 + public function getProductInfoAction()
  540 + {
  541 + $productId = $this->get('productId');
  542 + $uid = $this->getUid();
  543 + $vipLevel = -1;
  544 + $data = array();
  545 + if(!empty($this->_vip)) {
  546 + $vipLevel = Helpers::getVipLevel($this->_vip);
  547 + }
  548 + if(!empty($productId)) {
  549 + $data = ItemModel::getCartProductInfo($productId, $uid, $vipLevel);
  550 + }
  551 + $this->_view->display('goods-detail', $data);
  552 + }
  553 +
  554 + /**
  555 + * 获取购物车商品总数
  556 + *
  557 + * @return jsonp
  558 + */
  559 + public function countAction()
  560 + {
  561 + $callback = $this->get('callback', 'callback');
  562 + $uid = $this->getUid(false);
  563 + $shoppingKey = Helpers::getShoppingKeyByCookie();
  564 + $result = CartModel::getCartCount($uid, $shoppingKey);
  565 +
  566 + $this->helpJsonCallbackResult($callback, 200, '总数', $result);
  567 + }
  568 +
  569 + /**
  570 + * 设置购物车COOKIE信息
  571 + */
  572 + private function setShoppingCookie($uid = null)
  573 + {
  574 + ($uid === null) ? $uid = $this->getUid(false) : true;
  575 + $shoppingKey = Helpers::getShoppingKeyByCookie();
  576 + $cartCount = CartModel::getCartCount($uid, $shoppingKey);
  577 + if (!empty($cartCount['data']['cart_goods_count'])) {
  578 + $this->setCookie('_g', json_encode(array(
  579 + '_k' => $shoppingKey,
  580 + '_nac' => $cartCount['data']['cart_goods_count'],
  581 + '_ac' => 0,
  582 + '_r' => 1
  583 + )));
  584 + }
  585 + }
  586 +
  587 +}