Cart.php 4.0 KB

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