StoreCartController.php 3.6 KB

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