StoreCartController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\api\controller\store;
  3. use app\models\store\StoreBargainUserHelp;
  4. use app\models\store\StoreCart;
  5. use app\models\store\StoreProduct;
  6. use app\Request;
  7. use crmeb\services\UtilService;
  8. use Exception;
  9. use think\db\exception\DataNotFoundException;
  10. use think\db\exception\DbException;
  11. use think\db\exception\ModelNotFoundException;
  12. /**
  13. * 购物车类
  14. * Class StoreCartController
  15. * @package app\api\controller\store
  16. */
  17. class StoreCartController
  18. {
  19. /**
  20. * 购物车 列表
  21. * @param Request $request
  22. * @return mixed
  23. * @throws DataNotFoundException
  24. * @throws DbException
  25. * @throws ModelNotFoundException
  26. */
  27. public function lst(Request $request)
  28. {
  29. return app('json')->successful(StoreCart::getUserProductCartList($request->uid(), '', 0, $request->mer_id()));
  30. }
  31. /**
  32. * 购物车 添加
  33. * @param Request $request
  34. * @return mixed
  35. * @throws DataNotFoundException
  36. * @throws ModelNotFoundException
  37. * @throws DbException
  38. * @throws Exception
  39. */
  40. public function add(Request $request)
  41. {
  42. list($productId, $cartNum, $uniqueId, $assistanceId, $combinationId, $secKillId, $bargainId, $new) = UtilService::postMore([
  43. [['productId', 'd'], 0],//普通商品编号
  44. [['cartNum', 'd'], 1], //购物车数量
  45. ['uniqueId', ''],//属性唯一值
  46. [['assistanceId', 'd'], 0],//助力商品编号
  47. [['combinationId', 'd'], 0],//拼团商品编号
  48. [['secKillId', 'd'], 0],//秒杀商品编号
  49. [['bargainId', 'd'], 0],//砍价商品编号
  50. [['new', 'd'], 1], // 1 加入购物车直接购买 0 加入购物车
  51. ], $request, true);
  52. if (!$productId || !is_numeric($productId)) return app('json')->fail('参数错误');
  53. if (!StoreProduct::merSet($request->mer_id())->where('id', $productId)->find()) return app('json')->fail('参数错误');
  54. if ($bargainId && StoreBargainUserHelp::getSurplusPrice($bargainId, $request->uid())) return app('json')->fail('请先砍价');
  55. $res = StoreCart::setCart($request->uid(), $productId, $cartNum, $uniqueId, 'product', $new, $assistanceId, $combinationId, $secKillId, $bargainId, $request->mer_id());
  56. if (!$res) return app('json')->fail(StoreCart::getErrorInfo());
  57. else return app('json')->successful('ok', ['cartId' => $res->id]);
  58. }
  59. /**
  60. * 购物车 删除商品
  61. * @param Request $request
  62. * @return mixed
  63. * @throws Exception
  64. */
  65. public function del(Request $request)
  66. {
  67. list($ids) = UtilService::postMore([
  68. ['ids', []],//购物车编号
  69. ], $request, true);
  70. if (!count($ids))
  71. return app('json')->fail('参数错误!');
  72. if (StoreCart::removeUserCart($request->uid(), $ids))
  73. return app('json')->successful();
  74. return app('json')->fail('清除失败!');
  75. }
  76. /**
  77. * 购物车 修改商品数量
  78. * @param Request $request
  79. * @return mixed
  80. * @throws DataNotFoundException
  81. * @throws ModelNotFoundException
  82. * @throws DbException
  83. * @throws Exception
  84. */
  85. public function num(Request $request)
  86. {
  87. list($id, $number) = UtilService::postMore([
  88. ['id', 0],//购物车编号
  89. ['number', 0],//购物车编号
  90. ], $request, true);
  91. if (!$id || !$number || !is_numeric($id) || !is_numeric($number)) return app('json')->fail('参数错误!');
  92. $res = StoreCart::changeUserCartNum($id, $number, $request->uid(), $request->mer_id());
  93. if ($res) return app('json')->successful();
  94. else return app('json')->fail(StoreCart::getErrorInfo('修改失败'));
  95. }
  96. /**
  97. * 购物车 获取数量
  98. * @param Request $request
  99. * @return mixed
  100. * @throws Exception
  101. */
  102. public function count(Request $request)
  103. {
  104. list($numType) = UtilService::postMore([
  105. ['numType', true],//购物车编号
  106. ], $request, true);
  107. if (!(int)$numType) $numType = false;
  108. return app('json')->success('ok', ['count' => StoreCart::getUserCartNum($request->uid(), 'product', $numType)]);
  109. }
  110. }