StoreCartController.php 3.7 KB

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