Cart.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\index\controller;
  13. use app\api\model\Cart as CartModel;
  14. use app\index\service\Cart as CartService;
  15. use app\common\exception\BaseException;
  16. use think\facade\Session;
  17. use think\response\Json;
  18. use think\View;
  19. /**
  20. * 购物车管理
  21. * Class Cart
  22. * @package app\api\controller
  23. */
  24. class Cart extends Controller
  25. {
  26. /**
  27. * @return |\think\response\View
  28. */
  29. public function shoppingCart()
  30. {
  31. $userId = Session::get('user_id');
  32. if (empty($userId)) {
  33. return view('passport/login');
  34. }
  35. // 购物车商品列表
  36. $service = new CartService;
  37. $list = $service->getList();
  38. // 购物车商品总数量
  39. $cartTotal = (new CartModel)->getCartTotal();
  40. //dd($list->toArray(),$cartTotal);
  41. $user = $this->getCurrentLoginUser();
  42. //dd($user->toArray());
  43. $cartMoney = '0.00';
  44. foreach ($list as $item) {
  45. $temp = bcmul(strval($item['goods_num']), $item['goods']['goods_price_min'], 2);
  46. $cartMoney = bcadd($cartMoney, $temp, 2);
  47. }
  48. return view('shoppingCart', ['list' => $list, 'cartTotal' => $cartTotal,'cartMoney'=>$cartMoney, 'points' => $user['points']]);
  49. }
  50. /**
  51. * 购物车商品列表
  52. * @return Json
  53. * @throws BaseException
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. * @throws \cores\exception\BaseException
  58. */
  59. public function list(): Json
  60. {
  61. // 购物车商品列表
  62. $service = new CartService;
  63. $list = $service->getList();
  64. // 购物车商品总数量
  65. $cartTotal = (new CartModel)->getCartTotal();
  66. return $this->renderSuccess(compact('cartTotal', 'list'));
  67. }
  68. /**
  69. * 购物车商品总数量
  70. * @return Json
  71. * @throws BaseException
  72. */
  73. public function total(): Json
  74. {
  75. $model = new CartModel;
  76. $cartTotal = $model->getCartTotal();
  77. return $this->renderSuccess(compact('cartTotal'));
  78. }
  79. /**
  80. * 加入购物车
  81. * @param int $goodsId 商品ID
  82. * @param string $goodsSkuId 商品sku索引
  83. * @param int $goodsNum 商品数量
  84. * @return Json |\think\response\View
  85. * @throws BaseException
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\DbException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. */
  90. public function add(int $goodsId, string $goodsSkuId, int $goodsNum)
  91. {
  92. $userId = Session::get('user_id');
  93. if (empty($userId)) {
  94. return view('passport/logIn');
  95. }
  96. $model = new CartModel;
  97. if (!$model->add($goodsId, $goodsSkuId, $goodsNum)) {
  98. return $this->renderError($model->getError() ?: '加入购物车失败');
  99. }
  100. // 购物车商品总数量
  101. $cartTotal = $model->getCartTotal();
  102. return $this->renderSuccess(compact('cartTotal'), '加入购物车成功');
  103. }
  104. /**
  105. * 更新购物车商品数量
  106. * @param int $goodsId 商品ID
  107. * @param string $goodsSkuId 商品sku索引
  108. * @param int $goodsNum 商品数量
  109. * @return Json |\think\response\View
  110. * @throws BaseException
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\DbException
  113. * @throws \think\db\exception\ModelNotFoundException
  114. */
  115. public function update(int $goodsId, string $goodsSkuId, int $goodsNum): Json
  116. {
  117. $userId = Session::get('user_id');
  118. if (empty($userId)) {
  119. return view('passport/logIn');
  120. }
  121. $model = new CartModel;
  122. if (!$model->sUpdate($goodsId, $goodsSkuId, $goodsNum)) {
  123. return $this->renderError($model->getError() ?: '更新失败');
  124. }
  125. // 购物车商品总数量
  126. //$cartTotal = $model->getCartTotal();
  127. $cartMoney = $this->getCartMoney();
  128. return $this->renderSuccess(compact('cartMoney'), '更新成功');
  129. }
  130. /**
  131. * 删除购物车中指定记录
  132. * @param array $cartIds 购物车ID集, 如果为空删除所有
  133. * @return Json
  134. * @throws BaseException
  135. */
  136. public function clear(array $cartIds = []): Json
  137. {
  138. $model = new CartModel;
  139. if (!$model->clear($cartIds)) {
  140. return $this->renderError($model->getError() ?: '操作失败');
  141. }
  142. // 购物车商品总数量
  143. //$cartTotal = $model->getCartTotal();
  144. $cartMoney = $this->getCartMoney();
  145. return $this->renderSuccess(compact('cartMoney'), '操作成功');
  146. }
  147. private function getCartMoney()
  148. {
  149. $service = new CartService;
  150. $list = $service->getList();
  151. $sum = '0.00';
  152. foreach ($list as $item) {
  153. $temp = bcmul($item['goods_num'], $item['goods']['goods_price_min'], 2);
  154. $sum = bcadd($sum, $temp, 2);
  155. }
  156. return $sum;
  157. }
  158. }