StoreCartController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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, $activityId, $liveId, $new,$is_gift) = UtilService::postMore([
  34. ['productId', 0],//普通产品编号
  35. ['cartNum', 1], //购物车数量
  36. ['uniqueId', ''],//属性唯一值
  37. ['combinationId', 0],//拼团产品编号
  38. ['secKillId', 0],//秒杀产品编号
  39. ['bargainId', 0],//砍价产品编号
  40. ['activityId', 0],//活动产品编号
  41. ['liveId', 0],//直播产品编号
  42. ['new', 1], // 1 加入购物车直接购买 0 加入购物车
  43. ['is_gift',0]
  44. ], $request, true);
  45. if (!$productId || !is_numeric($productId)) return app('json')->fail('参数错误');
  46. if ($bargainId && StoreBargainUserHelp::getSurplusPrice($bargainId, $request->uid())) return app('json')->fail('请先砍价');
  47. $res = StoreCart::setCart($request->uid(), $productId, $cartNum, $uniqueId, 'product', $new, $combinationId, $secKillId, $bargainId, $activityId, $liveId,$is_gift);
  48. if (!$res) return app('json')->fail(StoreCart::getErrorInfo());
  49. else return app('json')->successful('ok', ['cartId' => $res->id]);
  50. }
  51. /**
  52. * 购物车 删除产品
  53. * @param Request $request
  54. * @return mixed
  55. */
  56. public function del(Request $request)
  57. {
  58. list($ids) = UtilService::postMore([
  59. ['ids', []],//购物车编号
  60. ], $request, true);
  61. if (!count($ids))
  62. return app('json')->fail('参数错误!');
  63. if (StoreCart::removeUserCart($request->uid(), $ids))
  64. return app('json')->successful();
  65. return app('json')->fail('清除失败!');
  66. }
  67. /**
  68. * 购物车 修改产品数量
  69. * @param Request $request
  70. * @return mixed
  71. * @throws \think\Exception
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. * @throws \think\exception\DbException
  75. */
  76. public function num(Request $request)
  77. {
  78. list($id, $number) = UtilService::postMore([
  79. ['id', 0],//购物车编号
  80. ['number', 0],//购物车编号
  81. ], $request, true);
  82. if (!$id || !$number || !is_numeric($id) || !is_numeric($number)) return app('json')->fail('参数错误!');
  83. $res = StoreCart::changeUserCartNum($id, $number, $request->uid());
  84. if ($res) return app('json')->successful();
  85. else return app('json')->fail(StoreCart::getErrorInfo('修改失败'));
  86. }
  87. /**
  88. * 购物车 获取数量
  89. * @param Request $request
  90. * @return mixed
  91. */
  92. public function count(Request $request)
  93. {
  94. list($numType) = UtilService::postMore([
  95. ['numType', true],//购物车编号
  96. ], $request, true);
  97. if (!(int)$numType) $numType = false;
  98. return app('json')->success('ok', ['count' => StoreCart::getUserCartNum($request->uid(), 'product', $numType)]);
  99. }
  100. }