Cart.Class.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * 购物车
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2019/11/6
  7. * Time: 16:10
  8. */
  9. namespace JinDouYun\Controller\Cart;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\StatusCode;
  12. use JinDouYun\Controller\BaseController;
  13. use JinDouYun\Model\Cart\MCart;
  14. class Cart extends BaseController
  15. {
  16. private $objMCart;
  17. /**
  18. * ApiCart constructor.
  19. * @param bool $isCheckAcl
  20. * @param bool $isMustLogin
  21. * @throws \Exception
  22. */
  23. public function __construct($isCheckAcl = false, $isMustLogin = true)
  24. {
  25. parent::__construct($isCheckAcl, $isMustLogin);
  26. $userCenterId = $this->request->param('request_id');
  27. $this->onlineUserId = $userCenterId;
  28. $this->objMCart = new MCart($this->onlineUserId, $this->onlineEnterpriseId);
  29. }
  30. /**
  31. * 添加购物车,公共字段
  32. *
  33. * @return array
  34. */
  35. public function commonFieldFilter()
  36. {
  37. $params = $this->request->getRawJson();
  38. if (empty($params)) {
  39. parent::sendOutput('参数为空', ErrorCode::$paramError);
  40. }
  41. $cartData = [
  42. 'goodsData' => isset($params['goodsData']) ? $params['goodsData'] : [],//商品数据
  43. ];
  44. foreach ($cartData as $key => $value) {
  45. if (empty($value) && $value !== 0) {
  46. parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
  47. }
  48. }
  49. $goodsData = [];
  50. foreach ($cartData['goodsData'] as $key => $val) {
  51. $goodsData[$key] = [
  52. 'goodsBasicId' => isset($val['goodsBasicId']) ? $val['goodsBasicId'] : '',
  53. 'goodsId' => isset($val['goodsId']) ? $val['goodsId'] : '',
  54. 'skuId' => isset($val['skuId']) ? $val['skuId'] : '',
  55. 'buyNum' => isset($val['buyNum']) ? $val['buyNum'] : '',
  56. 'shopId' => isset($val['shopId']) ? $val['shopId'] : '',
  57. 'source' => isset($val['source']) ? $val['source'] : '',
  58. ];
  59. foreach ($goodsData[$key] as $k => $v) {
  60. if (empty($v)) {
  61. parent::sendOutput($k . '参数错误', ErrorCode::$paramError);
  62. }
  63. }
  64. $goodsData[$key]['goodsCode'] = createCode(StatusCode::$code['goodsBasic']['prefix'], $goodsData[$key]['goodsId'], StatusCode::$code['goodsBasic']['length']);
  65. }
  66. $cartData['goodsData'] = $goodsData;//过滤后数据
  67. return $cartData;
  68. }
  69. /**
  70. * 加入购物车
  71. * @throws \Exception
  72. */
  73. public function addCart()
  74. {
  75. $cartData = $this->commonFieldFilter();
  76. $result = $this->objMCart->manageAddCart($cartData);
  77. if ($result->isSuccess()) {
  78. parent::sendOutput($result->getData());
  79. }
  80. parent::sendOutput($result->getData(), $result->getErrorCode());
  81. }
  82. /**
  83. * 更新商品数量
  84. * @throws \Exception
  85. */
  86. public function updateBuyNum()
  87. {
  88. $paramsData = $this->request->getRawJson();
  89. $params = [
  90. 'cartId' => isset($paramsData['cartId']) ? $paramsData['cartId'] : '',
  91. 'buyNum' => isset($paramsData['buyNum']) ? $paramsData['buyNum'] : '',
  92. ];
  93. foreach ($params as $key => $value) {
  94. if (empty($value) && $value !== 0) {
  95. $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
  96. }
  97. }
  98. $result = $this->objMCart->updateBuyNum($params);
  99. if ($result->isSuccess()) {
  100. parent::sendOutput($result->getData());
  101. } else {
  102. parent::sendOutput($result->getData(), $result->getErrorCode());
  103. }
  104. }
  105. /**
  106. * inventory字段废弃 inventoryNum库存数量
  107. * 获取用户购物车数据
  108. * @throws \Exception
  109. */
  110. public function getCartByUserCenterId()
  111. {
  112. $result = $this->objMCart->getManageCartByUserCenterId();
  113. if ($result->isSuccess()) {
  114. $returnData = $result->getData();
  115. $pageData = [
  116. 'pageIndex' => 0,
  117. 'pageSize' => 0,
  118. 'pageTotal' => $returnData['total'],
  119. ];
  120. parent::sendOutput($returnData['data'], 0, $pageData);
  121. }
  122. parent::sendOutput($result->getData(), ErrorCode::$dberror);
  123. }
  124. /**
  125. * 更新选中状态
  126. * @throws \Exception
  127. */
  128. public function updateSelection()
  129. {
  130. $paramsData = $this->request->getRawJson();
  131. $params = [
  132. 'selection' => isset($paramsData['selection']) ? $paramsData['selection'] : '',//4=>未选中 5=>已选中
  133. 'id' => isset($paramsData['cartId']) ? $paramsData['cartId'] : 0,
  134. ];
  135. foreach ($params as $key => $value) {
  136. if (empty($value) && $value !== 0) {
  137. $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
  138. }
  139. }
  140. $result = $this->objMCart->updateSelection($params);
  141. if ($result->isSuccess()) {
  142. parent::sendOutput($result->getData());
  143. } else {
  144. parent::sendOutput($result->getData(), $result->getErrorCode());
  145. }
  146. }
  147. /**
  148. * 删除购物车中商品(可批量)
  149. */
  150. public function delCart()
  151. {
  152. $params = $this->request->getRawJson();
  153. $paramsData = [
  154. 'id' => isset($params['cartId']) ? $params['cartId'] : 0,
  155. ];
  156. foreach ($paramsData as $key => $value) {
  157. if (empty($value) && $value !== 0) {
  158. $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
  159. }
  160. }
  161. $result = $this->objMCart->delCart($paramsData);
  162. if ($result->isSuccess()) {
  163. parent::sendOutput($result->getData());
  164. } else {
  165. parent::sendOutput($result->getData(), $result->getErrorCode());
  166. }
  167. }
  168. /**
  169. * 清空指定用户下购物车数据
  170. */
  171. public function clearCart()
  172. {
  173. $result = $this->objMCart->clearCart();
  174. if ($result->isSuccess()) {
  175. parent::sendOutput($result->getData());
  176. } else {
  177. parent::sendOutput($result->getData(), $result->getErrorCode());
  178. }
  179. }
  180. /**
  181. * 批量删除购物车商品
  182. */
  183. public function clearCartByGoodsId()
  184. {
  185. $params = $this->request->getRawJson();
  186. $paramsData = [
  187. 'goodsId' => isset($params['goodsId']) ? $params['goodsId'] : '',
  188. ];
  189. foreach ($paramsData as $key => $value) {
  190. if (empty($value)) {
  191. $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
  192. }
  193. }
  194. $result = $this->objMCart->clearCartByGoodsId($paramsData);
  195. if ($result->isSuccess()) {
  196. parent::sendOutput($result->getData());
  197. } else {
  198. parent::sendOutput($result->getData(), $result->getErrorCode());
  199. }
  200. }
  201. }