Cart.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\controller;
  13. use think\response\Json;
  14. use app\api\model\Cart as CartModel;
  15. use app\api\service\Cart as CartService;
  16. use cores\exception\BaseException;
  17. /**
  18. * 购物车管理
  19. * Class Cart
  20. * @package app\api\controller
  21. */
  22. class Cart extends Controller
  23. {
  24. /**
  25. * 购物车商品列表
  26. * @return Json
  27. * @throws BaseException
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\DbException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. * @throws \cores\exception\BaseException
  32. */
  33. public function list(): Json
  34. {
  35. // 购物车商品列表
  36. $service = new CartService;
  37. $list = $service->getList();
  38. // 购物车商品总数量
  39. $cartTotal = (new CartModel)->getCartTotal();
  40. return $this->renderSuccess(compact('cartTotal', 'list'));
  41. }
  42. /**
  43. * 购物车商品总数量
  44. * @return Json
  45. * @throws BaseException
  46. */
  47. public function total(): Json
  48. {
  49. $model = new CartModel;
  50. $cartTotal = $model->getCartTotal();
  51. return $this->renderSuccess(compact('cartTotal'));
  52. }
  53. /**
  54. * 加入购物车
  55. * @param int $goodsId 商品ID
  56. * @param string $goodsSkuId 商品sku索引
  57. * @param int $goodsNum 商品数量
  58. * @return Json
  59. * @throws BaseException
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. public function add(int $goodsId, string $goodsSkuId, int $goodsNum): Json
  65. {
  66. $model = new CartModel;
  67. if (!$model->add($goodsId, $goodsSkuId, $goodsNum)) {
  68. return $this->renderError($model->getError() ?: '加入购物车失败');
  69. }
  70. // 购物车商品总数量
  71. $cartTotal = $model->getCartTotal();
  72. return $this->renderSuccess(compact('cartTotal'), '加入购物车成功');
  73. }
  74. /**
  75. * 更新购物车商品数量
  76. * @param int $goodsId 商品ID
  77. * @param string $goodsSkuId 商品sku索引
  78. * @param int $goodsNum 商品数量
  79. * @return Json
  80. * @throws BaseException
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. */
  85. public function update(int $goodsId, string $goodsSkuId, int $goodsNum): Json
  86. {
  87. $model = new CartModel;
  88. if (!$model->sUpdate($goodsId, $goodsSkuId, $goodsNum)) {
  89. return $this->renderError($model->getError() ?: '更新失败');
  90. }
  91. // 购物车商品总数量
  92. $cartTotal = $model->getCartTotal();
  93. return $this->renderSuccess(compact('cartTotal'), '更新成功');
  94. }
  95. /**
  96. * 删除购物车中指定记录
  97. * @param array $cartIds 购物车ID集, 如果为空删除所有
  98. * @return Json
  99. * @throws BaseException
  100. */
  101. public function clear(array $cartIds = []): Json
  102. {
  103. $model = new CartModel;
  104. if (!$model->clear($cartIds)) {
  105. return $this->renderError($model->getError() ?: '操作失败');
  106. }
  107. // 购物车商品总数量
  108. $cartTotal = $model->getCartTotal();
  109. return $this->renderSuccess(compact('cartTotal'), '操作成功');
  110. }
  111. }