Cart.php 4.3 KB

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