Cart.php 4.1 KB

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