Cart.php 4.1 KB

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