123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- /**
- * 购物车
- * Created by PhpStorm.
- * User: XiaoMing
- * Date: 2019/11/6
- * Time: 16:10
- */
- namespace JinDouYun\Controller\Cart;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\StatusCode;
- use JinDouYun\Controller\BaseController;
- use JinDouYun\Model\Cart\MCart;
- class Cart extends BaseController
- {
- private $objMCart;
- /**
- * ApiCart constructor.
- * @param bool $isCheckAcl
- * @param bool $isMustLogin
- * @throws \Exception
- */
- public function __construct($isCheckAcl = false, $isMustLogin = true)
- {
- parent::__construct($isCheckAcl, $isMustLogin);
- $userCenterId = $this->request->param('request_id');
- $this->onlineUserId = $userCenterId;
- $this->objMCart = new MCart($this->onlineUserId, $this->onlineEnterpriseId);
- }
- /**
- * 添加购物车,公共字段
- *
- * @return array
- */
- public function commonFieldFilter()
- {
- $params = $this->request->getRawJson();
- if (empty($params)) {
- parent::sendOutput('参数为空', ErrorCode::$paramError);
- }
- $cartData = [
- 'goodsData' => isset($params['goodsData']) ? $params['goodsData'] : [],//商品数据
- ];
- foreach ($cartData as $key => $value) {
- if (empty($value) && $value !== 0) {
- parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
- }
- }
- $goodsData = [];
- foreach ($cartData['goodsData'] as $key => $val) {
- $goodsData[$key] = [
- 'goodsBasicId' => isset($val['goodsBasicId']) ? $val['goodsBasicId'] : '',
- 'goodsId' => isset($val['goodsId']) ? $val['goodsId'] : '',
- 'skuId' => isset($val['skuId']) ? $val['skuId'] : '',
- 'buyNum' => isset($val['buyNum']) ? $val['buyNum'] : '',
- 'shopId' => isset($val['shopId']) ? $val['shopId'] : '',
- 'source' => isset($val['source']) ? $val['source'] : '',
- ];
- foreach ($goodsData[$key] as $k => $v) {
- if (empty($v)) {
- parent::sendOutput($k . '参数错误', ErrorCode::$paramError);
- }
- }
- $goodsData[$key]['goodsCode'] = createCode(StatusCode::$code['goodsBasic']['prefix'], $goodsData[$key]['goodsId'], StatusCode::$code['goodsBasic']['length']);
- }
- $cartData['goodsData'] = $goodsData;//过滤后数据
- return $cartData;
- }
- /**
- * 加入购物车
- * @throws \Exception
- */
- public function addCart()
- {
- $cartData = $this->commonFieldFilter();
- $result = $this->objMCart->manageAddCart($cartData);
- if ($result->isSuccess()) {
- parent::sendOutput($result->getData());
- }
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
- /**
- * 更新商品数量
- * @throws \Exception
- */
- public function updateBuyNum()
- {
- $paramsData = $this->request->getRawJson();
- $params = [
- 'cartId' => isset($paramsData['cartId']) ? $paramsData['cartId'] : '',
- 'buyNum' => isset($paramsData['buyNum']) ? $paramsData['buyNum'] : '',
- ];
- foreach ($params as $key => $value) {
- if (empty($value) && $value !== 0) {
- $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
- }
- }
- $result = $this->objMCart->updateBuyNum($params);
- if ($result->isSuccess()) {
- parent::sendOutput($result->getData());
- } else {
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
- }
- /**
- * inventory字段废弃 inventoryNum库存数量
- * 获取用户购物车数据
- * @throws \Exception
- */
- public function getCartByUserCenterId()
- {
- $result = $this->objMCart->getManageCartByUserCenterId();
- if ($result->isSuccess()) {
- $returnData = $result->getData();
- $pageData = [
- 'pageIndex' => 0,
- 'pageSize' => 0,
- 'pageTotal' => $returnData['total'],
- ];
- parent::sendOutput($returnData['data'], 0, $pageData);
- }
- parent::sendOutput($result->getData(), ErrorCode::$dberror);
- }
- /**
- * 更新选中状态
- * @throws \Exception
- */
- public function updateSelection()
- {
- $paramsData = $this->request->getRawJson();
- $params = [
- 'selection' => isset($paramsData['selection']) ? $paramsData['selection'] : '',//4=>未选中 5=>已选中
- 'id' => isset($paramsData['cartId']) ? $paramsData['cartId'] : 0,
- ];
- foreach ($params as $key => $value) {
- if (empty($value) && $value !== 0) {
- $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
- }
- }
- $result = $this->objMCart->updateSelection($params);
- if ($result->isSuccess()) {
- parent::sendOutput($result->getData());
- } else {
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
- }
- /**
- * 删除购物车中商品(可批量)
- */
- public function delCart()
- {
- $params = $this->request->getRawJson();
- $paramsData = [
- 'id' => isset($params['cartId']) ? $params['cartId'] : 0,
- ];
- foreach ($paramsData as $key => $value) {
- if (empty($value) && $value !== 0) {
- $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
- }
- }
- $result = $this->objMCart->delCart($paramsData);
- if ($result->isSuccess()) {
- parent::sendOutput($result->getData());
- } else {
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
- }
- /**
- * 清空指定用户下购物车数据
- */
- public function clearCart()
- {
- $result = $this->objMCart->clearCart();
- if ($result->isSuccess()) {
- parent::sendOutput($result->getData());
- } else {
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
- }
- /**
- * 批量删除购物车商品
- */
- public function clearCartByGoodsId()
- {
- $params = $this->request->getRawJson();
- $paramsData = [
- 'goodsId' => isset($params['goodsId']) ? $params['goodsId'] : '',
- ];
- foreach ($paramsData as $key => $value) {
- if (empty($value)) {
- $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
- }
- }
- $result = $this->objMCart->clearCartByGoodsId($paramsData);
- if ($result->isSuccess()) {
- parent::sendOutput($result->getData());
- } else {
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
- }
- }
|