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