Cart.php 6.2 KB

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