Cart.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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): Json
  91. {
  92. $userId = Session::get('user_id');
  93. if (empty($userId)) {
  94. return $this->renderJson(config('status.not_logged'), 'Log in please!');
  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
  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 $this->renderJson(config('status.not_logged'), 'Log in please!');
  120. }
  121. if ($goodsNum <= 0) {
  122. return $this->renderError('Action Not Supported!');
  123. }
  124. $model = new CartModel;
  125. if (!$model->sUpdate($goodsId, $goodsSkuId, $goodsNum)) {
  126. return $this->renderError($model->getError() ?: '更新失败');
  127. }
  128. // 购物车商品总数量
  129. //$cartTotal = $model->getCartTotal();
  130. $cartMoney = $this->getCartMoney();
  131. return $this->renderSuccess(compact('cartMoney'), '更新成功');
  132. }
  133. /**
  134. * 删除购物车中指定记录
  135. * @param array $cartIds 购物车ID集, 如果为空删除所有
  136. * @return Json
  137. * @throws BaseException
  138. */
  139. public function clear(array $cartIds = []): Json
  140. {
  141. $userId = Session::get('user_id');
  142. if (empty($userId)) {
  143. return $this->renderJson(config('status.not_logged'), 'Log in please!');
  144. }
  145. $model = new CartModel;
  146. if (!$model->clear($cartIds)) {
  147. return $this->renderError($model->getError() ?: '操作失败');
  148. }
  149. // 购物车商品总数量
  150. //$cartTotal = $model->getCartTotal();
  151. $cartMoney = $this->getCartMoney();
  152. return $this->renderSuccess(compact('cartMoney'), '操作成功');
  153. }
  154. private function getCartMoney()
  155. {
  156. $service = new CartService;
  157. $list = $service->getList();
  158. $sum = '0.00';
  159. foreach ($list as $item) {
  160. $temp = bcmul(strval($item['goods_num']), strval($item['goods']['goods_price_min']), 2);
  161. $sum = bcadd($sum, $temp, 2);
  162. }
  163. return $sum;
  164. }
  165. }